Module: Hosemonkey::Time

Included in:
Time
Defined in:
lib/hosemonkey/ext/time.rb

Constant Summary collapse

COMMON_YEAR_DAYS_IN_MONTH =

Copied directly from ActiveSupport

[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
DAYS_INTO_WEEK =

Copied directly from ActiveSupport

{
  :monday    => 0,
  :tuesday   => 1,
  :wednesday => 2,
  :thursday  => 3,
  :friday    => 4,
  :saturday  => 5,
  :sunday    => 6
}

Instance Method Summary collapse

Instance Method Details

#advance(options) ⇒ Object

Copied directly from ActiveSupport Uses Date to provide precise Time calculations for years, months, and days. The options parameter takes a hash with any of these keys: :years, :months, :weeks, :days, :hours, :minutes, :seconds.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hosemonkey/ext/time.rb', line 32

def advance(options)
  unless options[:weeks].nil?
    options[:weeks], partial_weeks = options[:weeks].divmod(1)
    options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
  end

  unless options[:days].nil?
    options[:days], partial_days = options[:days].divmod(1)
    options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
  end

  d = to_date.advance(options)
  time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
  seconds_to_advance = \
    options.fetch(:seconds, 0) +
    options.fetch(:minutes, 0) * 60 +
    options.fetch(:hours, 0) * 3600

  if seconds_to_advance.zero?
    time_advanced_by_date
  else
    time_advanced_by_date.since(seconds_to_advance)
  end
end

#change(options) ⇒ Object

Copied directly from ActiveSupport Returns a new Time where one or more of the elements have been changed according to the options parameter. The time options (hour, min, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and minute is passed, then sec and usec is set to 0.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hosemonkey/ext/time.rb', line 61

def change(options)
  ::Time.send(
    utc? ? :utc_time : :local_time,
    options.fetch(:year, year),
    options.fetch(:month, month),
    options.fetch(:day, day),
    options.fetch(:hour, hour),
    options.fetch(:min, options[:hour] ? 0 : min),
    options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec),
    options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
  )
end

#days_in_month(month, year = now.year) ⇒ Object

Copied directly from ActiveSupport Return the number of days in the given month. If no year is specified, it will use the current year.



19
20
21
22
23
24
25
# File 'lib/hosemonkey/ext/time.rb', line 19

def days_in_month(month, year = now.year)
  if month == 2 && ::Date.gregorian_leap?(year)
    29
  else
    COMMON_YEAR_DAYS_IN_MONTH[month]
  end
end

#to_shortObject



82
83
84
# File 'lib/hosemonkey/ext/time.rb', line 82

def to_short
  self.gmtime.strftime("%Y%m")
end

#to_webObject



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

def to_web
  self.gmtime.strftime("%a, %d %b %Y %H:%M:%S %z")
end

#to_zObject



74
75
76
# File 'lib/hosemonkey/ext/time.rb', line 74

def to_z
  self.gmtime.strftime("%Y-%m-%dT%H:%M:%S.000Z")
end