module Lazier::DateTime

Extensions for date and time objects.

Public Instance Methods

format(format = nil, custom: true, change_time_zone: false) click to toggle source

Formats a datetime, eventually looking up also custom formats and/or moving to the current timezone. @see Settings#setup_date_formats

@param format [String|NilClass] A format or a custom format name to use for formatting. @param custom [Boolean] Whether to use custom formats. @param change_time_zone [Boolean] Whether to move the date to the current timezone. @return [String] The formatted date.

# File lib/lazier/datetime.rb, line 171
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
months_since_year(base = nil) click to toggle source

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

Example:

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

@param base [DateTime|NilClass] The base year to start computation from. Default to current year. @return [Fixnum] Returns the number of months passed between the beginning of the base year and the current date.

# File lib/lazier/datetime.rb, line 153
def months_since_year(base = nil)
  (year - (base || ::Date.today.year)) * 12 + month
end
padded_month() click to toggle source

Returns the current month number with a leading zero if needed.

@return [String] The current month number with leading zero if needed.

# File lib/lazier/datetime.rb, line 160
def padded_month
  month.indexize
end