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

- (String) format(format = nil, custom: true, change_time_zone: false)

Formats a datetime, eventually looking up also custom formats and/or moving to the current timezone.

Parameters:

  • format (String) (defaults to: nil)

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

  • custom (Boolean)

    Whether to use custom formats.

  • change_time_zone (Boolean)

    Whether to move the date to the current timezone.

Returns:

  • (String)

    The formatted date.

See Also:



166
167
168
169
170
# File 'lib/lazier/datetime.rb', line 166

def format(format = nil, custom: true, change_time_zone: false)
  target = change_time_zone && respond_to?(:in_time_zone) ? in_time_zone : self
  format = custom ? ::DateTime.custom_format(format.to_s).gsub(/(?<!%)(%[ab])/i) { |mo| localize_time_component(mo) } : format.to_s
  target.strftime(format)
end

- (Fixnum) months_since_year(base = nil)

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

ruby 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.



148
149
150
# File 'lib/lazier/datetime.rb', line 148

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

- (String) padded_month

Returns the current month number with leading 0.

Returns:

  • (String)

    The current month number with leading 0.



155
156
157
# File 'lib/lazier/datetime.rb', line 155

def padded_month
  month.indexize
end