Class: Time
- Defined in:
- lib/volt/helpers/time.rb,
lib/volt/utils/time_patch.rb
Overview
Much of this class was borrowed from ActiveSupport: github.com/rails/rails/blob/ca9736e78ca9348e785a5c78c8cc085c0c2d4731/activesupport/lib/active_support/core_ext/time/calculations.rb
Instance Method Summary collapse
- #beginning_of_day ⇒ Object
-
#change(options) ⇒ Object
Returns a new Time where one or more of the elements have been changed according to the
options
parameter. -
#end_of_day ⇒ Object
Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9).
- #hash ⇒ Object
Instance Method Details
#beginning_of_day ⇒ Object
29 30 31 32 |
# File 'lib/volt/helpers/time.rb', line 29 def beginning_of_day #(self - seconds_since_midnight).change(usec: 0) change(hour: 0, min: 0, sec: 0) 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
.
Time.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => Time.new(2012, 8, 1, 22, 35, 0)
Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => Time.new(1981, 8, 1, 22, 35, 0)
Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => Time.new(1981, 8, 29, 0, 0, 0)
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/volt/helpers/time.rb', line 16 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 = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000)) # TODO: Opal doesn't have rational yet, so usec's don't get added in right yet ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec, utc_offset) end |
#end_of_day ⇒ Object
Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)
35 36 37 38 39 40 41 42 |
# File 'lib/volt/helpers/time.rb', line 35 def end_of_day change( hour: 23, min: 59, sec: 59, # usec: Rational(999999999, 1000) ) end |
#hash ⇒ Object
8 9 10 |
# File 'lib/volt/utils/time_patch.rb', line 8 def hash "Time:#{to_i}" end |