public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Introduce convenience methods past?, today? and future? for Date and Time 
classes to facilitate Date/Time comparisons.
clemens (author)
Thu Sep 11 03:51:16 -0700 2008
gbuesing (committer)
Sun Sep 14 19:21:19 -0700 2008
commit  bfa12d7a02ce0e84fcd2b83f2ce6fee1386757e3
tree    d0b4637922d92bcaec89d9e36cc05c5ff11738e7
parent  f636c6fda037fbef25e98c81d019a6c600a18f66
...
20
21
22
23
 
24
25
26
27
28
 
29
30
31
32
33
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
36
37
...
57
58
59
60
 
61
62
63
...
65
66
67
68
 
69
70
71
...
73
74
75
76
77
 
 
78
79
80
...
98
99
100
101
 
102
103
104
...
161
162
163
164
 
165
166
167
...
20
21
22
 
23
24
25
26
27
 
28
29
30
31
32
33
 
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
...
72
73
74
 
75
76
77
78
...
80
81
82
 
83
84
85
86
...
88
89
90
 
 
91
92
93
94
95
...
113
114
115
 
116
117
118
119
...
176
177
178
 
179
180
181
182
0
@@ -20,18 +20,33 @@ module ActiveSupport #:nodoc:
0
           def yesterday
0
             ::Date.today.yesterday
0
           end
0
-          
0
+
0
           # Returns a new Date representing the date 1 day after today (i.e. tomorrow's date).
0
           def tomorrow
0
             ::Date.today.tomorrow
0
           end
0
-          
0
+
0
           # Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.
0
           def current
0
             ::Time.zone_default ? ::Time.zone.today : ::Date.today
0
           end
0
         end
0
-        
0
+
0
+        # Tells whether the Date object's date lies in the past
0
+        def past?
0
+          self < ::Date.current
0
+        end
0
+
0
+        # Tells whether the Date object's date is today
0
+        def today?
0
+          self.to_date == ::Date.current # we need the to_date because of DateTime
0
+        end
0
+
0
+        # Tells whether the Date object's date lies in the future
0
+        def future?
0
+          self > ::Date.current
0
+        end
0
+
0
         # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
0
         # and then subtracts the specified number of seconds
0
         def ago(seconds)
0
@@ -57,7 +72,7 @@ module ActiveSupport #:nodoc:
0
         def end_of_day
0
           to_time.end_of_day
0
         end
0
-        
0
+
0
         def plus_with_duration(other) #:nodoc:
0
           if ActiveSupport::Duration === other
0
             other.since(self)
0
@@ -65,7 +80,7 @@ module ActiveSupport #:nodoc:
0
             plus_without_duration(other)
0
           end
0
         end
0
-        
0
+
0
         def minus_with_duration(other) #:nodoc:
0
           if ActiveSupport::Duration === other
0
             plus_with_duration(-other)
0
@@ -73,8 +88,8 @@ module ActiveSupport #:nodoc:
0
             minus_without_duration(other)
0
           end
0
         end
0
-        
0
-        # Provides precise Date calculations for years, months, and days.  The +options+ parameter takes a hash with 
0
+
0
+        # Provides precise Date calculations for years, months, and days.  The +options+ parameter takes a hash with
0
         # any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
0
         def advance(options)
0
           d = self
0
@@ -98,7 +113,7 @@ module ActiveSupport #:nodoc:
0
             options[:day]   || self.day
0
           )
0
         end
0
-        
0
+
0
         # Returns a new Date/DateTime representing the time a number of specified months ago
0
         def months_ago(months)
0
           advance(:months => -months)
0
@@ -161,7 +176,7 @@ module ActiveSupport #:nodoc:
0
           days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
0
           result = (self + 7).beginning_of_week + days_into_week[day]
0
           self.acts_like?(:time) ? result.change(:hour => 0) : result
0
-        end          
0
+        end
0
 
0
         # Returns a new ; DateTime objects will have time set to 0:00DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
0
         def beginning_of_month
...
7
8
9
10
 
11
12
13
...
19
20
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
23
24
...
78
79
80
81
 
82
83
84
...
89
90
91
92
 
93
94
95
96
97
 
98
99
100
101
102
 
103
104
105
...
7
8
9
 
10
11
12
13
...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
92
93
94
 
95
96
97
98
...
103
104
105
 
106
107
108
109
110
 
111
112
113
114
115
 
116
117
118
119
0
@@ -7,7 +7,7 @@ module ActiveSupport #:nodoc:
0
       module Calculations
0
         def self.included(base) #:nodoc:
0
           base.extend ClassMethods
0
-          
0
+
0
           base.class_eval do
0
             alias_method :compare_without_coercion, :<=>
0
             alias_method :<=>, :compare_with_coercion
0
@@ -19,6 +19,20 @@ module ActiveSupport #:nodoc:
0
           def local_offset
0
             ::Time.local(2007).utc_offset.to_r / 86400
0
           end
0
+
0
+          def current
0
+            ::Time.zone_default ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime
0
+          end
0
+        end
0
+
0
+        # Tells whether the DateTime object's datetime lies in the past
0
+        def past?
0
+          self < ::DateTime.current
0
+        end
0
+
0
+        # Tells whether the DateTime object's datetime lies in the future
0
+        def future?
0
+          self > ::DateTime.current
0
         end
0
 
0
         # Seconds since midnight: DateTime.now.seconds_since_midnight
0
@@ -78,7 +92,7 @@ module ActiveSupport #:nodoc:
0
         def end_of_day
0
           change(:hour => 23, :min => 59, :sec => 59)
0
         end
0
-        
0
+
0
         # Adjusts DateTime to UTC by adding its offset value; offset is set to 0
0
         #
0
         # Example:
0
@@ -89,17 +103,17 @@ module ActiveSupport #:nodoc:
0
           new_offset(0)
0
         end
0
         alias_method :getutc, :utc
0
-        
0
+
0
         # Returns true if offset == 0
0
         def utc?
0
           offset == 0
0
         end
0
-        
0
+
0
         # Returns the offset value in seconds
0
         def utc_offset
0
           (offset * 86400).to_i
0
         end
0
-        
0
+
0
         # Layers additional behavior on DateTime#<=> so that Time and ActiveSupport::TimeWithZone instances can be compared with a DateTime
0
         def compare_with_coercion(other)
0
           other = other.comparable_time if other.respond_to?(:comparable_time)
...
9
10
11
12
 
13
14
15
 
16
17
18
 
19
20
21
...
28
29
30
31
32
33
 
 
 
34
35
36
...
57
58
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
61
62
...
106
107
108
109
 
110
111
112
...
199
200
201
202
 
203
204
205
...
249
250
251
252
 
253
254
255
...
257
258
259
260
 
261
262
263
...
9
10
11
 
12
13
14
 
15
16
17
 
18
19
20
21
...
28
29
30
 
 
 
31
32
33
34
35
36
...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
...
121
122
123
 
124
125
126
127
...
214
215
216
 
217
218
219
220
...
264
265
266
 
267
268
269
270
...
272
273
274
 
275
276
277
278
0
@@ -9,13 +9,13 @@ module ActiveSupport #:nodoc:
0
           base.class_eval do
0
             alias_method :plus_without_duration, :+
0
             alias_method :+, :plus_with_duration
0
-            
0
+
0
             alias_method :minus_without_duration, :-
0
             alias_method :-, :minus_with_duration
0
-            
0
+
0
             alias_method :minus_without_coercion, :-
0
             alias_method :-, :minus_with_coercion
0
-            
0
+
0
             alias_method :compare_without_coercion, :<=>
0
             alias_method :<=>, :compare_with_coercion
0
           end
0
@@ -28,9 +28,9 @@ module ActiveSupport #:nodoc:
0
           def ===(other)
0
             other.is_a?(::Time)
0
           end
0
-          
0
-          # Return the number of days in the given month. 
0
-          # If no year is specified, it will use the current year. 
0
+
0
+          # Return the number of days in the given month.
0
+          # If no year is specified, it will use the current year.
0
           def days_in_month(month, year = now.year)
0
             return 29 if month == 2 && ::Date.gregorian_leap?(year)
0
             COMMON_YEAR_DAYS_IN_MONTH[month]
0
@@ -57,6 +57,21 @@ module ActiveSupport #:nodoc:
0
           end
0
         end
0
 
0
+        # Tells whether the Time object's time lies in the past
0
+        def past?
0
+          self < ::Time.current
0
+        end
0
+
0
+        # Tells whether the Time object's time is today
0
+        def today?
0
+          self.to_date == ::Date.current
0
+        end
0
+
0
+        # Tells whether the Time object's time lies in the future
0
+        def future?
0
+          self > ::Time.current
0
+        end
0
+
0
         # Seconds since midnight: Time.now.seconds_since_midnight
0
         def seconds_since_midnight
0
           self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
0
@@ -106,7 +121,7 @@ module ActiveSupport #:nodoc:
0
             (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
0
           end
0
         rescue
0
-          self.to_datetime.since(seconds)          
0
+          self.to_datetime.since(seconds)
0
         end
0
         alias :in :since
0
 
0
@@ -199,7 +214,7 @@ module ActiveSupport #:nodoc:
0
           change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 0)
0
         end
0
         alias :at_end_of_month :end_of_month
0
-    
0
+
0
         # Returns  a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
0
         def beginning_of_quarter
0
           beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
0
@@ -249,7 +264,7 @@ module ActiveSupport #:nodoc:
0
             minus_without_duration(other)
0
           end
0
         end
0
-        
0
+
0
         # Time#- can also be used to determine the number of seconds between two Time instances.
0
         # We're layering on additional behavior so that ActiveSupport::TimeWithZone instances
0
         # are coerced into values that Time#- will recognize
0
@@ -257,7 +272,7 @@ module ActiveSupport #:nodoc:
0
           other = other.comparable_time if other.respond_to?(:comparable_time)
0
           minus_without_coercion(other)
0
         end
0
-        
0
+
0
         # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances
0
         # can be chronologically compared with a Time
0
         def compare_with_coercion(other)
...
1
2
3
 
4
5
6
...
32
33
34
35
 
36
37
38
39
40
 
41
42
43
...
51
52
53
54
 
55
56
57
...
62
63
64
65
 
66
67
68
69
70
71
 
72
73
74
75
76
 
77
78
79
80
81
 
82
83
84
85
86
87
 
88
89
90
91
 
92
93
94
95
96
 
97
98
99
...
122
123
124
125
 
126
127
128
...
130
131
132
133
 
134
135
136
137
 
138
139
140
141
142
 
143
144
145
 
146
147
148
...
150
151
152
153
 
154
155
156
157
158
159
160
 
161
162
163
164
165
 
166
167
168
169
 
 
 
 
 
 
 
 
 
 
 
 
 
170
171
172
173
 
174
175
176
...
194
195
196
197
 
198
199
200
...
204
205
206
207
 
208
209
210
...
218
219
220
221
 
222
223
224
...
226
227
228
229
 
230
231
232
233
 
234
235
236
237
 
238
239
240
241
 
 
242
243
244
245
246
247
 
248
249
250
251
252
 
253
254
255
256
 
257
258
259
260
261
 
262
263
264
265
266
267
 
268
269
270
...
273
274
275
276
 
277
278
279
...
290
291
292
293
294
 
 
295
296
 
297
298
299
...
304
305
306
307
 
308
309
310
311
 
312
313
314
...
1
2
 
3
4
5
6
...
32
33
34
 
35
36
37
38
39
 
40
41
42
43
...
51
52
53
 
54
55
56
57
...
62
63
64
 
65
66
67
68
69
70
 
71
72
73
74
75
 
76
77
78
79
80
 
81
82
83
84
85
86
 
87
88
89
90
 
91
92
93
94
95
 
96
97
98
99
...
122
123
124
 
125
126
127
128
...
130
131
132
 
133
134
135
136
 
137
138
139
140
141
 
142
143
144
 
145
146
147
148
...
150
151
152
 
153
154
155
156
157
158
159
 
160
161
162
163
164
 
165
166
167
168
 
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
 
185
186
187
188
...
206
207
208
 
209
210
211
212
...
216
217
218
 
219
220
221
222
...
230
231
232
 
233
234
235
236
...
238
239
240
 
241
242
243
244
 
245
246
247
248
 
249
250
251
 
 
252
253
254
255
256
257
258
 
259
260
261
262
263
 
264
265
266
267
 
268
269
270
271
272
 
273
274
275
276
277
278
 
279
280
281
282
...
285
286
287
 
288
289
290
291
...
302
303
304
 
 
305
306
307
 
308
309
310
311
...
316
317
318
 
319
320
321
322
 
323
324
325
326
0
@@ -1,6 +1,6 @@
0
 require 'tzinfo'
0
 module ActiveSupport
0
-  # A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are 
0
+  # A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are
0
   # limited to UTC and the system's <tt>ENV['TZ']</tt> zone.
0
   #
0
   # You shouldn't ever need to create a TimeWithZone instance directly via <tt>new</tt> -- instead, Rails provides the methods
0
@@ -32,12 +32,12 @@ module ActiveSupport
0
   class TimeWithZone
0
     include Comparable
0
     attr_reader :time_zone
0
-  
0
+
0
     def initialize(utc_time, time_zone, local_time = nil, period = nil)
0
       @utc, @time_zone, @time = utc_time, time_zone, local_time
0
       @period = @utc ? period : get_period_and_ensure_valid_local_time
0
     end
0
-  
0
+
0
     # Returns a Time or DateTime instance that represents the time in +time_zone+.
0
     def time
0
       @time ||= period.to_local(@utc)
0
@@ -51,7 +51,7 @@ module ActiveSupport
0
     alias_method :getgm, :utc
0
     alias_method :getutc, :utc
0
     alias_method :gmtime, :utc
0
-  
0
+
0
     # Returns the underlying TZInfo::TimezonePeriod.
0
     def period
0
       @period ||= time_zone.period_for_utc(@utc)
0
@@ -62,38 +62,38 @@ module ActiveSupport
0
       return self if time_zone == new_zone
0
       utc.in_time_zone(new_zone)
0
     end
0
-  
0
+
0
     # Returns a <tt>Time.local()</tt> instance of the simultaneous time in your system's <tt>ENV['TZ']</tt> zone
0
     def localtime
0
       utc.getlocal
0
     end
0
     alias_method :getlocal, :localtime
0
-  
0
+
0
     def dst?
0
       period.dst?
0
     end
0
     alias_method :isdst, :dst?
0
-  
0
+
0
     def utc?
0
       time_zone.name == 'UTC'
0
     end
0
     alias_method :gmt?, :utc?
0
-  
0
+
0
     def utc_offset
0
       period.utc_total_offset
0
     end
0
     alias_method :gmt_offset, :utc_offset
0
     alias_method :gmtoff, :utc_offset
0
-  
0
+
0
     def formatted_offset(colon = true, alternate_utc_string = nil)
0
       utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon)
0
     end
0
-  
0
+
0
     # Time uses +zone+ to display the time zone abbreviation, so we're duck-typing it.
0
     def zone
0
       period.zone_identifier.to_s
0
     end
0
-  
0
+
0
     def inspect
0
       "#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
0
     end
0
@@ -122,7 +122,7 @@ module ActiveSupport
0
         %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
0
       end
0
     end
0
-    
0
+
0
     def to_yaml(options = {})
0
       if options.kind_of?(YAML::Emitter)
0
         utc.to_yaml(options)
0
@@ -130,19 +130,19 @@ module ActiveSupport
0
         time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z'))
0
       end
0
     end
0
-    
0
+
0
     def httpdate
0
       utc.httpdate
0
     end
0
-  
0
+
0
     def rfc2822
0
       to_s(:rfc822)
0
     end
0
     alias_method :rfc822, :rfc2822
0
-  
0
+
0
     # <tt>:db</tt> format outputs time in UTC; all others output time in local.
0
     # Uses TimeWithZone's +strftime+, so <tt>%Z</tt> and <tt>%z</tt> work correctly.
0
-    def to_s(format = :default) 
0
+    def to_s(format = :default)
0
       return utc.to_s(format) if format == :db
0
       if formatter = ::Time::DATE_FORMATS[format]
0
         formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
0
@@ -150,27 +150,39 @@ module ActiveSupport
0
         "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
0
       end
0
     end
0
-    
0
+
0
     # Replaces <tt>%Z</tt> and <tt>%z</tt> directives with +zone+ and +formatted_offset+, respectively, before passing to
0
     # Time#strftime, so that zone information is correct
0
     def strftime(format)
0
       format = format.gsub('%Z', zone).gsub('%z', formatted_offset(false))
0
       time.strftime(format)
0
     end
0
-  
0
+
0
     # Use the time in UTC for comparisons.
0
     def <=>(other)
0
       utc <=> other
0
     end
0
-    
0
+
0
     def between?(min, max)
0
       utc.between?(min, max)
0
     end
0
-    
0
+
0
+    def past?
0
+      utc.past?
0
+    end
0
+
0
+    def today?
0
+      utc.today?
0
+    end
0
+
0
+    def future?
0
+      utc.future?
0
+    end
0
+
0
     def eql?(other)
0
       utc == other
0
     end
0
-    
0
+
0
     def +(other)
0
       # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time,
0
       # otherwise move forward from #utc, for accuracy when moving across DST boundaries
0
@@ -194,7 +206,7 @@ module ActiveSupport
0
         result.in_time_zone(time_zone)
0
       end
0
     end
0
-    
0
+
0
     def since(other)
0
       # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time,
0
       # otherwise move forward from #utc, for accuracy when moving across DST boundaries
0
@@ -204,7 +216,7 @@ module ActiveSupport
0
         utc.since(other).in_time_zone(time_zone)
0
       end
0
     end
0
-    
0
+
0
     def ago(other)
0
       since(-other)
0
     end
0
@@ -218,7 +230,7 @@ module ActiveSupport
0
         utc.advance(options).in_time_zone(time_zone)
0
       end
0
     end
0
-    
0
+
0
     %w(year mon month day mday hour min sec).each do |method_name|
0
       class_eval <<-EOV
0
         def #{method_name}
0
@@ -226,45 +238,45 @@ module ActiveSupport
0
         end
0
       EOV
0
     end
0
-    
0
+
0
     def usec
0
       time.respond_to?(:usec) ? time.usec : 0
0
     end
0
-    
0
+
0
     def to_a
0
       [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
0
     end
0
-    
0
+
0
     def to_f
0
       utc.to_f
0
-    end    
0
-    
0
+    end
0
+
0
     def to_i
0
       utc.to_i
0
     end
0
     alias_method :hash, :to_i
0
     alias_method :tv_sec, :to_i
0
-  
0
+
0
     # A TimeWithZone acts like a Time, so just return +self+.
0
     def to_time
0
       self
0
     end
0
-    
0
+
0
     def to_datetime
0
       utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
0
     end
0
-    
0
+
0
     # So that +self+ <tt>acts_like?(:time)</tt>.
0
     def acts_like_time?
0
       true
0
     end
0
-  
0
+
0
     # Say we're a Time to thwart type checking.
0
     def is_a?(klass)
0
       klass == ::Time || super
0
     end
0
     alias_method :kind_of?, :is_a?
0
-  
0
+
0
     # Neuter freeze because freezing can cause problems with lazy loading of attributes.
0
     def freeze
0
       self
0
@@ -273,7 +285,7 @@ module ActiveSupport
0
     def marshal_dump
0
       [utc, time_zone.name, time]
0
     end
0
-    
0
+
0
     def marshal_load(variables)
0
       initialize(variables[0].utc, ::Time.__send__(:get_zone, variables[1]), variables[2].utc)
0
     end
0
@@ -290,10 +302,10 @@ module ActiveSupport
0
       result = time.__send__(sym, *args, &block)
0
       result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
0
     end
0
-    
0
-    private      
0
+
0
+    private
0
       def get_period_and_ensure_valid_local_time
0
-        # we don't want a Time.local instance enforcing its own DST rules as well, 
0
+        # we don't want a Time.local instance enforcing its own DST rules as well,
0
         # so transfer time values to a utc constructor if necessary
0
         @time = transfer_time_values_to_utc_constructor(@time) unless @time.utc?
0
         begin
0
@@ -304,11 +316,11 @@ module ActiveSupport
0
           retry
0
         end
0
       end
0
-      
0
+
0
       def transfer_time_values_to_utc_constructor(time)
0
         ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:usec) ? time.usec : 0)
0
       end
0
-      
0
+
0
       def duration_of_variable_length?(obj)
0
         ActiveSupport::Duration === obj && obj.parts.flatten.detect {|p| [:years, :months, :days].include? p }
0
       end
...
210
211
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
214
215
...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
0
@@ -210,6 +210,21 @@ class DateExtCalculationsTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+  uses_mocha 'past?, today? and future?' do
0
+    def test_today_past_future
0
+      Date.stubs(:current).returns(Date.civil(2000, 1, 1))
0
+      t2 = Date.civil(2000, 1, 1)
0
+      t1, t3 = t2.yesterday, t2.tomorrow
0
+      t4, t5 = t2 - 1.second, t2 + 1.second
0
+
0
+      assert t1.past?
0
+      assert t2.today?
0
+      assert t3.future?
0
+      assert t4.past?
0
+      assert t5.today?
0
+    end
0
+  end
0
+
0
   uses_mocha 'TestDateCurrent' do
0
     def test_current_returns_date_today_when_zone_default_not_set
0
       with_env_tz 'US/Central' do
...
207
208
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
211
212
...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
0
@@ -207,6 +207,29 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
0
     assert_match(/^2080-02-28T15:15:10-06:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10, -0.25).xmlschema)
0
   end
0
 
0
+  uses_mocha 'past?, today? and future?' do
0
+    def test_past_today_future
0
+      Date.stubs(:current).returns(Date.civil(2000, 1, 1))
0
+      DateTime.stubs(:current).returns(DateTime.civil(2000, 1, 1, 1, 0, 1))
0
+      t1, t2 = DateTime.civil(2000, 1, 1, 1, 0, 0), DateTime.civil(2000, 1, 1, 1, 0, 2)
0
+
0
+      assert t1.past?
0
+      assert t2.future?
0
+      assert t1.today?
0
+      assert t2.today?
0
+    end
0
+  end
0
+
0
+  def test_current_without_time_zone
0
+    assert DateTime.current.is_a?(DateTime)
0
+  end
0
+
0
+  def test_current_with_time_zone
0
+    with_env_tz 'US/Eastern' do
0
+      assert DateTime.current.is_a?(DateTime)
0
+    end
0
+  end
0
+
0
   def test_acts_like_time
0
     assert DateTime.new.acts_like_time?
0
   end
...
563
564
565
 
 
 
 
 
 
 
 
 
 
 
 
 
566
567
568
...
640
641
642
643
644
 
 
645
646
647
648
649
650
651
652
 
 
653
654
655
656
657
658
659
660
 
 
661
662
663
...
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
...
653
654
655
 
 
656
657
658
659
660
661
662
663
 
 
664
665
666
667
668
669
670
671
 
 
672
673
674
675
676
0
@@ -563,6 +563,19 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
0
     assert_nothing_raised { Time.now.xmlschema }
0
   end
0
 
0
+  uses_mocha 'past?, today? and future?' do
0
+    def test_past_today_future
0
+      Date.stubs(:current).returns(Date.civil(2000, 1, 1))
0
+      Time.stubs(:current).returns(Time.local(2000, 1, 1, 1, 0, 1))
0
+      t1, t2 = Time.local(2000, 1, 1, 1, 0, 0), Time.local(2000, 1, 1, 1, 0, 2)
0
+
0
+      assert t1.past?
0
+      assert t2.future?
0
+      assert t1.today?
0
+      assert t2.today?
0
+    end
0
+  end
0
+
0
   def test_acts_like_time
0
     assert Time.new.acts_like_time?
0
   end
0
@@ -640,24 +653,24 @@ class TimeExtMarshalingTest < Test::Unit::TestCase
0
     assert_equal t, unmarshaled
0
     assert_equal t.zone, unmarshaled.zone
0
   end
0
-  
0
-  def test_marshaling_with_local_instance  
0
+
0
+  def test_marshaling_with_local_instance
0
     t = Time.local(2000)
0
     marshaled = Marshal.dump t
0
     unmarshaled = Marshal.load marshaled
0
     assert_equal t, unmarshaled
0
     assert_equal t.zone, unmarshaled.zone
0
   end
0
-    
0
-  def test_marshaling_with_frozen_utc_instance  
0
+
0
+  def test_marshaling_with_frozen_utc_instance
0
     t = Time.utc(2000).freeze
0
     marshaled = Marshal.dump t
0
     unmarshaled = Marshal.load marshaled
0
     assert_equal t, unmarshaled
0
     assert_equal t.zone, unmarshaled.zone
0
   end
0
-  
0
-  def test_marshaling_with_frozen_local_instance  
0
+
0
+  def test_marshaling_with_frozen_local_instance
0
     t = Time.local(2000).freeze
0
     marshaled = Marshal.dump t
0
     unmarshaled = Marshal.load marshaled
...
152
153
154
 
 
 
 
 
 
 
 
 
 
 
 
 
155
156
157
...
538
539
540
541
 
542
543
544
...
548
549
550
551
 
552
553
554
...
565
566
567
568
 
569
570
571
...
582
583
584
585
 
586
587
588
...
593
594
595
596
 
597
598
599
...
603
604
605
606
 
607
608
609
...
620
621
622
623
 
624
625
626
...
669
670
671
672
 
673
674
675
...
679
680
681
682
 
683
684
685
...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
...
551
552
553
 
554
555
556
557
...
561
562
563
 
564
565
566
567
...
578
579
580
 
581
582
583
584
...
595
596
597
 
598
599
600
601
...
606
607
608
 
609
610
611
612
...
616
617
618
 
619
620
621
622
...
633
634
635
 
636
637
638
639
...
682
683
684
 
685
686
687
688
...
692
693
694
 
695
696
697
698
0
@@ -152,6 +152,19 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2))
0
   end
0
 
0
+  uses_mocha 'past?, today? and future?' do
0
+    def test_past_today_future
0
+      Time.stubs(:current).returns(@twz.utc)
0
+      Date.stubs(:current).returns(@twz.utc.to_date)
0
+      t1, t2 = @twz - 1.second, @twz + 1.second
0
+
0
+      assert t1.past?
0
+      assert t2.future?
0
+      assert !t1.today?
0
+      assert t2.today?
0
+    end
0
+  end
0
+
0
   def test_eql?
0
     assert @twz.eql?(Time.utc(2000))
0
     assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
0
@@ -538,7 +551,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", twz.since(1.days + 1.second).inspect
0
     assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", (twz + 1.days + 1.second).inspect
0
   end
0
-  
0
+
0
   def test_advance_1_day_across_spring_dst_transition_backwards
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,10,30))
0
     # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
0
@@ -548,7 +561,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1.days).inspect
0
     assert_equal "Sat, 01 Apr 2006 10:30:01 EST -05:00", twz.ago(1.days - 1.second).inspect
0
   end
0
-    
0
+
0
   def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
0
     # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
0
@@ -565,7 +578,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(24.hours).inspect
0
     assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:hours => 24).inspect
0
   end
0
-  
0
+
0
   def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition_backwards
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,11,30))
0
     # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
0
@@ -582,7 +595,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(24.hours).inspect
0
     assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:hours => -24).inspect
0
   end
0
-  
0
+
0
   def test_advance_1_day_across_fall_dst_transition
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
0
     # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
0
@@ -593,7 +606,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", twz.since(1.days + 1.second).inspect
0
     assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", (twz + 1.days + 1.second).inspect
0
   end
0
-  
0
+
0
   def test_advance_1_day_across_fall_dst_transition_backwards
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,10,30))
0
     # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
0
@@ -603,7 +616,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.days).inspect
0
     assert_equal "Sat, 28 Oct 2006 10:30:01 EDT -04:00", twz.ago(1.days - 1.second).inspect
0
   end
0
-  
0
+
0
   def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
0
     # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
0
@@ -620,7 +633,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(24.hours).inspect
0
     assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:hours => 24).inspect
0
   end
0
-  
0
+
0
   def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition_backwards
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,9,30))
0
     # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
0
@@ -669,7 +682,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1.month).inspect
0
     assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.month).inspect
0
   end
0
-  
0
+
0
   def test_advance_1_year
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,2,15,10,30))
0
     assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", twz.advance(:years => 1).inspect
0
@@ -679,7 +692,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
0
     assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", twz.years_ago(1).inspect
0
     assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", (twz - 1.year).inspect
0
   end
0
-  
0
+
0
   def test_advance_1_year_during_dst
0
     twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,7,15,10,30))
0
     assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", twz.advance(:years => 1).inspect

Comments