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", 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",
  weekday_name_abbr: "a", wwwww: "W", week: "W", wwwwww: "U", week_offset: "U",
  yy: "y", yr: "y", yyyy: "Y", year: "Y"
}
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",
  date: "%B %-d, %Y", date_abbr:"%b %-d, %Y", date_iso: "%Y-%m-%d",
  day: "%B %-d", day_abbr: "%b %-d", day_iso: "%m-%d"
}

Instance Method Summary collapse

Instance Method Details

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



34
35
36
# File 'lib/active_object/date.rb', line 34

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

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



40
41
42
# File 'lib/active_object/date.rb', line 40

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

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



46
47
48
# File 'lib/active_object/date.rb', line 46

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

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



52
53
54
# File 'lib/active_object/date.rb', line 52

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

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



58
59
60
# File 'lib/active_object/date.rb', line 58

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

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



64
65
66
# File 'lib/active_object/date.rb', line 64

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

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



70
71
72
# File 'lib/active_object/date.rb', line 70

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



76
77
78
# File 'lib/active_object/date.rb', line 76

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

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



82
83
84
# File 'lib/active_object/date.rb', line 82

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

#format(string) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/active_object/date.rb', line 88

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

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



99
100
101
# File 'lib/active_object/date.rb', line 99

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