Module: ActiveObject::Time

Defined in:
lib/active_object/time.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
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',
  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', hour: '%H', hour_padded: '%H', hour_blank: '%k', hour_imperical: '%I',
  hour_imperical_padded: '%I', hour_imperical_blank: '%l', ampm: '%P', meridian: '%P',
  minute: '%M', second: '%S', time_zone: '%z', time_zone_offset: '%:z', time_padded: '%H:%M',
  time_zone_offset_full: '%::z', time_zone_name: '%Z', date: '%B %-d, %Y', time_tzn: '%H:%M %Z',
  date_abbr: '%b %-d, %Y', date_iso: '%Y-%m-%d', datetime: '%B %-d, %Y %H:%M', time: '%H:%M',
  datetime_abbr: '%b %-d, %Y %H:%M', datetime_iso: '%Y-%m-%d %H:%M', time_imperical: '%I:%M %P',
  datetime_imperical: '%B %-d, %Y %I:%M %P', datetime_imperical_abbr: '%b %-d, %Y %I:%M %P',
  datetime_imperical_iso: '%Y-%m-%d %I:%M %P', datetime_tzn: '%B %-d, %Y %H:%M %Z',
  datetime_abbr_tzn: '%b %-d, %Y %H:%M %Z', datetime_iso_tzn: '%Y-%m-%d %H:%M %z',
  datetime_imperical_tzn: '%B %-d, %Y %I:%M %P %Z', day: '%B %-d', day_abbr: '%b %-d',
  datetime_imperical_abbr_tzn: '%b %-d, %Y %I:%M %P %Z', time_tz: '%H:%M %z', time_blank: '%k:%M',
  datetime_imperical_iso_tzn: '%Y-%m-%d %I:%M %P %z', day_iso: '%m-%d', daytime: '%B %-d %H:%M',
  daytime_abbr: '%b %-d %H:%M', daytime_iso: '%m-%d %H:%M', daytime_imperical: '%B %-d %I:%M %P',
  daytime_imperical_abbr: '%b %-d %I:%M %P', daytime_imperical_iso: '%m-%d %I:%M %P',
  time_imperical_padded: '%I:%M %P', time_imperical_blank: '%l:%M %P',
  time_imperical_tz: '%I:%M %P %z', time_imperical_tzn: '%I:%M %P %Z'
}.freeze
STRING_UNITS =
{
  d: 'd', day: 'd', day_padded: 'd', dd: '-d', Day: '-d', day_unpadded: '-d', ddd: '_d', zz: ':z',
  DAY: '_d', day_blank: '_d', dddd: 'j', day_of_the_year: 'j', m: 'm', month: 'm', hhhh: 'l',
  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', z: 'z',
  weekday: 'u', ww: 'w', weekday_offset: 'w', www: 'A', weekday_name: 'A', wwww: 'a', yyyy: 'Y',
  weekday_name_abbr: 'a', wwwww: 'W', week: 'W', wwwwww: 'U', week_offset: 'U', yy: 'y', yr: 'y',
  year: 'Y', h: 'H', hour: 'H', hour_padded: 'H', hh: 'k', HOUR: 'k', hour_blank: 'k', hhh: 'I',
  hour_imperical: 'I', hour_imperical_padded: 'I', HOUR_IMPERICAL: 'l', hour_imperical_blank: 'l',
  ampm: 'P', meridian: 'P', AMPM: 'p', MERIDIAN: 'p', n: 'M', minute: 'M', s: 'S', second: 'S',
  time_zone: 'z', time_zone_offset: ':z', zzz: '::z', time_zone_offset_full: '::z', zzzz: 'Z',
  time_zone_name: 'Z'
}.freeze

Instance Method Summary collapse

Instance Method Details

#count_centuries_since(time) ⇒ Object Also known as: count_centuries_until



48
49
50
# File 'lib/active_object/time.rb', line 48

def count_centuries_since(time)
  count_seconds_since(time) / CENTURY
end

#count_days_since(time) ⇒ Object Also known as: count_days_until



54
55
56
# File 'lib/active_object/time.rb', line 54

def count_days_since(time)
  count_seconds_since(time) / DAY
end

#count_decades_since(time) ⇒ Object Also known as: count_decades_until



60
61
62
# File 'lib/active_object/time.rb', line 60

def count_decades_since(time)
  count_seconds_since(time) / DECADE
end

#count_hours_since(time) ⇒ Object Also known as: count_hours_until



66
67
68
# File 'lib/active_object/time.rb', line 66

def count_hours_since(time)
  count_seconds_since(time) / HOUR
end

#count_milleniums_since(time) ⇒ Object Also known as: count_milleniums_until



72
73
74
# File 'lib/active_object/time.rb', line 72

def count_milleniums_since(time)
  count_seconds_since(time) / MILLENNIUM
end

#count_minutes_since(time) ⇒ Object Also known as: count_minutes_until



78
79
80
# File 'lib/active_object/time.rb', line 78

def count_minutes_since(time)
  count_seconds_since(time) / MINUTE
end

#count_seconds_since(time) ⇒ Object Also known as: count_seconds_until



84
85
86
87
# File 'lib/active_object/time.rb', line 84

def count_seconds_since(time)
  time = time.to_time if time.respond_to?(:to_time)
  (to_f - time.to_f).abs
end

#count_weeks_since(time) ⇒ Object Also known as: count_weeks_until



91
92
93
# File 'lib/active_object/time.rb', line 91

def count_weeks_since(time)
  count_seconds_since(time) / WEEK
end

#count_years_since(time) ⇒ Object Also known as: count_years_until



97
98
99
# File 'lib/active_object/time.rb', line 97

def count_years_since(time)
  count_seconds_since(time) / YEAR
end

#format(string) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/active_object/time.rb', line 103

def format(string)
  delimiters = string.scan(/\W+/)
  formatters = string.scan(/[a-z0-9_]+/i)

  string = formatters.map { |unit| "%#{STRING_UNITS.fetch(unit.to_sym)}#{delimiters.shift}" }

  strftime(string.join)
end

#to_format(key = :datetime_iso) ⇒ Object Also known as: stamp



112
113
114
# File 'lib/active_object/time.rb', line 112

def to_format(key = :datetime_iso)
  strftime(KEY_UNITS.fetch(key.to_sym))
end