Skip to content
Newer
Older
100644 271 lines (236 sloc) 9.77 KB
2ace3d9 @jeremy Explicitly require AS::Duration
jeremy authored
1 require 'active_support/duration'
780ee36 @guilleiguaran Fix AR test suite error under Rubinius 2.0
guilleiguaran authored
2 require 'active_support/core_ext/time/conversions'
e6d4dc3 @rsutphin Add missing require.
rsutphin authored
3 require 'active_support/time_with_zone'
aa6f512 @fxn adds a missing require [fixes #6896]
fxn authored
4 require 'active_support/core_ext/time/zones'
b530fd1 @panthomakos Refactored common date and time calculations.
panthomakos authored
5 require 'active_support/core_ext/date_and_time/calculations'
48583f8 @pixeltrix Deprecate obsolete Time to DateTime fallback methods
pixeltrix authored
6 require 'active_support/deprecation'
2ace3d9 @jeremy Explicitly require AS::Duration
jeremy authored
7
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
8 class Time
b530fd1 @panthomakos Refactored common date and time calculations.
panthomakos authored
9 include DateAndTime::Calculations
10
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
11 COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
12
13 class << self
14 # Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances
15 def ===(other)
1ebf84a @jeremyevans Edited activesupport/lib/active_support/core_ext/time/calculations.rb…
jeremyevans authored
16 super || (self == Time && other.is_a?(ActiveSupport::TimeWithZone))
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
17 end
18
19 # Return the number of days in the given month.
20 # If no year is specified, it will use the current year.
21 def days_in_month(month, year = now.year)
9257224 @gazay AS core_ext refactoring pt.2
gazay authored
22 if month == 2 && ::Date.gregorian_leap?(year)
23 29
24 else
25 COMMON_YEAR_DAYS_IN_MONTH[month]
26 end
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
27 end
28
48583f8 @pixeltrix Deprecate obsolete Time to DateTime fallback methods
pixeltrix authored
29 # *DEPRECATED*: Use +Time#utc+ or +Time#local+ instead.
30 #
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
31 # Returns a new Time if requested year can be accommodated by Ruby's Time class
32 # (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
bc1bcdd @fxn implements weeks_ago and prev_week for Date/DateTime/Time [#5122 stat…
fxn authored
33 # otherwise returns a DateTime.
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
34 def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
48583f8 @pixeltrix Deprecate obsolete Time to DateTime fallback methods
pixeltrix authored
35 ActiveSupport::Deprecation.warn 'time_with_datetime_fallback is deprecated. Use Time#utc or Time#local instead', caller
53c1cd6 @fxn let Time.time_with_datetime_fallback handle properly years in the ran…
fxn authored
36 time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
1946d7b @gazay AS core_ext refactoring
gazay authored
37
53c1cd6 @fxn let Time.time_with_datetime_fallback handle properly years in the ran…
fxn authored
38 # This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
1946d7b @gazay AS core_ext refactoring
gazay authored
39 if time.year == year
40 time
41 else
42 ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
43 end
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
44 rescue
53c1cd6 @fxn let Time.time_with_datetime_fallback handle properly years in the ran…
fxn authored
45 ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
46 end
47
48583f8 @pixeltrix Deprecate obsolete Time to DateTime fallback methods
pixeltrix authored
48 # *DEPRECATED*: Use +Time#utc+ instead.
49 #
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
50 # Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>.
51 def utc_time(*args)
48583f8 @pixeltrix Deprecate obsolete Time to DateTime fallback methods
pixeltrix authored
52 ActiveSupport::Deprecation.warn 'utc_time is deprecated. Use Time#utc instead', caller
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
53 time_with_datetime_fallback(:utc, *args)
54 end
55
48583f8 @pixeltrix Deprecate obsolete Time to DateTime fallback methods
pixeltrix authored
56 # *DEPRECATED*: Use +Time#local+ instead.
57 #
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
58 # Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:local</tt>.
59 def local_time(*args)
48583f8 @pixeltrix Deprecate obsolete Time to DateTime fallback methods
pixeltrix authored
60 ActiveSupport::Deprecation.warn 'local_time is deprecated. Use Time#local instead', caller
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
61 time_with_datetime_fallback(:local, *args)
62 end
201f373 @spastorino Refactor move some date, time and date_time methods to */zones and fi…
spastorino authored
63
0f8d279 @joshk updated Time, Date and DateTime current methods in AS to use Time.zon…
joshk authored
64 # Returns <tt>Time.zone.now</tt> when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise just returns <tt>Time.now</tt>.
201f373 @spastorino Refactor move some date, time and date_time methods to */zones and fi…
spastorino authored
65 def current
0f8d279 @joshk updated Time, Date and DateTime current methods in AS to use Time.zon…
joshk authored
66 ::Time.zone ? ::Time.zone.now : ::Time.now
201f373 @spastorino Refactor move some date, time and date_time methods to */zones and fi…
spastorino authored
67 end
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
68 end
69
70 # Seconds since midnight: Time.now.seconds_since_midnight
71 def seconds_since_midnight
72 to_i - change(:hour => 0).to_i + (usec / 1.0e+6)
73 end
74
ce73055 @exviva Add #seconds_until_end_of_day to DateTime and Time
exviva authored
75 # Returns the number of seconds until 23:59:59.
76 #
77 # Time.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
78 # Time.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
79 # Time.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0
80 def seconds_until_end_of_day
81 end_of_day.to_i - to_i
82 end
83
f4e1805 update some AS code examples to 1.9 hash syntax [ci skip]
Francesco Rodriguez authored
84 # Returns a new Time where one or more of the elements have been changed according
85 # to the +options+ parameter. The time options (<tt>:hour</tt>, <tt>:min</tt>,
86 # <tt>:sec</tt>, <tt>:usec</tt>) reset cascadingly, so if only the hour is passed,
87 # then minute, sec, and usec is set to 0. If the hour and minute is passed, then
88 # sec and usec is set to 0. The +options+ parameter takes a hash with any of these
89 # keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>,
90 # <tt>:sec</tt>, <tt>:usec</tt>.
67fa3e8 @jeffshantz Expand documentation for change() method in Time, Date, and DateTime
jeffshantz authored
91 #
f4e1805 update some AS code examples to 1.9 hash syntax [ci skip]
Francesco Rodriguez authored
92 # Time.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => Time.new(2012, 8, 1, 22, 35, 0)
93 # Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => Time.new(1981, 8, 1, 22, 35, 0)
94 # Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => Time.new(1981, 8, 29, 0, 0, 0)
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
95 def change(options)
98b46bf @pixeltrix Make Time#change work with offsets other than UTC or local
pixeltrix authored
96 new_year = options.fetch(:year, year)
97 new_month = options.fetch(:month, month)
98 new_day = options.fetch(:day, day)
99 new_hour = options.fetch(:hour, hour)
100 new_min = options.fetch(:min, options[:hour] ? 0 : min)
101 new_sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec)
102 new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
103
104 if utc?
105 ::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
106 elsif zone
107 ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
108 else
109 ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset)
110 end
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
111 end
112
113 # Uses Date to provide precise Time calculations for years, months, and days.
114 # The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
115 # <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
116 # <tt>:minutes</tt>, <tt>:seconds</tt>.
117 def advance(options)
118 unless options[:weeks].nil?
119 options[:weeks], partial_weeks = options[:weeks].divmod(1)
1946d7b @gazay AS core_ext refactoring
gazay authored
120 options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
121 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
122
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
123 unless options[:days].nil?
124 options[:days], partial_days = options[:days].divmod(1)
1946d7b @gazay AS core_ext refactoring
gazay authored
125 options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
126 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
127
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
128 d = to_date.advance(options)
129 time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
1946d7b @gazay AS core_ext refactoring
gazay authored
130 seconds_to_advance = \
131 options.fetch(:seconds, 0) +
132 options.fetch(:minutes, 0) * 60 +
133 options.fetch(:hours, 0) * 3600
134
135 if seconds_to_advance.zero?
136 time_advanced_by_date
137 else
138 time_advanced_by_date.since(seconds_to_advance)
139 end
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
140 end
141
142 # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
143 def ago(seconds)
144 since(-seconds)
145 end
146
685a53a @jeremy Merge branch 'master' into cherry
jeremy authored
147 # Returns a new Time representing the time a number of seconds since the instance time
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
148 def since(seconds)
685a53a @jeremy Merge branch 'master' into cherry
jeremy authored
149 self + seconds
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
150 rescue
685a53a @jeremy Merge branch 'master' into cherry
jeremy authored
151 to_datetime.since(seconds)
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
152 end
153 alias :in :since
154
155 # Returns a new Time representing the start of the day (0:00)
156 def beginning_of_day
71c67d1 @AvnerCohen Hash Syntax changes to 1.9 format
AvnerCohen authored
157 #(self - seconds_since_midnight).change(usec: 0)
377bd69 @farski Removed some redundant Time#change time options from beginning_of_ me…
farski authored
158 change(:hour => 0)
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
159 end
160 alias :midnight :beginning_of_day
161 alias :at_midnight :beginning_of_day
162 alias :at_beginning_of_day :beginning_of_day
163
3545d6b @hugopeixoto Setting usec (and nsec for Ruby 1.9) on Time#end_of_* methods [#1255
hugopeixoto authored
164 # Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
165 def end_of_day
1946d7b @gazay AS core_ext refactoring
gazay authored
166 change(
167 :hour => 23,
168 :min => 59,
169 :sec => 59,
dcdde7d @nashby respect nsec in TimeWithZone
nashby authored
170 :usec => Rational(999999999, 1000)
1946d7b @gazay AS core_ext refactoring
gazay authored
171 )
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
172 end
6816978 @pixeltrix Add missing at_end_of_* aliases
pixeltrix authored
173 alias :at_end_of_day :end_of_day
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
174
8c3f4be @mjtko added beginning_of_hour support to core_ext calculations for Time and…
mjtko authored
175 # Returns a new Time representing the start of the hour (x:00)
176 def beginning_of_hour
177 change(:min => 0)
178 end
179 alias :at_beginning_of_hour :beginning_of_hour
180
181 # Returns a new Time representing the end of the hour, x:59:59.999999 (.999999999 in ruby1.9)
182 def end_of_hour
183 change(
184 :min => 59,
185 :sec => 59,
dcdde7d @nashby respect nsec in TimeWithZone
nashby authored
186 :usec => Rational(999999999, 1000)
8c3f4be @mjtko added beginning_of_hour support to core_ext calculations for Time and…
mjtko authored
187 )
188 end
6816978 @pixeltrix Add missing at_end_of_* aliases
pixeltrix authored
189 alias :at_end_of_hour :end_of_hour
8c3f4be @mjtko added beginning_of_hour support to core_ext calculations for Time and…
mjtko authored
190
d6cc0e5 @dhh Added Time#whole_day/week/quarter/year as a way of generating ranges …
dhh authored
191 # Returns a Range representing the whole day of the current time.
a646780 @dhh Rename Time#whole_* to Time#all_* [thanks Pratik!]
dhh authored
192 def all_day
d6cc0e5 @dhh Added Time#whole_day/week/quarter/year as a way of generating ranges …
dhh authored
193 beginning_of_day..end_of_day
194 end
195
da26dfb @gregolsen Date.beginning_of_week thread local and beginning_of_week application…
gregolsen authored
196 # Returns a Range representing the whole week of the current time.
197 # Week starts on start_day, default is <tt>Date.week_start</tt> or <tt>config.week_start</tt> when set.
198 def all_week(start_day = Date.beginning_of_week)
9abdabf @kennyj Optional start_day argument for Time#all_week.
kennyj authored
199 beginning_of_week(start_day)..end_of_week(start_day)
d6cc0e5 @dhh Added Time#whole_day/week/quarter/year as a way of generating ranges …
dhh authored
200 end
201
202 # Returns a Range representing the whole month of the current time.
a646780 @dhh Rename Time#whole_* to Time#all_* [thanks Pratik!]
dhh authored
203 def all_month
d6cc0e5 @dhh Added Time#whole_day/week/quarter/year as a way of generating ranges …
dhh authored
204 beginning_of_month..end_of_month
205 end
206
207 # Returns a Range representing the whole quarter of the current time.
a646780 @dhh Rename Time#whole_* to Time#all_* [thanks Pratik!]
dhh authored
208 def all_quarter
d6cc0e5 @dhh Added Time#whole_day/week/quarter/year as a way of generating ranges …
dhh authored
209 beginning_of_quarter..end_of_quarter
210 end
211
212 # Returns a Range representing the whole year of the current time.
a646780 @dhh Rename Time#whole_* to Time#all_* [thanks Pratik!]
dhh authored
213 def all_year
d6cc0e5 @dhh Added Time#whole_day/week/quarter/year as a way of generating ranges …
dhh authored
214 beginning_of_year..end_of_year
215 end
216
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
217 def plus_with_duration(other) #:nodoc:
218 if ActiveSupport::Duration === other
219 other.since(self)
220 else
221 plus_without_duration(other)
222 end
223 end
224 alias_method :plus_without_duration, :+
225 alias_method :+, :plus_with_duration
226
227 def minus_with_duration(other) #:nodoc:
228 if ActiveSupport::Duration === other
229 other.until(self)
230 else
231 minus_without_duration(other)
232 end
233 end
234 alias_method :minus_without_duration, :-
235 alias_method :-, :minus_with_duration
236
237 # Time#- can also be used to determine the number of seconds between two Time instances.
238 # We're layering on additional behavior so that ActiveSupport::TimeWithZone instances
239 # are coerced into values that Time#- will recognize
240 def minus_with_coercion(other)
241 other = other.comparable_time if other.respond_to?(:comparable_time)
e98f957 @gbuesing Time#- with a DateTime argument behaves the same as with a Time argum…
gbuesing authored
242 other.is_a?(DateTime) ? to_f - other.to_f : minus_without_coercion(other)
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
243 end
244 alias_method :minus_without_coercion, :-
245 alias_method :-, :minus_with_coercion
246
247 # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances
248 # can be chronologically compared with a Time
249 def compare_with_coercion(other)
7872cc9 @dcrec1 refactored Time#<=> and DateTime#<=> by removing unnecessary calls wi…
dcrec1 authored
250 # we're avoiding Time#to_datetime cause it's expensive
1946d7b @gazay AS core_ext refactoring
gazay authored
251 if other.is_a?(Time)
252 compare_without_coercion(other.to_time)
253 else
254 to_datetime <=> other
255 end
6ff54f7 @dhh Added Time::Calculations to ask for things like Time.now.tomorrow, Ti…
dhh authored
256 end
1c5a694 @jeremy Convert Time extension modules to class reopens
jeremy authored
257 alias_method :compare_without_coercion, :<=>
258 alias_method :<=>, :compare_with_coercion
a491207 @marcandre Fix inconsistencies with Time{WithZone}#{hash,eql?}
marcandre authored
259
260 # Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances
261 # can be eql? to an equivalent Time
262 def eql_with_coercion(other)
263 # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do eql? comparison
264 other = other.comparable_time if other.respond_to?(:comparable_time)
265 eql_without_coercion(other)
266 end
267 alias_method :eql_without_coercion, :eql?
268 alias_method :eql?, :eql_with_coercion
1946d7b @gazay AS core_ext refactoring
gazay authored
269
b4c34f7 @jamis Fixed that Time#midnight would have a non-zero usec on some platforms #…
jamis authored
270 end
Something went wrong with that request. Please try again.