Class: Time
Overview
The following methods are copied from ActiveSupport’s Time extension: activesupport/lib/active_support/core_ext/time/calculations.rb
Instance Method Summary collapse
-
#advance(options) ⇒ Object
Uses Date to provide precise Time calculations for years, months, and days according to the proleptic Gregorian calendar.
-
#change(options) ⇒ Object
Returns a new Time where one or more of the elements have been changed according to the options parameter.
- #since(seconds) ⇒ Object
Instance Method Details
#advance(options) ⇒ Object
Uses Date to provide precise Time calculations for years, months, and days according to the proleptic Gregorian calendar. The options parameter takes a hash with any of these keys: :years, :months, :weeks, :days, :hours, :minutes, :seconds.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/leap_cli/core_ext/time.rb', line 14 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() d = d.gregorian if d.julian? 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
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. The options parameter takes a hash with any of these keys: :year, :month, :day, :hour, :min, :sec, :usec.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/leap_cli/core_ext/time.rb', line 53 def change() new_year = .fetch(:year, year) new_month = .fetch(:month, month) new_day = .fetch(:day, day) new_hour = .fetch(:hour, hour) new_min = .fetch(:min, [:hour] ? 0 : min) new_sec = .fetch(:sec, ([:hour] || [:min]) ? 0 : sec) new_usec = .fetch(:usec, ([:hour] || [:min] || [:sec]) ? 0 : Rational(nsec, 1000)) if utc? ::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec) elsif zone ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec) else ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset) end end |
#since(seconds) ⇒ Object
39 40 41 42 43 |
# File 'lib/leap_cli/core_ext/time.rb', line 39 def since(seconds) self + seconds rescue to_datetime.since(seconds) end |