| FORMAT | = | { :db => "%Y-%m-%d %H:%M:%S", :dbase => "%Y-%m-%d %H:%M:%S", :datbase => "%Y-%m-%d %H:%M:%S", :utc => "%Y-%m-%d %H:%M:%S", :number => "%Y%m%d%H%M%S", :short => "%d %b %H:%M", :time => "%H:%M", :long => "%B %d, %Y %H:%M", :day1st => "%d-%m-%Y %H:%M", :dmYHM => "%d-%m-%Y %H:%M", :rfc822 => "%a, %d %b %Y %H:%M:%S %z", nil => "%a %b %d %H:%M:%S %Z %Y" |
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.
CREDIT: ActiveSupport Team
# File lib/core/facets/time/advance.rb, line 10
10: def advance(options)
11: d = to_date.advance(options)
12: time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
13: seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
14: seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
15: end
Returns a new Time representing the time a number of time-units ago.
# File lib/core/facets/time/hence.rb, line 15
15: def ago(number, units=:seconds)
16: return hence(-number, units) if number < 0
17:
18: time = (
19: case units.to_s.downcase.to_sym
20: when :years
21: set(:year => (year - number))
22: when :months
23: new_month = ((month - number - 1) % 12) + 1
24: y = (number / 12) + (new_month > month ? 1 : 0)
25: set(:year => (year - y), :month => new_month)
26: when :weeks
27: self - (number * 604800)
28: when :days
29: self - (number * 86400)
30: when :hours
31: self - (number * 3600)
32: when :minutes
33: self - (number * 60)
34: when :seconds, nil
35: self - number
36: else
37: raise ArgumentError, "unrecognized time units -- #{units}"
38: end
39: )
40: dst_adjustment(time)
41: end
Returns a new Time where one or more of the elements have been changed according to the options parameter. The time options (hour, minute, 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.
t = Time.now #=> Sat Dec 01 14:10:15 -0500 2007 t.change(:hour => 11) #=> Sat Dec 01 11:00:00 -0500 2007
# File lib/core/facets/time/change.rb, line 13
13: def change(options)
14: opts=options; #{}; options.each_pair{ |k,v| opts[k] = v.to_i }
15: self.class.send(
16: self.utc? ? :utc : :local,
17: opts[:year] || self.year,
18: opts[:month] || self.month,
19: opts[:day] || self.day,
20: opts[:hour] || self.hour,
21: opts[:min] || (opts[:hour] ? 0 : self.min),
22: opts[:sec] || ((opts[:hour] || opts[:min]) ? 0 : self.sec),
23: opts[:usec] || ((opts[:hour] || opts[:min] || opts[:sec]) ? 0 : self.usec)
24: )
25: end
Adjust DST
TODO: Can‘t seem to get this to pass ActiveSupport tests. Even though it is essentially identical to the ActiveSupport code (see Time#since in time/calculations.rb). It handles all but 4 tests.
# File lib/core/facets/time/hence.rb, line 83
83: def dst_adjustment(time)
84: self_dst = self.dst? ? 1 : 0
85: time_dst = time.dst? ? 1 : 0
86: seconds = (self - time).abs
87: if (seconds >= 86400 && self_dst != time_dst)
88: time + ((self_dst - time_dst) * 60 * 60)
89: else
90: time
91: end
92: end
Returns a new Time representing the time a number of time-units hence.
# File lib/core/facets/time/hence.rb, line 46
46: def hence(number, units=:seconds)
47: return ago(-number, units) if number < 0
48:
49: time = (
50: case units.to_s.downcase.to_sym
51: when :years
52: set( :year=>(year + number) )
53: when :months
54: new_month = ((month + number - 1) % 12) + 1
55: y = (number / 12) + (new_month < month ? 1 : 0)
56: set(:year => (year + y), :month => new_month)
57: when :weeks
58: self + (number * 604800)
59: when :days
60: self + (number * 86400)
61: when :hours
62: self + (number * 3600)
63: when :minutes
64: self + (number * 60)
65: when :seconds
66: self + number
67: else
68: raise ArgumentError, "unrecognized time units -- #{units}"
69: end
70: )
71: dst_adjustment(time)
72: end
Round time at the nearest range (in seconds).
t = Time.now => t.round(60*60) # 1 hour =>
# File lib/core/facets/time/round.rb, line 12
12: def round(amount)
13: (self+amount/2.0).trunc(amount)
14: end
Like change but does not reset earlier times.
NOTE: It would be better, probably if this were called "change".
and that #change were called "reset".
# File lib/core/facets/time/set.rb, line 8
8: def set(options)
9: opts={}; options.each_pair{ |k,v| opts[k] = v.to_i }
10: self.class.send( self.utc? ? :utc : :local,
11: opts[:year] || self.year,
12: opts[:month] || self.month,
13: opts[:day] || self.day,
14: opts[:hour] || self.hour,
15: opts[:min] || self.min,
16: opts[:sec] || self.sec,
17: opts[:usec] || self.usec
18: )
19: end
To be able to keep Dates and Times interchangeable on conversions.
# File lib/core/facets/time/to_time.rb, line 8
8: def to_time
9: getlocal
10: end