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)
-
- (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.
-
- (Fixnum) months_since_year(base = nil)
Returns the number of months passed between the beginning of the base year and the current date.
-
- (String) padded_month
Returns the current month number with a leading zero if needed.
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.
171 172 173 174 175 |
# 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 |
- (Fixnum) months_since_year(base = nil)
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
153 154 155 |
# File 'lib/lazier/datetime.rb', line 153 def months_since_year(base = nil) (year - (base || ::Date.today.year)) * 12 + month end |
- (String) padded_month
Returns the current month number with a leading zero if needed.
160 161 162 |
# File 'lib/lazier/datetime.rb', line 160 def padded_month month.indexize end |