Module: Lazier::DateTime

Extended by:
ActiveSupport::Concern
Defined in:
lib/lazier/datetime.rb

Overview

Extensions for date and time objects.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#in_months(base = nil) ⇒ Fixnum

Returns the number of months passed between the beginning of the base year and the current date.

DateTime.civil(2013, 6, 1).in_months(2011)
# => 18

Parameters:

  • base (DateTime) (defaults to: nil)

    The base year to start computation from. Default to current year.

Returns:

  • (Fixnum)

    Returns the number of months passed between the beginning of the base year and the current date.



235
236
237
238
# File 'lib/lazier/datetime.rb', line 235

def in_months(base = nil)
  base ||= ::Date.today.year
  ((self.year) - base) * 12 + self.month
end

#local_lstrftime(format = nil) ⇒ String

Formats a datetime in the current timezone, looking up also custom formats.

Parameters:

  • format (String) (defaults to: nil)

    A format or a custom format name.

Returns:

  • (String)

    The formatted date.

See Also:



269
270
271
# File 'lib/lazier/datetime.rb', line 269

def local_lstrftime(format = nil)
  (self.respond_to?(:in_time_zone) ? self.in_time_zone : self).lstrftime(format)
end

#local_strftime(format = nil) ⇒ String

Formats a datetime in the current timezone.

Parameters:

  • format (String) (defaults to: nil)

    The format to use for formatting.

Returns:

  • (String)

    The formatted date.



260
261
262
# File 'lib/lazier/datetime.rb', line 260

def local_strftime(format = nil)
  (self.respond_to?(:in_time_zone) ? self.in_time_zone : self).strftime(::DateTime.custom_format(format))
end

#lstrftime(format = nil) ⇒ String

Formats a datetime, looking up also custom formats.

Parameters:

  • format (String) (defaults to: nil)

    A format or a custom format name to use for formatting.

Returns:

  • (String)

    The formatted date.

See Also:



252
253
254
# File 'lib/lazier/datetime.rb', line 252

def lstrftime(format = nil)
  self.strftime(::DateTime.custom_format(format).ensure_string.gsub(/(?<!%)(%[ab])/i) {|mo| localize_time_component(mo) })
end

#padded_monthString

Returns the current month number with leading 0.

Returns:

  • (String)

    The current month number with leading 0.



243
244
245
# File 'lib/lazier/datetime.rb', line 243

def padded_month
  self.month.to_s.rjust(2, "0")
end

#utc_timeUTC::Time

Returns the UTC::Time representation of the current datetime.

Returns:

  • (UTC::Time)

    The UTC::Time representation of the current datetime.



221
222
223
224
# File 'lib/lazier/datetime.rb', line 221

def utc_time
  ua = (self.respond_to?(:utc) ? self : self.to_datetime).utc
  ::Time.utc(ua.year, ua.month, ua.day, ua.hour, ua.min, ua.sec)
end