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
-
#advance(options) ⇒ Object
Copied directly from ActiveSupport Uses Date to provide precise Time calculations for years, months, and days.
-
#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. -
#days_in_month(month, year = now.year) ⇒ Object
Copied directly from ActiveSupport Return the number of days in the given month.
- #to_short ⇒ Object
- #to_web ⇒ Object
- #to_z ⇒ Object
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() unless [:weeks].nil? [:weeks], partial_weeks = [:weeks].divmod(1) [:days] = .fetch(:days, 0) + 7 * partial_weeks end unless [:days].nil? [:days], partial_days = [:days].divmod(1) [:hours] = .fetch(:hours, 0) + 24 * partial_days end d = to_date.advance() time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day) seconds_to_advance = \ .fetch(:seconds, 0) + .fetch(:minutes, 0) * 60 + .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() ::Time.send( utc? ? :utc_time : :local_time, .fetch(:year, year), .fetch(:month, month), .fetch(:day, day), .fetch(:hour, hour), .fetch(:min, [:hour] ? 0 : min), .fetch(:sec, ([:hour] || [:min]) ? 0 : sec), .fetch(:usec, ([:hour] || [:min] || [: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_short ⇒ Object
82 83 84 |
# File 'lib/hosemonkey/ext/time.rb', line 82 def to_short self.gmtime.strftime("%Y%m") end |
#to_web ⇒ Object
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_z ⇒ Object
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 |