Module: ActiveObject::Date
- Defined in:
- lib/active_object/date.rb
Constant Summary collapse
- MINUTE =
60.0- HOUR =
MINUTE * 60.0
- DAY =
HOUR * 24.0
- WEEK =
DAY * 7.0
- YEAR =
DAY * 365.25
- DECADE =
YEAR * 10.0
- CENTURY =
DECADE * 10.0
- MILLENNIUM =
CENTURY * 10.0
- STRING_UNITS =
{ d: 'd', day: 'd', day_padded: 'd', dd: '-d', Day: '-d', day_unpadded: '-d', ddd: '_d', DAY: '_d', day_blank: '_d', dddd: 'j', day_of_the_year: 'j', m: 'm', month: 'm', yyyy: 'Y', month_padded: 'm', mm: '-m', Month: '-m', month_unpadded: '-m', mmm: '_m', MONTH: '_m', month_blank: '_m', mmmm: 'B', month_name: 'B', mmmmm: 'b', month_name_abbr: 'b', w: 'u', weekday: 'u', ww: 'w', weekday_offset: 'w', www: 'A', weekday_name: 'A', wwww: 'a', year: 'Y', weekday_name_abbr: 'a', wwwww: 'W', week: 'W', wwwwww: 'U', week_offset: 'U', yy: 'y', yr: 'y' }.freeze
- KEY_UNITS =
{ month: '%m', month_padded: '%m', month_unpadded: '%-m', month_blank: '%_m', month_name: '%B', month_name_abbr: '%b', month_year: '%m %Y', month_padded_year: '%m %Y', day_iso: '%m-%d', month_unpadded_year: '%-m %Y', month_blank_year: '%_m %Y', month_name_year: '%B %Y', month_name_abbr_year: '%b %Y', weekday: '%d', weekday_padded: '%d', weekday_unpadded: '%-d', weekday_blank: '%_d', weekday_name: '%A', weekday_name_abbr: '%a', yr: '%y', year_abbr: '%y', year: '%Y', date: '%B %-d, %Y', date_abbr: '%b %-d, %Y', date_iso: '%Y-%m-%d', day: '%B %-d', day_abbr: '%b %-d' }.freeze
Instance Method Summary collapse
- #count_centuries_since(time) ⇒ Object (also: #count_centuries_until)
- #count_days_since(time) ⇒ Object (also: #count_days_until)
- #count_decades_since(time) ⇒ Object (also: #count_decades_until)
- #count_hours_since(time) ⇒ Object (also: #count_hours_until)
- #count_milleniums_since(time) ⇒ Object (also: #count_milleniums_until)
- #count_minutes_since(time) ⇒ Object (also: #count_minutes_until)
- #count_seconds_since(time) ⇒ Object (also: #count_seconds_until)
- #count_weeks_since(time) ⇒ Object (also: #count_weeks_until)
- #count_years_since(time) ⇒ Object (also: #count_years_until)
- #format(string) ⇒ Object
- #to_format(key = :datetime_iso) ⇒ Object (also: #stamp)
Instance Method Details
#count_centuries_since(time) ⇒ Object Also known as: count_centuries_until
29 30 31 |
# File 'lib/active_object/date.rb', line 29 def count_centuries_since(time) count_seconds_since(time) / CENTURY end |
#count_days_since(time) ⇒ Object Also known as: count_days_until
35 36 37 |
# File 'lib/active_object/date.rb', line 35 def count_days_since(time) count_seconds_since(time) / DAY end |
#count_decades_since(time) ⇒ Object Also known as: count_decades_until
41 42 43 |
# File 'lib/active_object/date.rb', line 41 def count_decades_since(time) count_seconds_since(time) / DECADE end |
#count_hours_since(time) ⇒ Object Also known as: count_hours_until
47 48 49 |
# File 'lib/active_object/date.rb', line 47 def count_hours_since(time) count_seconds_since(time) / HOUR end |
#count_milleniums_since(time) ⇒ Object Also known as: count_milleniums_until
53 54 55 |
# File 'lib/active_object/date.rb', line 53 def count_milleniums_since(time) count_seconds_since(time) / MILLENNIUM end |
#count_minutes_since(time) ⇒ Object Also known as: count_minutes_until
59 60 61 |
# File 'lib/active_object/date.rb', line 59 def count_minutes_since(time) count_seconds_since(time) / MINUTE end |
#count_seconds_since(time) ⇒ Object Also known as: count_seconds_until
65 66 67 |
# File 'lib/active_object/date.rb', line 65 def count_seconds_since(time) (to_time.to_f - time.to_time.to_f).abs end |
#count_weeks_since(time) ⇒ Object Also known as: count_weeks_until
71 72 73 |
# File 'lib/active_object/date.rb', line 71 def count_weeks_since(time) count_seconds_since(time) / WEEK end |
#count_years_since(time) ⇒ Object Also known as: count_years_until
77 78 79 |
# File 'lib/active_object/date.rb', line 77 def count_years_since(time) count_seconds_since(time) / YEAR end |
#format(string) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_object/date.rb', line 83 def format(string) delimiters = string.scan(/\W+/) formatters = string.scan(/[a-z0-9_]+/i) string = formatters.map do |unit| "%#{STRING_UNITS.fetch(unit.to_sym)}#{delimiters.shift || ''}" end strftime(string.join) end |