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 leading 0.
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.
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
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.
155 156 157 |
# File 'lib/lazier/datetime.rb', line 155 def padded_month month.indexize end |