class Time

Much of this class was borrowed from ActiveSupport: github.com/rails/rails/blob/ca9736e78ca9348e785a5c78c8cc085c0c2d4731/activesupport/lib/active_support/core_ext/time/calculations.rb

Public Instance Methods

beginning_of_day() click to toggle source
# 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) click to toggle source

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)
# File lib/volt/helpers/time.rb, line 16
def change(options)
  new_year  = options.fetch(:year, year)
  new_month = options.fetch(:month, month)
  new_day   = options.fetch(:day, day)
  new_hour  = options.fetch(:hour, hour)
  new_min   = options.fetch(:min, options[:hour] ? 0 : min)
  new_sec   = options.fetch(:sec, (options[:hour] || options[: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() click to toggle source

Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)

# 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() click to toggle source
# File lib/volt/utils/time_patch.rb, line 8
def hash
  "Time:#{to_i}"
end