time_ext_test.rb 53.6 KB
Newer Older
1
# frozen_string_literal: true
2

3 4 5 6
require "abstract_unit"
require "active_support/time"
require "core_ext/date_and_time_behavior"
require "time_zone_test_helpers"
7

8
class TimeExtCalculationsTest < ActiveSupport::TestCase
9 10
  def date_time_init(year, month, day, hour, minute, second, usec = 0)
    Time.local(year, month, day, hour, minute, second, usec)
11 12 13
  end

  include DateAndTimeBehavior
14
  include TimeZoneTestHelpers
15

16
  def test_seconds_since_midnight
17 18 19 20 21
    assert_equal 1, Time.local(2005, 1, 1, 0, 0, 1).seconds_since_midnight
    assert_equal 60, Time.local(2005, 1, 1, 0, 1, 0).seconds_since_midnight
    assert_equal 3660, Time.local(2005, 1, 1, 1, 1, 0).seconds_since_midnight
    assert_equal 86399, Time.local(2005, 1, 1, 23, 59, 59).seconds_since_midnight
    assert_equal 60.00001, Time.local(2005, 1, 1, 0, 1, 0, 10).seconds_since_midnight
22 23
  end

24
  def test_seconds_since_midnight_at_daylight_savings_time_start
25
    with_env_tz "US/Eastern" do
26
      # dt: US: 2005 April 3rd 2:00am ST => April 3rd 3:00am DT
27 28
      assert_equal 2 * 3600 - 1, Time.local(2005, 4, 3, 1, 59, 59).seconds_since_midnight, "just before DST start"
      assert_equal 2 * 3600 + 1, Time.local(2005, 4, 3, 3, 0, 1).seconds_since_midnight, "just after DST start"
29 30
    end

31
    with_env_tz "NZ" do
32
      # dt: New Zealand: 2006 October 1st 2:00am ST => October 1st 3:00am DT
33 34
      assert_equal 2 * 3600 - 1, Time.local(2006, 10, 1, 1, 59, 59).seconds_since_midnight, "just before DST start"
      assert_equal 2 * 3600 + 1, Time.local(2006, 10, 1, 3, 0, 1).seconds_since_midnight, "just after DST start"
35
    end
36 37 38
  end

  def test_seconds_since_midnight_at_daylight_savings_time_end
39
    with_env_tz "US/Eastern" do
40 41
      # st: US: 2005 October 30th 2:00am DT => October 30th 1:00am ST
      # avoid setting a time between 1:00 and 2:00 since that requires specifying whether DST is active
42 43
      assert_equal 1 * 3600 - 1, Time.local(2005, 10, 30, 0, 59, 59).seconds_since_midnight, "just before DST end"
      assert_equal 3 * 3600 + 1, Time.local(2005, 10, 30, 2, 0, 1).seconds_since_midnight, "just after DST end"
44

45 46
      # now set a time between 1:00 and 2:00 by specifying whether DST is active
      # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
47 48
      assert_equal 1 * 3600 + 30 * 60, Time.local(0, 30, 1, 30, 10, 2005, 0, 0, true, ENV["TZ"]).seconds_since_midnight, "before DST end"
      assert_equal 2 * 3600 + 30 * 60, Time.local(0, 30, 1, 30, 10, 2005, 0, 0, false, ENV["TZ"]).seconds_since_midnight, "after DST end"
49 50
    end

51
    with_env_tz "NZ" do
52 53
      # st: New Zealand: 2006 March 19th 3:00am DT => March 19th 2:00am ST
      # avoid setting a time between 2:00 and 3:00 since that requires specifying whether DST is active
54 55
      assert_equal 2 * 3600 - 1, Time.local(2006, 3, 19, 1, 59, 59).seconds_since_midnight, "just before DST end"
      assert_equal 4 * 3600 + 1, Time.local(2006, 3, 19, 3, 0, 1).seconds_since_midnight, "just after DST end"
56 57 58

      # now set a time between 2:00 and 3:00 by specifying whether DST is active
      # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
59 60
      assert_equal 2 * 3600 + 30 * 60, Time.local(0, 30, 2, 19, 3, 2006, 0, 0, true, ENV["TZ"]).seconds_since_midnight, "before DST end"
      assert_equal 3 * 3600 + 30 * 60, Time.local(0, 30, 2, 19, 3, 2006, 0, 0, false, ENV["TZ"]).seconds_since_midnight, "after DST end"
61
    end
62 63
  end

64
  def test_seconds_until_end_of_day
65 66 67 68 69
    assert_equal 0, Time.local(2005, 1, 1, 23, 59, 59).seconds_until_end_of_day
    assert_equal 1, Time.local(2005, 1, 1, 23, 59, 58).seconds_until_end_of_day
    assert_equal 60, Time.local(2005, 1, 1, 23, 58, 59).seconds_until_end_of_day
    assert_equal 3660, Time.local(2005, 1, 1, 22, 58, 59).seconds_until_end_of_day
    assert_equal 86399, Time.local(2005, 1, 1, 0, 0, 0).seconds_until_end_of_day
70 71 72
  end

  def test_seconds_until_end_of_day_at_daylight_savings_time_start
73
    with_env_tz "US/Eastern" do
74
      # dt: US: 2005 April 3rd 2:00am ST => April 3rd 3:00am DT
75 76
      assert_equal 21 * 3600, Time.local(2005, 4, 3, 1, 59, 59).seconds_until_end_of_day, "just before DST start"
      assert_equal 21 * 3600 - 2, Time.local(2005, 4, 3, 3, 0, 1).seconds_until_end_of_day, "just after DST start"
77 78
    end

79
    with_env_tz "NZ" do
80
      # dt: New Zealand: 2006 October 1st 2:00am ST => October 1st 3:00am DT
81 82
      assert_equal 21 * 3600, Time.local(2006, 10, 1, 1, 59, 59).seconds_until_end_of_day, "just before DST start"
      assert_equal 21 * 3600 - 2, Time.local(2006, 10, 1, 3, 0, 1).seconds_until_end_of_day, "just after DST start"
83 84 85 86
    end
  end

  def test_seconds_until_end_of_day_at_daylight_savings_time_end
87
    with_env_tz "US/Eastern" do
88 89
      # st: US: 2005 October 30th 2:00am DT => October 30th 1:00am ST
      # avoid setting a time between 1:00 and 2:00 since that requires specifying whether DST is active
90 91
      assert_equal 24 * 3600, Time.local(2005, 10, 30, 0, 59, 59).seconds_until_end_of_day, "just before DST end"
      assert_equal 22 * 3600 - 2, Time.local(2005, 10, 30, 2, 0, 1).seconds_until_end_of_day, "just after DST end"
92 93 94

      # now set a time between 1:00 and 2:00 by specifying whether DST is active
      # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
95 96
      assert_equal 24 * 3600 - 30 * 60 - 1, Time.local(0, 30, 1, 30, 10, 2005, 0, 0, true, ENV["TZ"]).seconds_until_end_of_day, "before DST end"
      assert_equal 23 * 3600 - 30 * 60 - 1, Time.local(0, 30, 1, 30, 10, 2005, 0, 0, false, ENV["TZ"]).seconds_until_end_of_day, "after DST end"
97 98
    end

99
    with_env_tz "NZ" do
100 101
      # st: New Zealand: 2006 March 19th 3:00am DT => March 19th 2:00am ST
      # avoid setting a time between 2:00 and 3:00 since that requires specifying whether DST is active
102 103
      assert_equal 23 * 3600, Time.local(2006, 3, 19, 1, 59, 59).seconds_until_end_of_day, "just before DST end"
      assert_equal 21 * 3600 - 2, Time.local(2006, 3, 19, 3, 0, 1).seconds_until_end_of_day, "just after DST end"
104 105 106

      # now set a time between 2:00 and 3:00 by specifying whether DST is active
      # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
107 108
      assert_equal 23 * 3600 - 30 * 60 - 1, Time.local(0, 30, 2, 19, 3, 2006, 0, 0, true, ENV["TZ"]).seconds_until_end_of_day, "before DST end"
      assert_equal 22 * 3600 - 30 * 60 - 1, Time.local(0, 30, 2, 19, 3, 2006, 0, 0, false, ENV["TZ"]).seconds_until_end_of_day, "after DST end"
109 110 111
    end
  end

112
  def test_sec_fraction
113 114
    time = Time.utc(2016, 4, 23, 0, 0, Rational(1, 10000000000))
    assert_equal Rational(1, 10000000000), time.sec_fraction
115 116 117 118

    time = Time.utc(2016, 4, 23, 0, 0, 0.0000000001)
    assert_equal 0.0000000001.to_r, time.sec_fraction

119 120
    time = Time.utc(2016, 4, 23, 0, 0, 0, Rational(1, 10000))
    assert_equal Rational(1, 10000000000), time.sec_fraction
121 122 123 124 125

    time = Time.utc(2016, 4, 23, 0, 0, 0, 0.0001)
    assert_equal 0.0001.to_r / 1000000, time.sec_fraction
  end

126
  def test_beginning_of_day
127
    assert_equal Time.local(2005, 2, 4, 0, 0, 0), Time.local(2005, 2, 4, 10, 10, 10).beginning_of_day
128
    with_env_tz "US/Eastern" do
129 130
      assert_equal Time.local(2006, 4, 2, 0, 0, 0), Time.local(2006, 4, 2, 10, 10, 10).beginning_of_day, "start DST"
      assert_equal Time.local(2006, 10, 29, 0, 0, 0), Time.local(2006, 10, 29, 10, 10, 10).beginning_of_day, "ends DST"
131
    end
132
    with_env_tz "NZ" do
133 134
      assert_equal Time.local(2006, 3, 19, 0, 0, 0), Time.local(2006, 3, 19, 10, 10, 10).beginning_of_day, "ends DST"
      assert_equal Time.local(2006, 10, 1, 0, 0, 0), Time.local(2006, 10, 1, 10, 10, 10).beginning_of_day, "start DST"
135
    end
136
  end
137

A
Anatoli Makarevich 已提交
138
  def test_middle_of_day
139
    assert_equal Time.local(2005, 2, 4, 12, 0, 0), Time.local(2005, 2, 4, 10, 10, 10).middle_of_day
140
    with_env_tz "US/Eastern" do
141 142
      assert_equal Time.local(2006, 4, 2, 12, 0, 0), Time.local(2006, 4, 2, 10, 10, 10).middle_of_day, "start DST"
      assert_equal Time.local(2006, 10, 29, 12, 0, 0), Time.local(2006, 10, 29, 10, 10, 10).middle_of_day, "ends DST"
A
Anatoli Makarevich 已提交
143
    end
144
    with_env_tz "NZ" do
145 146
      assert_equal Time.local(2006, 3, 19, 12, 0, 0), Time.local(2006, 3, 19, 10, 10, 10).middle_of_day, "ends DST"
      assert_equal Time.local(2006, 10, 1, 12, 0, 0), Time.local(2006, 10, 1, 10, 10, 10).middle_of_day, "start DST"
A
Anatoli Makarevich 已提交
147 148 149
    end
  end

150
  def test_beginning_of_hour
151
    assert_equal Time.local(2005, 2, 4, 19, 0, 0), Time.local(2005, 2, 4, 19, 30, 10).beginning_of_hour
152 153
  end

154
  def test_beginning_of_minute
155
    assert_equal Time.local(2005, 2, 4, 19, 30, 0), Time.local(2005, 2, 4, 19, 30, 10).beginning_of_minute
156 157
  end

158
  def test_end_of_day
159
    assert_equal Time.local(2007, 8, 12, 23, 59, 59, Rational(999999999, 1000)), Time.local(2007, 8, 12, 10, 10, 10).end_of_day
160
    with_env_tz "US/Eastern" do
161 162
      assert_equal Time.local(2007, 4, 2, 23, 59, 59, Rational(999999999, 1000)), Time.local(2007, 4, 2, 10, 10, 10).end_of_day, "start DST"
      assert_equal Time.local(2007, 10, 29, 23, 59, 59, Rational(999999999, 1000)), Time.local(2007, 10, 29, 10, 10, 10).end_of_day, "ends DST"
163
    end
164
    with_env_tz "NZ" do
165 166
      assert_equal Time.local(2006, 3, 19, 23, 59, 59, Rational(999999999, 1000)), Time.local(2006, 3, 19, 10, 10, 10).end_of_day, "ends DST"
      assert_equal Time.local(2006, 10, 1, 23, 59, 59, Rational(999999999, 1000)), Time.local(2006, 10, 1, 10, 10, 10).end_of_day, "start DST"
167
    end
168 169
    with_env_tz "Asia/Yekaterinburg" do
      assert_equal Time.local(2015, 2, 8, 23, 59, 59, Rational(999999999, 1000)), Time.new(2015, 2, 8, 8, 0, 0, "+05:00").end_of_day
170
    end
171 172
  end

173
  def test_end_of_hour
174
    assert_equal Time.local(2005, 2, 4, 19, 59, 59, Rational(999999999, 1000)), Time.local(2005, 2, 4, 19, 30, 10).end_of_hour
175 176
  end

177
  def test_end_of_minute
178
    assert_equal Time.local(2005, 2, 4, 19, 30, 59, Rational(999999999, 1000)), Time.local(2005, 2, 4, 19, 30, 10).end_of_minute
179 180
  end

181
  def test_ago
182 183 184 185
    assert_equal Time.local(2005, 2, 22, 10, 10, 9),  Time.local(2005, 2, 22, 10, 10, 10).ago(1)
    assert_equal Time.local(2005, 2, 22, 9, 10, 10),  Time.local(2005, 2, 22, 10, 10, 10).ago(3600)
    assert_equal Time.local(2005, 2, 20, 10, 10, 10), Time.local(2005, 2, 22, 10, 10, 10).ago(86400 * 2)
    assert_equal Time.local(2005, 2, 20, 9, 9, 45),   Time.local(2005, 2, 22, 10, 10, 10).ago(86400 * 2 + 3600 + 25)
186 187
  end

188
  def test_daylight_savings_time_crossings_backward_start
189
    with_env_tz "US/Eastern" do
190
      # dt: US: 2005 April 3rd 4:18am
191 192 193
      assert_equal Time.local(2005, 4, 2, 3, 18, 0), Time.local(2005, 4, 3, 4, 18, 0).ago(24.hours), "dt-24.hours=>st"
      assert_equal Time.local(2005, 4, 2, 3, 18, 0), Time.local(2005, 4, 3, 4, 18, 0).ago(86400), "dt-86400=>st"
      assert_equal Time.local(2005, 4, 2, 3, 18, 0), Time.local(2005, 4, 3, 4, 18, 0).ago(86400.seconds), "dt-86400.seconds=>st"
194

195 196 197
      assert_equal Time.local(2005, 4, 1, 4, 18, 0), Time.local(2005, 4, 2, 4, 18, 0).ago(24.hours), "st-24.hours=>st"
      assert_equal Time.local(2005, 4, 1, 4, 18, 0), Time.local(2005, 4, 2, 4, 18, 0).ago(86400), "st-86400=>st"
      assert_equal Time.local(2005, 4, 1, 4, 18, 0), Time.local(2005, 4, 2, 4, 18, 0).ago(86400.seconds), "st-86400.seconds=>st"
198
    end
199
    with_env_tz "NZ" do
200
      # dt: New Zealand: 2006 October 1st 4:18am
201 202 203
      assert_equal Time.local(2006, 9, 30, 3, 18, 0), Time.local(2006, 10, 1, 4, 18, 0).ago(24.hours), "dt-24.hours=>st"
      assert_equal Time.local(2006, 9, 30, 3, 18, 0), Time.local(2006, 10, 1, 4, 18, 0).ago(86400), "dt-86400=>st"
      assert_equal Time.local(2006, 9, 30, 3, 18, 0), Time.local(2006, 10, 1, 4, 18, 0).ago(86400.seconds), "dt-86400.seconds=>st"
204

205 206 207
      assert_equal Time.local(2006, 9, 29, 4, 18, 0), Time.local(2006, 9, 30, 4, 18, 0).ago(24.hours), "st-24.hours=>st"
      assert_equal Time.local(2006, 9, 29, 4, 18, 0), Time.local(2006, 9, 30, 4, 18, 0).ago(86400), "st-86400=>st"
      assert_equal Time.local(2006, 9, 29, 4, 18, 0), Time.local(2006, 9, 30, 4, 18, 0).ago(86400.seconds), "st-86400.seconds=>st"
208
    end
209 210 211
  end

  def test_daylight_savings_time_crossings_backward_end
212
    with_env_tz "US/Eastern" do
213
      # st: US: 2005 October 30th 4:03am
214 215 216
      assert_equal Time.local(2005, 10, 29, 5, 3), Time.local(2005, 10, 30, 4, 3, 0).ago(24.hours), "st-24.hours=>dt"
      assert_equal Time.local(2005, 10, 29, 5, 3), Time.local(2005, 10, 30, 4, 3, 0).ago(86400), "st-86400=>dt"
      assert_equal Time.local(2005, 10, 29, 5, 3), Time.local(2005, 10, 30, 4, 3, 0).ago(86400.seconds), "st-86400.seconds=>dt"
217

218 219 220
      assert_equal Time.local(2005, 10, 28, 4, 3), Time.local(2005, 10, 29, 4, 3, 0).ago(24.hours), "dt-24.hours=>dt"
      assert_equal Time.local(2005, 10, 28, 4, 3), Time.local(2005, 10, 29, 4, 3, 0).ago(86400), "dt-86400=>dt"
      assert_equal Time.local(2005, 10, 28, 4, 3), Time.local(2005, 10, 29, 4, 3, 0).ago(86400.seconds), "dt-86400.seconds=>dt"
221
    end
222
    with_env_tz "NZ" do
223
      # st: New Zealand: 2006 March 19th 4:03am
224 225 226
      assert_equal Time.local(2006, 3, 18, 5, 3), Time.local(2006, 3, 19, 4, 3, 0).ago(24.hours), "st-24.hours=>dt"
      assert_equal Time.local(2006, 3, 18, 5, 3), Time.local(2006, 3, 19, 4, 3, 0).ago(86400), "st-86400=>dt"
      assert_equal Time.local(2006, 3, 18, 5, 3), Time.local(2006, 3, 19, 4, 3, 0).ago(86400.seconds), "st-86400.seconds=>dt"
227

228 229 230
      assert_equal Time.local(2006, 3, 17, 4, 3), Time.local(2006, 3, 18, 4, 3, 0).ago(24.hours), "dt-24.hours=>dt"
      assert_equal Time.local(2006, 3, 17, 4, 3), Time.local(2006, 3, 18, 4, 3, 0).ago(86400), "dt-86400=>dt"
      assert_equal Time.local(2006, 3, 17, 4, 3), Time.local(2006, 3, 18, 4, 3, 0).ago(86400.seconds), "dt-86400.seconds=>dt"
231
    end
232 233
  end

234
  def test_daylight_savings_time_crossings_backward_start_1day
235
    with_env_tz "US/Eastern" do
236
      # dt: US: 2005 April 3rd 4:18am
237 238
      assert_equal Time.local(2005, 4, 2, 4, 18, 0), Time.local(2005, 4, 3, 4, 18, 0).ago(1.day), "dt-1.day=>st"
      assert_equal Time.local(2005, 4, 1, 4, 18, 0), Time.local(2005, 4, 2, 4, 18, 0).ago(1.day), "st-1.day=>st"
239
    end
240
    with_env_tz "NZ" do
241
      # dt: New Zealand: 2006 October 1st 4:18am
242 243
      assert_equal Time.local(2006, 9, 30, 4, 18, 0), Time.local(2006, 10, 1, 4, 18, 0).ago(1.day), "dt-1.day=>st"
      assert_equal Time.local(2006, 9, 29, 4, 18, 0), Time.local(2006, 9, 30, 4, 18, 0).ago(1.day), "st-1.day=>st"
244 245 246 247
    end
  end

  def test_daylight_savings_time_crossings_backward_end_1day
248
    with_env_tz "US/Eastern" do
249
      # st: US: 2005 October 30th 4:03am
250 251
      assert_equal Time.local(2005, 10, 29, 4, 3), Time.local(2005, 10, 30, 4, 3, 0).ago(1.day), "st-1.day=>dt"
      assert_equal Time.local(2005, 10, 28, 4, 3), Time.local(2005, 10, 29, 4, 3, 0).ago(1.day), "dt-1.day=>dt"
252
    end
253
    with_env_tz "NZ" do
254
      # st: New Zealand: 2006 March 19th 4:03am
255 256
      assert_equal Time.local(2006, 3, 18, 4, 3), Time.local(2006, 3, 19, 4, 3, 0).ago(1.day), "st-1.day=>dt"
      assert_equal Time.local(2006, 3, 17, 4, 3), Time.local(2006, 3, 18, 4, 3, 0).ago(1.day), "dt-1.day=>dt"
257 258
    end
  end
259

260
  def test_since
261 262 263 264
    assert_equal Time.local(2005, 2, 22, 10, 10, 11), Time.local(2005, 2, 22, 10, 10, 10).since(1)
    assert_equal Time.local(2005, 2, 22, 11, 10, 10), Time.local(2005, 2, 22, 10, 10, 10).since(3600)
    assert_equal Time.local(2005, 2, 24, 10, 10, 10), Time.local(2005, 2, 22, 10, 10, 10).since(86400 * 2)
    assert_equal Time.local(2005, 2, 24, 11, 10, 35), Time.local(2005, 2, 22, 10, 10, 10).since(86400 * 2 + 3600 + 25)
265
    # when out of range of Time, returns a DateTime
266
    assert_equal DateTime.civil(2038, 1, 20, 11, 59, 59), Time.utc(2038, 1, 18, 11, 59, 59).since(86400 * 2)
267 268
  end

269
  def test_daylight_savings_time_crossings_forward_start
270
    with_env_tz "US/Eastern" do
271
      # st: US: 2005 April 2nd 7:27pm
272 273 274
      assert_equal Time.local(2005, 4, 3, 20, 27, 0), Time.local(2005, 4, 2, 19, 27, 0).since(24.hours), "st+24.hours=>dt"
      assert_equal Time.local(2005, 4, 3, 20, 27, 0), Time.local(2005, 4, 2, 19, 27, 0).since(86400), "st+86400=>dt"
      assert_equal Time.local(2005, 4, 3, 20, 27, 0), Time.local(2005, 4, 2, 19, 27, 0).since(86400.seconds), "st+86400.seconds=>dt"
275

276 277 278
      assert_equal Time.local(2005, 4, 4, 19, 27, 0), Time.local(2005, 4, 3, 19, 27, 0).since(24.hours), "dt+24.hours=>dt"
      assert_equal Time.local(2005, 4, 4, 19, 27, 0), Time.local(2005, 4, 3, 19, 27, 0).since(86400), "dt+86400=>dt"
      assert_equal Time.local(2005, 4, 4, 19, 27, 0), Time.local(2005, 4, 3, 19, 27, 0).since(86400.seconds), "dt+86400.seconds=>dt"
279
    end
280
    with_env_tz "NZ" do
281
      # st: New Zealand: 2006 September 30th 7:27pm
282 283 284
      assert_equal Time.local(2006, 10, 1, 20, 27, 0), Time.local(2006, 9, 30, 19, 27, 0).since(24.hours), "st+24.hours=>dt"
      assert_equal Time.local(2006, 10, 1, 20, 27, 0), Time.local(2006, 9, 30, 19, 27, 0).since(86400), "st+86400=>dt"
      assert_equal Time.local(2006, 10, 1, 20, 27, 0), Time.local(2006, 9, 30, 19, 27, 0).since(86400.seconds), "st+86400.seconds=>dt"
285

286 287 288
      assert_equal Time.local(2006, 10, 2, 19, 27, 0), Time.local(2006, 10, 1, 19, 27, 0).since(24.hours), "dt+24.hours=>dt"
      assert_equal Time.local(2006, 10, 2, 19, 27, 0), Time.local(2006, 10, 1, 19, 27, 0).since(86400), "dt+86400=>dt"
      assert_equal Time.local(2006, 10, 2, 19, 27, 0), Time.local(2006, 10, 1, 19, 27, 0).since(86400.seconds), "dt+86400.seconds=>dt"
289
    end
290 291
  end

292
  def test_daylight_savings_time_crossings_forward_start_1day
293
    with_env_tz "US/Eastern" do
294
      # st: US: 2005 April 2nd 7:27pm
295 296
      assert_equal Time.local(2005, 4, 3, 19, 27, 0), Time.local(2005, 4, 2, 19, 27, 0).since(1.day), "st+1.day=>dt"
      assert_equal Time.local(2005, 4, 4, 19, 27, 0), Time.local(2005, 4, 3, 19, 27, 0).since(1.day), "dt+1.day=>dt"
297
    end
298
    with_env_tz "NZ" do
299
      # st: New Zealand: 2006 September 30th 7:27pm
300 301
      assert_equal Time.local(2006, 10, 1, 19, 27, 0), Time.local(2006, 9, 30, 19, 27, 0).since(1.day), "st+1.day=>dt"
      assert_equal Time.local(2006, 10, 2, 19, 27, 0), Time.local(2006, 10, 1, 19, 27, 0).since(1.day), "dt+1.day=>dt"
302 303 304
    end
  end

305
  def test_daylight_savings_time_crossings_forward_start_tomorrow
306
    with_env_tz "US/Eastern" do
307
      # st: US: 2005 April 2nd 7:27pm
308 309
      assert_equal Time.local(2005, 4, 3, 19, 27, 0), Time.local(2005, 4, 2, 19, 27, 0).tomorrow, "st+1.day=>dt"
      assert_equal Time.local(2005, 4, 4, 19, 27, 0), Time.local(2005, 4, 3, 19, 27, 0).tomorrow, "dt+1.day=>dt"
310
    end
311
    with_env_tz "NZ" do
312
      # st: New Zealand: 2006 September 30th 7:27pm
313 314
      assert_equal Time.local(2006, 10, 1, 19, 27, 0), Time.local(2006, 9, 30, 19, 27, 0).tomorrow, "st+1.day=>dt"
      assert_equal Time.local(2006, 10, 2, 19, 27, 0), Time.local(2006, 10, 1, 19, 27, 0).tomorrow, "dt+1.day=>dt"
315 316 317
    end
  end

318
  def test_daylight_savings_time_crossings_backward_start_yesterday
319
    with_env_tz "US/Eastern" do
320
      # st: US: 2005 April 2nd 7:27pm
321 322
      assert_equal Time.local(2005, 4, 2, 19, 27, 0), Time.local(2005, 4, 3, 19, 27, 0).yesterday, "dt-1.day=>st"
      assert_equal Time.local(2005, 4, 3, 19, 27, 0), Time.local(2005, 4, 4, 19, 27, 0).yesterday, "dt-1.day=>dt"
323
    end
324
    with_env_tz "NZ" do
325
      # st: New Zealand: 2006 September 30th 7:27pm
326 327
      assert_equal Time.local(2006, 9, 30, 19, 27, 0), Time.local(2006, 10, 1, 19, 27, 0).yesterday, "dt-1.day=>st"
      assert_equal Time.local(2006, 10, 1, 19, 27, 0), Time.local(2006, 10, 2, 19, 27, 0).yesterday, "dt-1.day=>dt"
328 329 330
    end
  end

331
  def test_daylight_savings_time_crossings_forward_end
332
    with_env_tz "US/Eastern" do
333
      # dt: US: 2005 October 30th 12:45am
334 335 336
      assert_equal Time.local(2005, 10, 30, 23, 45, 0), Time.local(2005, 10, 30, 0, 45, 0).since(24.hours), "dt+24.hours=>st"
      assert_equal Time.local(2005, 10, 30, 23, 45, 0), Time.local(2005, 10, 30, 0, 45, 0).since(86400), "dt+86400=>st"
      assert_equal Time.local(2005, 10, 30, 23, 45, 0), Time.local(2005, 10, 30, 0, 45, 0).since(86400.seconds), "dt+86400.seconds=>st"
337

338 339 340
      assert_equal Time.local(2005, 11, 1, 0, 45, 0), Time.local(2005, 10, 31, 0, 45, 0).since(24.hours), "st+24.hours=>st"
      assert_equal Time.local(2005, 11, 1, 0, 45, 0), Time.local(2005, 10, 31, 0, 45, 0).since(86400), "st+86400=>st"
      assert_equal Time.local(2005, 11, 1, 0, 45, 0), Time.local(2005, 10, 31, 0, 45, 0).since(86400.seconds), "st+86400.seconds=>st"
341
    end
342
    with_env_tz "NZ" do
343
      # dt: New Zealand: 2006 March 19th 1:45am
344 345 346
      assert_equal Time.local(2006, 3, 20, 0, 45, 0), Time.local(2006, 3, 19, 1, 45, 0).since(24.hours), "dt+24.hours=>st"
      assert_equal Time.local(2006, 3, 20, 0, 45, 0), Time.local(2006, 3, 19, 1, 45, 0).since(86400), "dt+86400=>st"
      assert_equal Time.local(2006, 3, 20, 0, 45, 0), Time.local(2006, 3, 19, 1, 45, 0).since(86400.seconds), "dt+86400.seconds=>st"
347

348 349 350
      assert_equal Time.local(2006, 3, 21, 1, 45, 0), Time.local(2006, 3, 20, 1, 45, 0).since(24.hours), "st+24.hours=>st"
      assert_equal Time.local(2006, 3, 21, 1, 45, 0), Time.local(2006, 3, 20, 1, 45, 0).since(86400), "st+86400=>st"
      assert_equal Time.local(2006, 3, 21, 1, 45, 0), Time.local(2006, 3, 20, 1, 45, 0).since(86400.seconds), "st+86400.seconds=>st"
351
    end
352 353
  end

354
  def test_daylight_savings_time_crossings_forward_end_1day
355
    with_env_tz "US/Eastern" do
356
      # dt: US: 2005 October 30th 12:45am
357 358
      assert_equal Time.local(2005, 10, 31, 0, 45, 0), Time.local(2005, 10, 30, 0, 45, 0).since(1.day), "dt+1.day=>st"
      assert_equal Time.local(2005, 11, 1, 0, 45, 0), Time.local(2005, 10, 31, 0, 45, 0).since(1.day), "st+1.day=>st"
359
    end
360
    with_env_tz "NZ" do
361
      # dt: New Zealand: 2006 March 19th 1:45am
362 363
      assert_equal Time.local(2006, 3, 20, 1, 45, 0), Time.local(2006, 3, 19, 1, 45, 0).since(1.day), "dt+1.day=>st"
      assert_equal Time.local(2006, 3, 21, 1, 45, 0), Time.local(2006, 3, 20, 1, 45, 0).since(1.day), "st+1.day=>st"
364 365 366
    end
  end

367
  def test_daylight_savings_time_crossings_forward_end_tomorrow
368
    with_env_tz "US/Eastern" do
369
      # dt: US: 2005 October 30th 12:45am
370 371
      assert_equal Time.local(2005, 10, 31, 0, 45, 0), Time.local(2005, 10, 30, 0, 45, 0).tomorrow, "dt+1.day=>st"
      assert_equal Time.local(2005, 11, 1, 0, 45, 0), Time.local(2005, 10, 31, 0, 45, 0).tomorrow, "st+1.day=>st"
372
    end
373
    with_env_tz "NZ" do
374
      # dt: New Zealand: 2006 March 19th 1:45am
375 376
      assert_equal Time.local(2006, 3, 20, 1, 45, 0), Time.local(2006, 3, 19, 1, 45, 0).tomorrow, "dt+1.day=>st"
      assert_equal Time.local(2006, 3, 21, 1, 45, 0), Time.local(2006, 3, 20, 1, 45, 0).tomorrow, "st+1.day=>st"
377 378 379
    end
  end

380
  def test_daylight_savings_time_crossings_backward_end_yesterday
381
    with_env_tz "US/Eastern" do
382
      # dt: US: 2005 October 30th 12:45am
383 384
      assert_equal Time.local(2005, 10, 30, 0, 45, 0), Time.local(2005, 10, 31, 0, 45, 0).yesterday, "st-1.day=>dt"
      assert_equal Time.local(2005, 10, 31, 0, 45, 0), Time.local(2005, 11, 1, 0, 45, 0).yesterday, "st-1.day=>st"
385
    end
386
    with_env_tz "NZ" do
387
      # dt: New Zealand: 2006 March 19th 1:45am
388 389
      assert_equal Time.local(2006, 3, 19, 1, 45, 0), Time.local(2006, 3, 20, 1, 45, 0).yesterday, "st-1.day=>dt"
      assert_equal Time.local(2006, 3, 20, 1, 45, 0), Time.local(2006, 3, 21, 1, 45, 0).yesterday, "st-1.day=>st"
390 391 392
    end
  end

393
  def test_change
394 395 396 397 398 399 400 401 402 403 404 405 406
    assert_equal Time.local(2006, 2, 22, 15, 15, 10), Time.local(2005, 2, 22, 15, 15, 10).change(year: 2006)
    assert_equal Time.local(2005, 6, 22, 15, 15, 10), Time.local(2005, 2, 22, 15, 15, 10).change(month: 6)
    assert_equal Time.local(2012, 9, 22, 15, 15, 10), Time.local(2005, 2, 22, 15, 15, 10).change(year: 2012, month: 9)
    assert_equal Time.local(2005, 2, 22, 16),       Time.local(2005, 2, 22, 15, 15, 10).change(hour: 16)
    assert_equal Time.local(2005, 2, 22, 16, 45),    Time.local(2005, 2, 22, 15, 15, 10).change(hour: 16, min: 45)
    assert_equal Time.local(2005, 2, 22, 15, 45),    Time.local(2005, 2, 22, 15, 15, 10).change(min: 45)

    assert_equal Time.local(2005, 1, 2, 5, 0, 0, 0), Time.local(2005, 1, 2, 11, 22, 33, 44).change(hour: 5)
    assert_equal Time.local(2005, 1, 2, 11, 6, 0, 0), Time.local(2005, 1, 2, 11, 22, 33, 44).change(min: 6)
    assert_equal Time.local(2005, 1, 2, 11, 22, 7, 0), Time.local(2005, 1, 2, 11, 22, 33, 44).change(sec: 7)
    assert_equal Time.local(2005, 1, 2, 11, 22, 33, 8), Time.local(2005, 1, 2, 11, 22, 33, 44).change(usec: 8)
    assert_equal Time.local(2005, 1, 2, 11, 22, 33, 8), Time.local(2005, 1, 2, 11, 22, 33, 2).change(nsec: 8000)
    assert_raise(ArgumentError) { Time.local(2005, 1, 2, 11, 22, 33, 8).change(usec: 1, nsec: 1) }
407
    assert_nothing_raised { Time.new(2015, 5, 9, 10, 00, 00, "+03:00").change(nsec: 999999999) }
D
David Heinemeier Hansson 已提交
408
  end
409

D
David Heinemeier Hansson 已提交
410
  def test_utc_change
411 412 413 414 415 416 417
    assert_equal Time.utc(2006, 2, 22, 15, 15, 10), Time.utc(2005, 2, 22, 15, 15, 10).change(year: 2006)
    assert_equal Time.utc(2005, 6, 22, 15, 15, 10), Time.utc(2005, 2, 22, 15, 15, 10).change(month: 6)
    assert_equal Time.utc(2012, 9, 22, 15, 15, 10), Time.utc(2005, 2, 22, 15, 15, 10).change(year: 2012, month: 9)
    assert_equal Time.utc(2005, 2, 22, 16),       Time.utc(2005, 2, 22, 15, 15, 10).change(hour: 16)
    assert_equal Time.utc(2005, 2, 22, 16, 45),    Time.utc(2005, 2, 22, 15, 15, 10).change(hour: 16, min: 45)
    assert_equal Time.utc(2005, 2, 22, 15, 45),    Time.utc(2005, 2, 22, 15, 15, 10).change(min: 45)
    assert_equal Time.utc(2005, 1, 2, 11, 22, 33, 8), Time.utc(2005, 1, 2, 11, 22, 33, 2).change(nsec: 8000)
418
  end
419

420
  def test_offset_change
421 422 423 424 425 426 427 428 429
    assert_equal Time.new(2006, 2, 22, 15, 15, 10, "-08:00"), Time.new(2005, 2, 22, 15, 15, 10, "-08:00").change(year: 2006)
    assert_equal Time.new(2005, 6, 22, 15, 15, 10, "-08:00"), Time.new(2005, 2, 22, 15, 15, 10, "-08:00").change(month: 6)
    assert_equal Time.new(2012, 9, 22, 15, 15, 10, "-08:00"), Time.new(2005, 2, 22, 15, 15, 10, "-08:00").change(year: 2012, month: 9)
    assert_equal Time.new(2005, 2, 22, 16, 0, 0, "-08:00"),   Time.new(2005, 2, 22, 15, 15, 10, "-08:00").change(hour: 16)
    assert_equal Time.new(2005, 2, 22, 16, 45, 0, "-08:00"),  Time.new(2005, 2, 22, 15, 15, 10, "-08:00").change(hour: 16, min: 45)
    assert_equal Time.new(2005, 2, 22, 15, 45, 0, "-08:00"),  Time.new(2005, 2, 22, 15, 15, 10, "-08:00").change(min: 45)
    assert_equal Time.new(2005, 2, 22, 15, 15, 10, "-08:00"),  Time.new(2005, 2, 22, 15, 15, 0, "-08:00").change(sec: 10)
    assert_equal 10, Time.new(2005, 2, 22, 15, 15, 0, "-08:00").change(usec: 10).usec
    assert_equal 10, Time.new(2005, 2, 22, 15, 15, 0, "-08:00").change(nsec: 10).nsec
430 431
    assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "-08:00").change(usec: 1000000) }
    assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "-08:00").change(nsec: 1000000000) }
432
  end
433 434 435 436 437 438 439

  def test_change_offset
    assert_equal Time.new(2006, 2, 22, 15, 15, 10, "-08:00"), Time.new(2006, 2, 22, 15, 15, 10, "+01:00").change(offset: "-08:00")
    assert_equal Time.new(2006, 2, 22, 15, 15, 10, -28800), Time.new(2006, 2, 22, 15, 15, 10, 3600).change(offset: -28800)
    assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "+01:00").change(usec: 1000000, offset: "-08:00") }
    assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "+01:00").change(nsec: 1000000000, offset: -28800) }
  end
440

441
  def test_advance
442 443 444 445 446 447 448 449 450 451 452 453
    assert_equal Time.local(2006, 2, 28, 15, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(years: 1)
    assert_equal Time.local(2005, 6, 28, 15, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(months: 4)
    assert_equal Time.local(2005, 3, 21, 15, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(weeks: 3)
    assert_equal Time.local(2005, 3, 25, 3, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(weeks: 3.5)
    assert_in_delta Time.local(2005, 3, 26, 12, 51, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(weeks: 3.7), 1
    assert_equal Time.local(2005, 3, 5, 15, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(days: 5)
    assert_equal Time.local(2005, 3, 6, 3, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(days: 5.5)
    assert_in_delta Time.local(2005, 3, 6, 8, 3, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(days: 5.7), 1
    assert_equal Time.local(2012, 9, 28, 15, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(years: 7, months: 7)
    assert_equal Time.local(2013, 10, 3, 15, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(years: 7, months: 19, days: 5)
    assert_equal Time.local(2013, 10, 17, 15, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(years: 7, months: 19, weeks: 2, days: 5)
    assert_equal Time.local(2001, 12, 27, 15, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(years: -3, months: -2, days: -1)
454
    assert_equal Time.local(2005, 2, 28, 15, 15, 10), Time.local(2004, 2, 29, 15, 15, 10).advance(years: 1) # leap day plus one year
455 456 457 458 459 460
    assert_equal Time.local(2005, 2, 28, 20, 15, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(hours: 5)
    assert_equal Time.local(2005, 2, 28, 15, 22, 10), Time.local(2005, 2, 28, 15, 15, 10).advance(minutes: 7)
    assert_equal Time.local(2005, 2, 28, 15, 15, 19), Time.local(2005, 2, 28, 15, 15, 10).advance(seconds: 9)
    assert_equal Time.local(2005, 2, 28, 20, 22, 19), Time.local(2005, 2, 28, 15, 15, 10).advance(hours: 5, minutes: 7, seconds: 9)
    assert_equal Time.local(2005, 2, 28, 10, 8, 1), Time.local(2005, 2, 28, 15, 15, 10).advance(hours: -5, minutes: -7, seconds: -9)
    assert_equal Time.local(2013, 10, 17, 20, 22, 19), Time.local(2005, 2, 28, 15, 15, 10).advance(years: 7, months: 19, weeks: 2, days: 5, hours: 5, minutes: 7, seconds: 9)
461
  end
462

463
  def test_utc_advance
464 465 466 467 468 469 470 471 472 473 474 475
    assert_equal Time.utc(2006, 2, 22, 15, 15, 10), Time.utc(2005, 2, 22, 15, 15, 10).advance(years: 1)
    assert_equal Time.utc(2005, 6, 22, 15, 15, 10), Time.utc(2005, 2, 22, 15, 15, 10).advance(months: 4)
    assert_equal Time.utc(2005, 3, 21, 15, 15, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(weeks: 3)
    assert_equal Time.utc(2005, 3, 25, 3, 15, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(weeks: 3.5)
    assert_in_delta Time.utc(2005, 3, 26, 12, 51, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(weeks: 3.7), 1
    assert_equal Time.utc(2005, 3, 5, 15, 15, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(days: 5)
    assert_equal Time.utc(2005, 3, 6, 3, 15, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(days: 5.5)
    assert_in_delta Time.utc(2005, 3, 6, 8, 3, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(days: 5.7), 1
    assert_equal Time.utc(2012, 9, 22, 15, 15, 10), Time.utc(2005, 2, 22, 15, 15, 10).advance(years: 7, months: 7)
    assert_equal Time.utc(2013, 10, 3, 15, 15, 10), Time.utc(2005, 2, 22, 15, 15, 10).advance(years: 7, months: 19, days: 11)
    assert_equal Time.utc(2013, 10, 17, 15, 15, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(years: 7, months: 19, weeks: 2, days: 5)
    assert_equal Time.utc(2001, 12, 27, 15, 15, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(years: -3, months: -2, days: -1)
476
    assert_equal Time.utc(2005, 2, 28, 15, 15, 10), Time.utc(2004, 2, 29, 15, 15, 10).advance(years: 1) # leap day plus one year
477 478 479 480 481 482
    assert_equal Time.utc(2005, 2, 28, 20, 15, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(hours: 5)
    assert_equal Time.utc(2005, 2, 28, 15, 22, 10), Time.utc(2005, 2, 28, 15, 15, 10).advance(minutes: 7)
    assert_equal Time.utc(2005, 2, 28, 15, 15, 19), Time.utc(2005, 2, 28, 15, 15, 10).advance(seconds: 9)
    assert_equal Time.utc(2005, 2, 28, 20, 22, 19), Time.utc(2005, 2, 28, 15, 15, 10).advance(hours: 5, minutes: 7, seconds: 9)
    assert_equal Time.utc(2005, 2, 28, 10, 8, 1), Time.utc(2005, 2, 28, 15, 15, 10).advance(hours: -5, minutes: -7, seconds: -9)
    assert_equal Time.utc(2013, 10, 17, 20, 22, 19), Time.utc(2005, 2, 28, 15, 15, 10).advance(years: 7, months: 19, weeks: 2, days: 5, hours: 5, minutes: 7, seconds: 9)
483 484
  end

485
  def test_offset_advance
486 487 488 489 490 491 492 493 494 495 496 497
    assert_equal Time.new(2006, 2, 22, 15, 15, 10, "-08:00"), Time.new(2005, 2, 22, 15, 15, 10, "-08:00").advance(years: 1)
    assert_equal Time.new(2005, 6, 22, 15, 15, 10, "-08:00"), Time.new(2005, 2, 22, 15, 15, 10, "-08:00").advance(months: 4)
    assert_equal Time.new(2005, 3, 21, 15, 15, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(weeks: 3)
    assert_equal Time.new(2005, 3, 25, 3, 15, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(weeks: 3.5)
    assert_in_delta Time.new(2005, 3, 26, 12, 51, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(weeks: 3.7), 1
    assert_equal Time.new(2005, 3, 5, 15, 15, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(days: 5)
    assert_equal Time.new(2005, 3, 6, 3, 15, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(days: 5.5)
    assert_in_delta Time.new(2005, 3, 6, 8, 3, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(days: 5.7), 1
    assert_equal Time.new(2012, 9, 22, 15, 15, 10, "-08:00"), Time.new(2005, 2, 22, 15, 15, 10, "-08:00").advance(years: 7, months: 7)
    assert_equal Time.new(2013, 10, 3, 15, 15, 10, "-08:00"), Time.new(2005, 2, 22, 15, 15, 10, "-08:00").advance(years: 7, months: 19, days: 11)
    assert_equal Time.new(2013, 10, 17, 15, 15, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(years: 7, months: 19, weeks: 2, days: 5)
    assert_equal Time.new(2001, 12, 27, 15, 15, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(years: -3, months: -2, days: -1)
498
    assert_equal Time.new(2005, 2, 28, 15, 15, 10, "-08:00"), Time.new(2004, 2, 29, 15, 15, 10, "-08:00").advance(years: 1) # leap day plus one year
499 500 501 502 503 504
    assert_equal Time.new(2005, 2, 28, 20, 15, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(hours: 5)
    assert_equal Time.new(2005, 2, 28, 15, 22, 10, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(minutes: 7)
    assert_equal Time.new(2005, 2, 28, 15, 15, 19, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(seconds: 9)
    assert_equal Time.new(2005, 2, 28, 20, 22, 19, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(hours: 5, minutes: 7, seconds: 9)
    assert_equal Time.new(2005, 2, 28, 10, 8, 1, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(hours: -5, minutes: -7, seconds: -9)
    assert_equal Time.new(2013, 10, 17, 20, 22, 19, "-08:00"), Time.new(2005, 2, 28, 15, 15, 10, "-08:00").advance(years: 7, months: 19, weeks: 2, days: 5, hours: 5, minutes: 7, seconds: 9)
505 506
  end

507 508
  def test_advance_with_nsec
    t = Time.at(0, Rational(108635108, 1000))
509
    assert_equal t, t.advance(months: 0)
510 511
  end

512
  def test_advance_gregorian_proleptic
513 514 515 516
    assert_equal Time.local(1582, 10, 14, 15, 15, 10), Time.local(1582, 10, 15, 15, 15, 10).advance(days: -1)
    assert_equal Time.local(1582, 10, 15, 15, 15, 10), Time.local(1582, 10, 14, 15, 15, 10).advance(days: 1)
    assert_equal Time.local(1582, 10, 5, 15, 15, 10), Time.local(1582, 10, 4, 15, 15, 10).advance(days: 1)
    assert_equal Time.local(1582, 10, 4, 15, 15, 10), Time.local(1582, 10, 5, 15, 15, 10).advance(days: -1)
517 518
  end

519
  def test_last_week
520
    with_env_tz "US/Eastern" do
521 522 523 524 525
      assert_equal Time.local(2005, 2, 21), Time.local(2005, 3, 1, 15, 15, 10).last_week
      assert_equal Time.local(2005, 2, 22), Time.local(2005, 3, 1, 15, 15, 10).last_week(:tuesday)
      assert_equal Time.local(2005, 2, 25), Time.local(2005, 3, 1, 15, 15, 10).last_week(:friday)
      assert_equal Time.local(2006, 10, 30), Time.local(2006, 11, 6, 0, 0, 0).last_week
      assert_equal Time.local(2006, 11, 15), Time.local(2006, 11, 23, 0, 0, 0).last_week(:wednesday)
526 527
    end
  end
528

529
  def test_next_week_near_daylight_start
530
    with_env_tz "US/Eastern" do
531
      assert_equal Time.local(2006, 4, 3), Time.local(2006, 4, 2, 23, 1, 0).next_week, "just crossed standard => daylight"
532
    end
533
    with_env_tz "NZ" do
534
      assert_equal Time.local(2006, 10, 2), Time.local(2006, 10, 1, 23, 1, 0).next_week, "just crossed standard => daylight"
535
    end
536 537 538
  end

  def test_next_week_near_daylight_end
539
    with_env_tz "US/Eastern" do
540
      assert_equal Time.local(2006, 10, 30), Time.local(2006, 10, 29, 23, 1, 0).next_week, "just crossed daylight => standard"
541
    end
542
    with_env_tz "NZ" do
543
      assert_equal Time.local(2006, 3, 20), Time.local(2006, 3, 19, 23, 1, 0).next_week, "just crossed daylight => standard"
544
    end
545 546
  end

547
  def test_to_s
548
    time = Time.utc(2005, 2, 21, 17, 44, 30.12345678901)
549 550
    assert_equal time.to_default_s,                 time.to_s
    assert_equal time.to_default_s,                 time.to_s(:doesnt_exist)
551 552 553
    assert_equal "2005-02-21 17:44:30",             time.to_s(:db)
    assert_equal "21 Feb 17:44",                    time.to_s(:short)
    assert_equal "17:44",                           time.to_s(:time)
554 555
    assert_equal "20050221174430",                  time.to_s(:number)
    assert_equal "20050221174430123456789",         time.to_s(:nsec)
M
Maarten Jacobs 已提交
556
    assert_equal "20050221174430123456",            time.to_s(:usec)
557 558
    assert_equal "February 21, 2005 17:44",         time.to_s(:long)
    assert_equal "February 21st, 2005 17:44",       time.to_s(:long_ordinal)
559
    with_env_tz "UTC" do
560 561
      assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822)
    end
562 563 564
    with_env_tz "US/Central" do
      assert_equal "Thu, 05 Feb 2009 14:30:05 -0600", Time.local(2009, 2, 5, 14, 30, 5).to_s(:rfc822)
      assert_equal "Mon, 09 Jun 2008 04:05:01 -0500", Time.local(2008, 6, 9, 4, 5, 1).to_s(:rfc822)
565 566 567
      assert_equal "2009-02-05T14:30:05-06:00", Time.local(2009, 2, 5, 14, 30, 5).to_s(:iso8601)
      assert_equal "2008-06-09T04:05:01-05:00", Time.local(2008, 6, 9, 4, 5, 1).to_s(:iso8601)
      assert_equal "2009-02-05T14:30:05Z", Time.utc(2009, 2, 5, 14, 30, 5).to_s(:iso8601)
568
    end
569
  end
570

571
  def test_custom_date_format
572 573
    Time::DATE_FORMATS[:custom] = "%Y%m%d%H%M%S"
    assert_equal "20050221143000", Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom)
574
    Time::DATE_FORMATS.delete(:custom)
575
  end
576

577 578 579 580 581
  def test_rfc3339_with_fractional_seconds
    time = Time.new(1999, 12, 31, 19, 0, Rational(1, 8), -18000)
    assert_equal "1999-12-31T19:00:00.125-05:00", time.rfc3339(3)
  end

582 583 584 585 586
  def test_to_date
    assert_equal Date.new(2005, 2, 21), Time.local(2005, 2, 21, 17, 44, 30).to_date
  end

  def test_to_datetime
587
    assert_equal Time.utc(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, 0)
588
    with_env_tz "US/Eastern" do
589
      assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400))
590
    end
591
    with_env_tz "NZ" do
592
      assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400))
593 594
    end
    assert_equal ::Date::ITALY, Time.utc(2005, 2, 21, 17, 44, 30).to_datetime.start # use Ruby's default start value
595 596
  end

597
  def test_to_time
598
    with_env_tz "US/Eastern" do
599 600 601 602
      assert_equal Time, Time.local(2005, 2, 21, 17, 44, 30).to_time.class
      assert_equal Time.local(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30).to_time
      assert_equal Time.local(2005, 2, 21, 17, 44, 30).utc_offset, Time.local(2005, 2, 21, 17, 44, 30).to_time.utc_offset
    end
603
  end
604 605 606 607 608 609 610

  # NOTE: this test seems to fail (changeset 1958) only on certain platforms,
  # like OSX, and FreeBSD 5.4.
  def test_fp_inaccuracy_ticket_1836
    midnight = Time.local(2005, 2, 21, 0, 0, 0)
    assert_equal midnight.midnight, (midnight + 1.hour + 0.000001).midnight
  end
611

612
  def test_days_in_month_with_year
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    assert_equal 31, Time.days_in_month(1, 2005)

    assert_equal 28, Time.days_in_month(2, 2005)
    assert_equal 29, Time.days_in_month(2, 2004)
    assert_equal 29, Time.days_in_month(2, 2000)
    assert_equal 28, Time.days_in_month(2, 1900)

    assert_equal 31, Time.days_in_month(3, 2005)
    assert_equal 30, Time.days_in_month(4, 2005)
    assert_equal 31, Time.days_in_month(5, 2005)
    assert_equal 30, Time.days_in_month(6, 2005)
    assert_equal 31, Time.days_in_month(7, 2005)
    assert_equal 31, Time.days_in_month(8, 2005)
    assert_equal 30, Time.days_in_month(9, 2005)
    assert_equal 31, Time.days_in_month(10, 2005)
    assert_equal 30, Time.days_in_month(11, 2005)
    assert_equal 31, Time.days_in_month(12, 2005)
  end
631

632
  def test_days_in_month_feb_in_common_year_without_year_arg
633 634 635
    Time.stub(:now, Time.utc(2007)) do
      assert_equal 28, Time.days_in_month(2)
    end
636
  end
637

638
  def test_days_in_month_feb_in_leap_year_without_year_arg
639 640 641
    Time.stub(:now, Time.utc(2008)) do
      assert_equal 29, Time.days_in_month(2)
    end
642
  end
643

J
Jon Pascoe 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
  def test_days_in_year_with_year
    assert_equal 365, Time.days_in_year(2005)
    assert_equal 366, Time.days_in_year(2004)
    assert_equal 366, Time.days_in_year(2000)
    assert_equal 365, Time.days_in_year(1900)
  end

  def test_days_in_year_in_common_year_without_year_arg
    Time.stub(:now, Time.utc(2007)) do
      assert_equal 365, Time.days_in_year
    end
  end

  def test_days_in_year_in_leap_year_without_year_arg
    Time.stub(:now, Time.utc(2008)) do
      assert_equal 366, Time.days_in_year
    end
  end

663 664 665
  def test_xmlschema_is_available
    assert_nothing_raised { Time.now.xmlschema }
  end
666

667
  def test_today_with_time_local
668
    Date.stub(:current, Date.new(2000, 1, 1)) do
669 670 671 672
      assert_equal false, Time.local(1999, 12, 31, 23, 59, 59).today?
      assert_equal true,  Time.local(2000, 1, 1, 0).today?
      assert_equal true,  Time.local(2000, 1, 1, 23, 59, 59).today?
      assert_equal false, Time.local(2000, 1, 2, 0).today?
673
    end
674 675 676
  end

  def test_today_with_time_utc
677
    Date.stub(:current, Date.new(2000, 1, 1)) do
678 679 680 681
      assert_equal false, Time.utc(1999, 12, 31, 23, 59, 59).today?
      assert_equal true,  Time.utc(2000, 1, 1, 0).today?
      assert_equal true,  Time.utc(2000, 1, 1, 23, 59, 59).today?
      assert_equal false, Time.utc(2000, 1, 2, 0).today?
682
    end
683 684 685
  end

  def test_past_with_time_current_as_time_local
686
    with_env_tz "US/Eastern" do
687 688 689 690 691 692 693
      Time.stub(:current, Time.local(2005, 2, 10, 15, 30, 45)) do
        assert_equal true,  Time.local(2005, 2, 10, 15, 30, 44).past?
        assert_equal false,  Time.local(2005, 2, 10, 15, 30, 45).past?
        assert_equal false,  Time.local(2005, 2, 10, 15, 30, 46).past?
        assert_equal true,  Time.utc(2005, 2, 10, 20, 30, 44).past?
        assert_equal false,  Time.utc(2005, 2, 10, 20, 30, 45).past?
        assert_equal false,  Time.utc(2005, 2, 10, 20, 30, 46).past?
694
      end
695 696 697 698
    end
  end

  def test_past_with_time_current_as_time_with_zone
699
    with_env_tz "US/Eastern" do
700
      twz = Time.utc(2005, 2, 10, 15, 30, 45).in_time_zone("Central Time (US & Canada)")
701
      Time.stub(:current, twz) do
702 703 704 705 706 707
        assert_equal true,  Time.local(2005, 2, 10, 10, 30, 44).past?
        assert_equal false,  Time.local(2005, 2, 10, 10, 30, 45).past?
        assert_equal false,  Time.local(2005, 2, 10, 10, 30, 46).past?
        assert_equal true,  Time.utc(2005, 2, 10, 15, 30, 44).past?
        assert_equal false,  Time.utc(2005, 2, 10, 15, 30, 45).past?
        assert_equal false,  Time.utc(2005, 2, 10, 15, 30, 46).past?
708
      end
709 710 711 712
    end
  end

  def test_future_with_time_current_as_time_local
713
    with_env_tz "US/Eastern" do
714 715 716 717 718 719 720
      Time.stub(:current, Time.local(2005, 2, 10, 15, 30, 45)) do
        assert_equal false,  Time.local(2005, 2, 10, 15, 30, 44).future?
        assert_equal false,  Time.local(2005, 2, 10, 15, 30, 45).future?
        assert_equal true,  Time.local(2005, 2, 10, 15, 30, 46).future?
        assert_equal false,  Time.utc(2005, 2, 10, 20, 30, 44).future?
        assert_equal false,  Time.utc(2005, 2, 10, 20, 30, 45).future?
        assert_equal true,  Time.utc(2005, 2, 10, 20, 30, 46).future?
721
      end
722 723 724 725
    end
  end

  def test_future_with_time_current_as_time_with_zone
726
    with_env_tz "US/Eastern" do
727
      twz = Time.utc(2005, 2, 10, 15, 30, 45).in_time_zone("Central Time (US & Canada)")
728
      Time.stub(:current, twz) do
729 730 731 732 733 734
        assert_equal false,  Time.local(2005, 2, 10, 10, 30, 44).future?
        assert_equal false,  Time.local(2005, 2, 10, 10, 30, 45).future?
        assert_equal true,  Time.local(2005, 2, 10, 10, 30, 46).future?
        assert_equal false,  Time.utc(2005, 2, 10, 15, 30, 44).future?
        assert_equal false,  Time.utc(2005, 2, 10, 15, 30, 45).future?
        assert_equal true,  Time.utc(2005, 2, 10, 15, 30, 46).future?
735
      end
736 737 738
    end
  end

739
  def test_acts_like_time
740
    assert_predicate Time.new, :acts_like_time?
741
  end
742

743
  def test_formatted_offset_with_utc
744 745 746
    assert_equal "+00:00", Time.utc(2000).formatted_offset
    assert_equal "+0000", Time.utc(2000).formatted_offset(false)
    assert_equal "UTC", Time.utc(2000).formatted_offset(true, "UTC")
747
  end
748

749
  def test_formatted_offset_with_local
750 751 752 753 754
    with_env_tz "US/Eastern" do
      assert_equal "-05:00", Time.local(2000).formatted_offset
      assert_equal "-0500", Time.local(2000).formatted_offset(false)
      assert_equal "-04:00", Time.local(2000, 7).formatted_offset
      assert_equal "-0400", Time.local(2000, 7).formatted_offset(false)
755 756
    end
  end
757

758 759 760 761 762
  def test_compare_with_time
    assert_equal  1, Time.utc(2000) <=> Time.utc(1999, 12, 31, 23, 59, 59, 999)
    assert_equal  0, Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0)
    assert_equal(-1, Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0, 001))
  end
763

764 765 766 767 768
  def test_compare_with_datetime
    assert_equal  1, Time.utc(2000) <=> DateTime.civil(1999, 12, 31, 23, 59, 59)
    assert_equal  0, Time.utc(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 0)
    assert_equal(-1, Time.utc(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 1))
  end
769

770
  def test_compare_with_time_with_zone
771 772 773
    assert_equal  1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new(Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone["UTC"])
    assert_equal  0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone["UTC"])
    assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone["UTC"]))
774
  end
775

776 777 778
  def test_compare_with_string
    assert_equal   1, Time.utc(2000) <=> Time.utc(1999, 12, 31, 23, 59, 59, 999).to_s
    assert_equal   0, Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0).to_s
779
    assert_equal(-1, Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 1, 0).to_s)
780
    assert_nil Time.utc(2000) <=> "Invalid as Time"
781 782
  end

783 784 785 786 787 788 789 790 791 792 793
  def test_at_with_datetime
    assert_equal Time.utc(2000, 1, 1, 0, 0, 0), Time.at(DateTime.civil(2000, 1, 1, 0, 0, 0))

    # Only test this if the underlying Time.at raises a TypeError
    begin
      Time.at_without_coercion(Time.now, 0)
    rescue TypeError
      assert_raise(TypeError) { assert_equal(Time.utc(2000, 1, 1, 0, 0, 0), Time.at(DateTime.civil(2000, 1, 1, 0, 0, 0), 0)) }
    end
  end

794
  def test_at_with_datetime_returns_local_time
795 796
    with_env_tz "US/Eastern" do
      dt = DateTime.civil(2000, 1, 1, 0, 0, 0, "+0")
797
      assert_equal Time.local(1999, 12, 31, 19, 0, 0), Time.at(dt)
798
      assert_equal "EST", Time.at(dt).zone
799 800 801
      assert_equal(-18000, Time.at(dt).utc_offset)

      # Daylight savings
802
      dt = DateTime.civil(2000, 7, 1, 1, 0, 0, "+1")
803
      assert_equal Time.local(2000, 6, 30, 20, 0, 0), Time.at(dt)
804
      assert_equal "EDT", Time.at(dt).zone
805
      assert_equal(-14400, Time.at(dt).utc_offset)
806 807 808
    end
  end

809
  def test_at_with_time_with_zone
810
    assert_equal Time.utc(2000, 1, 1, 0, 0, 0), Time.at(ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone["UTC"]))
811 812 813 814 815

    # Only test this if the underlying Time.at raises a TypeError
    begin
      Time.at_without_coercion(Time.now, 0)
    rescue TypeError
816
      assert_raise(TypeError) { assert_equal(Time.utc(2000, 1, 1, 0, 0, 0), Time.at(ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone["UTC"]), 0)) }
817 818 819
    end
  end

820
  def test_at_with_time_with_zone_returns_local_time
821 822
    with_env_tz "US/Eastern" do
      twz = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone["London"])
823
      assert_equal Time.local(1999, 12, 31, 19, 0, 0), Time.at(twz)
824
      assert_equal "EST", Time.at(twz).zone
825 826 827
      assert_equal(-18000, Time.at(twz).utc_offset)

      # Daylight savings
828
      twz = ActiveSupport::TimeWithZone.new(Time.utc(2000, 7, 1, 0, 0, 0), ActiveSupport::TimeZone["London"])
829
      assert_equal Time.local(2000, 6, 30, 20, 0, 0), Time.at(twz)
830
      assert_equal "EDT", Time.at(twz).zone
831
      assert_equal(-14400, Time.at(twz).utc_offset)
832 833 834
    end
  end

835 836 837 838
  def test_at_with_time_microsecond_precision
    assert_equal Time.at(Time.utc(2000, 1, 1, 0, 0, 0, 111)).to_f, Time.utc(2000, 1, 1, 0, 0, 0, 111).to_f
  end

839
  def test_at_with_utc_time
840
    with_env_tz "US/Eastern" do
841
      assert_equal Time.utc(2000), Time.at(Time.utc(2000))
842
      assert_equal "UTC", Time.at(Time.utc(2000)).zone
843
      assert_equal(0, Time.at(Time.utc(2000)).utc_offset)
844 845 846 847
    end
  end

  def test_at_with_local_time
848
    with_env_tz "US/Eastern" do
849
      assert_equal Time.local(2000), Time.at(Time.local(2000))
850
      assert_equal "EST", Time.at(Time.local(2000)).zone
851
      assert_equal(-18000, Time.at(Time.local(2000)).utc_offset)
852 853

      assert_equal Time.local(2000, 7, 1), Time.at(Time.local(2000, 7, 1))
854
      assert_equal "EDT", Time.at(Time.local(2000, 7, 1)).zone
855
      assert_equal(-14400, Time.at(Time.local(2000, 7, 1)).utc_offset)
856 857 858
    end
  end

859
  def test_eql?
860 861 862
    assert_equal true, Time.utc(2000).eql?(ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["UTC"]))
    assert_equal true, Time.utc(2000).eql?(ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]))
    assert_equal false, Time.utc(2000, 1, 1, 0, 0, 1).eql?(ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["UTC"]))
863 864
  end

865
  def test_minus_with_time_with_zone
866
    assert_equal  86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), ActiveSupport::TimeZone["UTC"])
867
  end
868

869 870 871
  def test_minus_with_datetime
    assert_equal  86_400.0, Time.utc(2000, 1, 2) - DateTime.civil(2000, 1, 1)
  end
872

873
  def test_time_created_with_local_constructor_cannot_represent_times_during_hour_skipped_by_dst
874
    with_env_tz "US/Eastern" do
875 876 877
      # On Apr 2 2006 at 2:00AM in US, clocks were moved forward to 3:00AM.
      # Therefore, 2AM EST doesn't exist for this date; Time.local fails over to 3:00AM EDT
      assert_equal Time.local(2006, 4, 2, 3), Time.local(2006, 4, 2, 2)
878
      assert_predicate Time.local(2006, 4, 2, 2), :dst?
879 880 881
    end
  end

882 883
  def test_case_equality
    assert Time === Time.utc(2000)
884
    assert Time === ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["UTC"])
J
Jeremy Evans 已提交
885
    assert Time === Class.new(Time).utc(2000)
886
    assert_equal false, Time === DateTime.civil(2000)
J
Jeremy Evans 已提交
887
    assert_equal false, Class.new(Time) === Time.utc(2000)
888
    assert_equal false, Class.new(Time) === ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["UTC"])
889 890
  end

891
  def test_all_day
892
    assert_equal Time.local(2011, 6, 7, 0, 0, 0)..Time.local(2011, 6, 7, 23, 59, 59, Rational(999999999, 1000)), Time.local(2011, 6, 7, 10, 10, 10).all_day
893 894
  end

895
  def test_all_day_with_timezone
896 897
    beginning_of_day = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], Time.local(2011, 6, 7, 0, 0, 0))
    end_of_day = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Hawaii"], Time.local(2011, 6, 7, 23, 59, 59, Rational(999999999, 1000)))
898

899 900
    assert_equal beginning_of_day, ActiveSupport::TimeWithZone.new(Time.local(2011, 6, 7, 10, 10, 10), ActiveSupport::TimeZone["Hawaii"]).all_day.begin
    assert_equal end_of_day, ActiveSupport::TimeWithZone.new(Time.local(2011, 6, 7, 10, 10, 10), ActiveSupport::TimeZone["Hawaii"]).all_day.end
901 902
  end

903
  def test_all_week
904 905
    assert_equal Time.local(2011, 6, 6, 0, 0, 0)..Time.local(2011, 6, 12, 23, 59, 59, Rational(999999999, 1000)), Time.local(2011, 6, 7, 10, 10, 10).all_week
    assert_equal Time.local(2011, 6, 5, 0, 0, 0)..Time.local(2011, 6, 11, 23, 59, 59, Rational(999999999, 1000)), Time.local(2011, 6, 7, 10, 10, 10).all_week(:sunday)
906 907
  end

908
  def test_all_month
909
    assert_equal Time.local(2011, 6, 1, 0, 0, 0)..Time.local(2011, 6, 30, 23, 59, 59, Rational(999999999, 1000)), Time.local(2011, 6, 7, 10, 10, 10).all_month
910 911
  end

912
  def test_all_quarter
913
    assert_equal Time.local(2011, 4, 1, 0, 0, 0)..Time.local(2011, 6, 30, 23, 59, 59, Rational(999999999, 1000)), Time.local(2011, 6, 7, 10, 10, 10).all_quarter
914 915
  end

916
  def test_all_year
917
    assert_equal Time.local(2011, 1, 1, 0, 0, 0)..Time.local(2011, 12, 31, 23, 59, 59, Rational(999999999, 1000)), Time.local(2011, 6, 7, 10, 10, 10).all_year
918
  end
A
Andrew White 已提交
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

  def test_rfc3339_parse
    time = Time.rfc3339("1999-12-31T19:00:00.125-05:00")

    assert_equal 1999, time.year
    assert_equal 12, time.month
    assert_equal 31, time.day
    assert_equal 19, time.hour
    assert_equal 0, time.min
    assert_equal 0, time.sec
    assert_equal 125000, time.usec
    assert_equal(-18000, time.utc_offset)

    exception = assert_raises(ArgumentError) do
      Time.rfc3339("1999-12-31")
    end

    assert_equal "invalid date", exception.message

    exception = assert_raises(ArgumentError) do
      Time.rfc3339("1999-12-31T19:00:00")
    end

    assert_equal "invalid date", exception.message

    exception = assert_raises(ArgumentError) do
      Time.rfc3339("foobar")
    end

    assert_equal "invalid date", exception.message
  end
950
end
951

952
class TimeExtMarshalingTest < ActiveSupport::TestCase
953 954
  def test_marshaling_with_utc_instance
    t = Time.utc(2000)
955
    unmarshaled = Marshal.load(Marshal.dump(t))
956
    assert_equal "UTC", unmarshaled.zone
957
    assert_equal t, unmarshaled
958
  end
959 960

  def test_marshaling_with_local_instance
961
    t = Time.local(2000)
962
    unmarshaled = Marshal.load(Marshal.dump(t))
963
    assert_equal t.zone, unmarshaled.zone
964
    assert_equal t, unmarshaled
965
  end
966 967

  def test_marshaling_with_frozen_utc_instance
968
    t = Time.utc(2000).freeze
969
    unmarshaled = Marshal.load(Marshal.dump(t))
970
    assert_equal "UTC", unmarshaled.zone
971
    assert_equal t, unmarshaled
972
  end
973 974

  def test_marshaling_with_frozen_local_instance
975
    t = Time.local(2000).freeze
976
    unmarshaled = Marshal.load(Marshal.dump(t))
977
    assert_equal t.zone, unmarshaled.zone
978
    assert_equal t, unmarshaled
979
  end
980 981

  def test_marshalling_preserves_fractional_seconds
982
    t = Time.parse("00:00:00.500")
983 984 985 986
    unmarshaled = Marshal.load(Marshal.dump(t))
    assert_equal t.to_f, unmarshaled.to_f
    assert_equal t, unmarshaled
  end
987 988 989 990

  def test_last_quarter_on_31st
    assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter
  end
991
end