PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/activesupport/test/core_ext/time_with_zone_test.rb

https://github.com/ghar/rails
Ruby | 926 lines | 775 code | 131 blank | 20 comment | 0 complexity | d9192c17b27eb4c48a670240ebb3d87f MD5 | raw file
  1. require 'abstract_unit'
  2. require 'active_support/time'
  3. require 'active_support/json'
  4. class TimeWithZoneTest < Test::Unit::TestCase
  5. def setup
  6. @utc = Time.utc(2000, 1, 1, 0)
  7. @time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  8. @twz = ActiveSupport::TimeWithZone.new(@utc, @time_zone)
  9. end
  10. def test_utc
  11. assert_equal @utc, @twz.utc
  12. end
  13. def test_time
  14. assert_equal Time.utc(1999, 12, 31, 19), @twz.time
  15. end
  16. def test_time_zone
  17. assert_equal @time_zone, @twz.time_zone
  18. end
  19. def test_in_time_zone
  20. Time.use_zone 'Alaska' do
  21. assert_equal ActiveSupport::TimeWithZone.new(@utc, ActiveSupport::TimeZone['Alaska']), @twz.in_time_zone
  22. end
  23. end
  24. def test_in_time_zone_with_argument
  25. assert_equal ActiveSupport::TimeWithZone.new(@utc, ActiveSupport::TimeZone['Alaska']), @twz.in_time_zone('Alaska')
  26. end
  27. def test_in_time_zone_with_new_zone_equal_to_old_zone_does_not_create_new_object
  28. assert_equal @twz.object_id, @twz.in_time_zone(ActiveSupport::TimeZone['Eastern Time (US & Canada)']).object_id
  29. end
  30. def test_in_time_zone_with_bad_argument
  31. assert_raise(ArgumentError) { @twz.in_time_zone('No such timezone exists') }
  32. assert_raise(ArgumentError) { @twz.in_time_zone(-15.hours) }
  33. assert_raise(ArgumentError) { @twz.in_time_zone(Object.new) }
  34. end
  35. def test_localtime
  36. assert_equal @twz.localtime, @twz.utc.getlocal
  37. end
  38. def test_utc?
  39. assert_equal false, @twz.utc?
  40. assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone['UTC']).utc?
  41. end
  42. def test_formatted_offset
  43. assert_equal '-05:00', @twz.formatted_offset
  44. assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst
  45. end
  46. def test_dst?
  47. assert_equal false, @twz.dst?
  48. assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).dst?
  49. end
  50. def test_zone
  51. assert_equal 'EST', @twz.zone
  52. assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst
  53. end
  54. def test_to_json_with_use_standard_json_time_format_config_set_to_false
  55. old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, false
  56. assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(@twz)
  57. ensure
  58. ActiveSupport.use_standard_json_time_format = old
  59. end
  60. def test_to_json_with_use_standard_json_time_format_config_set_to_true
  61. old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, true
  62. assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(@twz)
  63. ensure
  64. ActiveSupport.use_standard_json_time_format = old
  65. end
  66. def test_strftime
  67. assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z')
  68. end
  69. def test_inspect
  70. assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect
  71. end
  72. def test_to_s
  73. assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s
  74. end
  75. def test_to_formatted_s
  76. assert_equal '1999-12-31 19:00:00 -0500', @twz.to_formatted_s
  77. end
  78. def test_to_s_db
  79. assert_equal '2000-01-01 00:00:00', @twz.to_s(:db)
  80. end
  81. def test_xmlschema
  82. assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema
  83. end
  84. def test_xmlschema_with_fractional_seconds
  85. @twz += 0.1234560001 # advance the time by a fraction of a second
  86. assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3)
  87. assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6)
  88. assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12)
  89. end
  90. def test_xmlschema_with_fractional_seconds_lower_than_hundred_thousand
  91. @twz += 0.001234 # advance the time by a fraction
  92. assert_equal "1999-12-31T19:00:00.001-05:00", @twz.xmlschema(3)
  93. assert_equal "1999-12-31T19:00:00.001234-05:00", @twz.xmlschema(6)
  94. assert_equal "1999-12-31T19:00:00.001234-05:00", @twz.xmlschema(12)
  95. end
  96. def test_to_yaml
  97. assert_match(/^--- 2000-01-01 00:00:00(\.0+)?\s*Z\n/, @twz.to_yaml)
  98. end
  99. def test_ruby_to_yaml
  100. assert_match(/---\s*\n:twz: 2000-01-01 00:00:00(\.0+)?\s*Z\n/, {:twz => @twz}.to_yaml)
  101. end
  102. def test_httpdate
  103. assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate
  104. end
  105. def test_rfc2822
  106. assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822
  107. end
  108. def test_compare_with_time
  109. assert_equal 1, @twz <=> Time.utc(1999, 12, 31, 23, 59, 59)
  110. assert_equal 0, @twz <=> Time.utc(2000, 1, 1, 0, 0, 0)
  111. assert_equal(-1, @twz <=> Time.utc(2000, 1, 1, 0, 0, 1))
  112. end
  113. def test_compare_with_datetime
  114. assert_equal 1, @twz <=> DateTime.civil(1999, 12, 31, 23, 59, 59)
  115. assert_equal 0, @twz <=> DateTime.civil(2000, 1, 1, 0, 0, 0)
  116. assert_equal(-1, @twz <=> DateTime.civil(2000, 1, 1, 0, 0, 1))
  117. end
  118. def test_compare_with_time_with_zone
  119. assert_equal 1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone['UTC'] )
  120. assert_equal 0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone['UTC'] )
  121. assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone['UTC'] ))
  122. end
  123. def test_between?
  124. assert @twz.between?(Time.utc(1999,12,31,23,59,59), Time.utc(2000,1,1,0,0,1))
  125. assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2))
  126. end
  127. def test_today
  128. Date.stubs(:current).returns(Date.new(2000, 1, 1))
  129. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(1999,12,31,23,59,59) ).today?
  130. assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,0) ).today?
  131. assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,1,23,59,59) ).today?
  132. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.utc(2000,1,2,0) ).today?
  133. end
  134. def test_past_with_time_current_as_time_local
  135. with_env_tz 'US/Eastern' do
  136. Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
  137. assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
  138. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
  139. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
  140. end
  141. end
  142. def test_past_with_time_current_as_time_with_zone
  143. twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
  144. Time.stubs(:current).returns(twz)
  145. assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).past?
  146. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).past?
  147. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).past?
  148. end
  149. def test_future_with_time_current_as_time_local
  150. with_env_tz 'US/Eastern' do
  151. Time.stubs(:current).returns(Time.local(2005,2,10,15,30,45))
  152. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
  153. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
  154. assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
  155. end
  156. end
  157. def future_with_time_current_as_time_with_zone
  158. twz = ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45) )
  159. Time.stubs(:current).returns(twz)
  160. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,44)).future?
  161. assert_equal false, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,45)).future?
  162. assert_equal true, ActiveSupport::TimeWithZone.new( nil, @time_zone, Time.local(2005,2,10,15,30,46)).future?
  163. end
  164. def test_eql?
  165. assert @twz.eql?(Time.utc(2000))
  166. assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
  167. end
  168. def test_plus_with_integer
  169. assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time
  170. end
  171. def test_plus_with_integer_when_self_wraps_datetime
  172. datetime = DateTime.civil(2000, 1, 1, 0)
  173. twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
  174. assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time
  175. end
  176. def test_plus_when_crossing_time_class_limit
  177. twz = ActiveSupport::TimeWithZone.new(Time.utc(2038, 1, 19), @time_zone)
  178. assert_equal [0, 0, 19, 19, 1, 2038], (twz + 86_400).to_a[0,6]
  179. end
  180. def test_plus_with_duration
  181. assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time
  182. end
  183. def test_minus_with_integer
  184. assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time
  185. end
  186. def test_minus_with_integer_when_self_wraps_datetime
  187. datetime = DateTime.civil(2000, 1, 1, 0)
  188. twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
  189. assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time
  190. end
  191. def test_minus_with_duration
  192. assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time
  193. end
  194. def test_minus_with_time
  195. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1)
  196. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['Hawaii'] ) - Time.utc(2000, 1, 1)
  197. end
  198. def test_minus_with_time_with_zone
  199. twz1 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] )
  200. twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] )
  201. assert_equal 86_400.0, twz2 - twz1
  202. end
  203. def test_minus_with_datetime
  204. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1)
  205. end
  206. def test_minus_with_wrapped_datetime
  207. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1)
  208. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1)
  209. end
  210. def test_plus_and_minus_enforce_spring_dst_rules
  211. utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start
  212. twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
  213. assert_equal Time.utc(2006,4,2,1,59,59), twz.time
  214. assert_equal false, twz.dst?
  215. assert_equal 'EST', twz.zone
  216. twz = twz + 1
  217. assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT
  218. assert_equal true, twz.dst?
  219. assert_equal 'EDT', twz.zone
  220. twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST
  221. assert_equal Time.utc(2006,4,2,1,59,59), twz.time
  222. assert_equal false, twz.dst?
  223. assert_equal 'EST', twz.zone
  224. end
  225. def test_plus_and_minus_enforce_fall_dst_rules
  226. utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end
  227. twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
  228. assert_equal Time.utc(2006,10,29,1,59,59), twz.time
  229. assert_equal true, twz.dst?
  230. assert_equal 'EDT', twz.zone
  231. twz = twz + 1
  232. assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST
  233. assert_equal false, twz.dst?
  234. assert_equal 'EST', twz.zone
  235. twz = twz - 1
  236. assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT
  237. assert_equal true, twz.dst?
  238. assert_equal 'EDT', twz.zone
  239. end
  240. def test_to_a
  241. assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), ActiveSupport::TimeZone['Hawaii'] ).to_a
  242. end
  243. def test_to_f
  244. result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Hawaii'] ).to_f
  245. assert_equal 946684800.0, result
  246. assert_kind_of Float, result
  247. end
  248. def test_to_i
  249. result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Hawaii'] ).to_i
  250. assert_equal 946684800, result
  251. assert_kind_of Integer, result
  252. end
  253. def test_to_i_with_wrapped_datetime
  254. datetime = DateTime.civil(2000, 1, 1, 0)
  255. twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
  256. assert_equal 946684800, twz.to_i
  257. end
  258. def test_to_time
  259. assert_equal @twz, @twz.to_time
  260. end
  261. def test_to_date
  262. # 1 sec before midnight Jan 1 EST
  263. assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
  264. # midnight Jan 1 EST
  265. assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
  266. # 1 sec before midnight Jan 2 EST
  267. assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
  268. # midnight Jan 2 EST
  269. assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date
  270. end
  271. def test_to_datetime
  272. assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime
  273. end
  274. def test_acts_like_time
  275. assert @twz.acts_like?(:time)
  276. assert ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:time)
  277. end
  278. def test_acts_like_date
  279. assert_equal false, @twz.acts_like?(:date)
  280. assert_equal false, ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:date)
  281. end
  282. def test_is_a
  283. assert_kind_of Time, @twz
  284. assert_kind_of Time, @twz
  285. assert_kind_of ActiveSupport::TimeWithZone, @twz
  286. end
  287. def test_class_name
  288. assert_equal 'Time', ActiveSupport::TimeWithZone.name
  289. end
  290. def test_method_missing_with_time_return_value
  291. assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1)
  292. assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time
  293. end
  294. def test_marshal_dump_and_load
  295. marshal_str = Marshal.dump(@twz)
  296. mtime = Marshal.load(marshal_str)
  297. assert_equal Time.utc(2000, 1, 1, 0), mtime.utc
  298. assert mtime.utc.utc?
  299. assert_equal ActiveSupport::TimeZone['Eastern Time (US & Canada)'], mtime.time_zone
  300. assert_equal Time.utc(1999, 12, 31, 19), mtime.time
  301. assert mtime.time.utc?
  302. assert_equal @twz.inspect, mtime.inspect
  303. end
  304. def test_marshal_dump_and_load_with_tzinfo_identifier
  305. twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York'))
  306. marshal_str = Marshal.dump(twz)
  307. mtime = Marshal.load(marshal_str)
  308. assert_equal Time.utc(2000, 1, 1, 0), mtime.utc
  309. assert mtime.utc.utc?
  310. assert_equal 'America/New_York', mtime.time_zone.name
  311. assert_equal Time.utc(1999, 12, 31, 19), mtime.time
  312. assert mtime.time.utc?
  313. assert_equal @twz.inspect, mtime.inspect
  314. end
  315. def test_freeze
  316. @twz.freeze
  317. assert @twz.frozen?
  318. end
  319. def test_freeze_preloads_instance_variables
  320. @twz.freeze
  321. assert_nothing_raised do
  322. @twz.period
  323. @twz.time
  324. end
  325. end
  326. def test_method_missing_with_non_time_return_value
  327. @twz.time.expects(:foo).returns('bar')
  328. assert_equal 'bar', @twz.foo
  329. end
  330. def test_date_part_value_methods
  331. twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
  332. twz.expects(:method_missing).never
  333. assert_equal 1999, twz.year
  334. assert_equal 12, twz.month
  335. assert_equal 31, twz.day
  336. assert_equal 14, twz.hour
  337. assert_equal 18, twz.min
  338. assert_equal 17, twz.sec
  339. assert_equal 500, twz.usec
  340. assert_equal 5, twz.wday
  341. assert_equal 365, twz.yday
  342. end
  343. def test_usec_returns_0_when_datetime_is_wrapped
  344. twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone)
  345. assert_equal 0, twz.usec
  346. end
  347. def test_utc_to_local_conversion_saves_period_in_instance_variable
  348. assert_nil @twz.instance_variable_get('@period')
  349. @twz.time
  350. assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period')
  351. end
  352. def test_instance_created_with_local_time_returns_correct_utc_time
  353. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19))
  354. assert_equal Time.utc(2000), twz.utc
  355. end
  356. def test_instance_created_with_local_time_enforces_spring_dst_rules
  357. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST
  358. assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM
  359. assert_equal true, twz.dst?
  360. assert_equal 'EDT', twz.zone
  361. end
  362. def test_instance_created_with_local_time_enforces_fall_dst_rules
  363. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST
  364. assert_equal Time.utc(2006,10,29,1), twz.time
  365. assert_equal true, twz.dst?
  366. assert_equal 'EDT', twz.zone
  367. end
  368. def test_ruby_19_weekday_name_query_methods
  369. ruby_19_or_greater = RUBY_VERSION >= '1.9'
  370. %w(sunday? monday? tuesday? wednesday? thursday? friday? saturday?).each do |name|
  371. assert_equal ruby_19_or_greater, @twz.respond_to?(name)
  372. end
  373. end
  374. def test_utc_to_local_conversion_with_far_future_datetime
  375. assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6]
  376. end
  377. def test_local_to_utc_conversion_with_far_future_datetime
  378. assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f
  379. end
  380. def test_change
  381. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  382. assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.change(:year => 2001).inspect
  383. assert_equal "Wed, 31 Mar 1999 19:00:00 EST -05:00", @twz.change(:month => 3).inspect
  384. assert_equal "Wed, 03 Mar 1999 19:00:00 EST -05:00", @twz.change(:month => 2).inspect
  385. assert_equal "Wed, 15 Dec 1999 19:00:00 EST -05:00", @twz.change(:day => 15).inspect
  386. assert_equal "Fri, 31 Dec 1999 06:00:00 EST -05:00", @twz.change(:hour => 6).inspect
  387. assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.change(:min => 15).inspect
  388. assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.change(:sec => 30).inspect
  389. end
  390. def test_advance
  391. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  392. assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.advance(:years => 2).inspect
  393. assert_equal "Fri, 31 Mar 2000 19:00:00 EST -05:00", @twz.advance(:months => 3).inspect
  394. assert_equal "Tue, 04 Jan 2000 19:00:00 EST -05:00", @twz.advance(:days => 4).inspect
  395. assert_equal "Sat, 01 Jan 2000 01:00:00 EST -05:00", @twz.advance(:hours => 6).inspect
  396. assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.advance(:minutes => 15).inspect
  397. assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.advance(:seconds => 30).inspect
  398. end
  399. def beginning_of_year
  400. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  401. assert_equal "Fri, 01 Jan 1999 00:00:00 EST -05:00", @twz.beginning_of_year.inspect
  402. end
  403. def end_of_year
  404. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  405. assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_year.inspect
  406. end
  407. def beginning_of_month
  408. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  409. assert_equal "Fri, 01 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_month.inspect
  410. end
  411. def end_of_month
  412. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  413. assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_month.inspect
  414. end
  415. def beginning_of_day
  416. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  417. assert_equal "Fri, 31 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_day.inspect
  418. end
  419. def end_of_day
  420. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  421. assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect
  422. end
  423. def test_since
  424. assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect
  425. end
  426. def test_ago
  427. assert_equal "Fri, 31 Dec 1999 18:59:59 EST -05:00", @twz.ago(1).inspect
  428. end
  429. def test_seconds_since_midnight
  430. assert_equal 19 * 60 * 60, @twz.seconds_since_midnight
  431. end
  432. def test_advance_1_year_from_leap_day
  433. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2004,2,29))
  434. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.advance(:years => 1).inspect
  435. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.years_since(1).inspect
  436. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.since(1.year).inspect
  437. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", (twz + 1.year).inspect
  438. end
  439. def test_advance_1_month_from_last_day_of_january
  440. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2005,1,31))
  441. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.advance(:months => 1).inspect
  442. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.months_since(1).inspect
  443. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.since(1.month).inspect
  444. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", (twz + 1.month).inspect
  445. end
  446. def test_advance_1_month_from_last_day_of_january_during_leap_year
  447. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2000,1,31))
  448. assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", twz.advance(:months => 1).inspect
  449. assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", twz.months_since(1).inspect
  450. assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", twz.since(1.month).inspect
  451. assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", (twz + 1.month).inspect
  452. end
  453. def test_advance_1_month_into_spring_dst_gap
  454. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,3,2,2))
  455. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.advance(:months => 1).inspect
  456. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.months_since(1).inspect
  457. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.since(1.month).inspect
  458. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", (twz + 1.month).inspect
  459. end
  460. def test_advance_1_second_into_spring_dst_gap
  461. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,1,59,59))
  462. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.advance(:seconds => 1).inspect
  463. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", (twz + 1).inspect
  464. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.since(1).inspect
  465. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.since(1.second).inspect
  466. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", (twz + 1.second).inspect
  467. end
  468. def test_advance_1_day_across_spring_dst_transition
  469. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
  470. # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
  471. # When we advance 1 day, we want to end up at the same time on the next day
  472. assert_equal "Sun, 02 Apr 2006 10:30:00 EDT -04:00", twz.advance(:days => 1).inspect
  473. assert_equal "Sun, 02 Apr 2006 10:30:00 EDT -04:00", twz.since(1.days).inspect
  474. assert_equal "Sun, 02 Apr 2006 10:30:00 EDT -04:00", (twz + 1.days).inspect
  475. assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", twz.since(1.days + 1.second).inspect
  476. assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", (twz + 1.days + 1.second).inspect
  477. end
  478. def test_advance_1_day_across_spring_dst_transition_backwards
  479. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,10,30))
  480. # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
  481. # When we advance back 1 day, we want to end up at the same time on the previous day
  482. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:days => -1).inspect
  483. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(1.days).inspect
  484. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1.days).inspect
  485. assert_equal "Sat, 01 Apr 2006 10:30:01 EST -05:00", twz.ago(1.days - 1.second).inspect
  486. end
  487. def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition
  488. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
  489. # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
  490. # When we advance a specific number of hours, minutes or seconds, we want to advance exactly that amount
  491. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", (twz + 86400).inspect
  492. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", (twz + 86400.seconds).inspect
  493. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(86400).inspect
  494. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(86400.seconds).inspect
  495. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:seconds => 86400).inspect
  496. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", (twz + 1440.minutes).inspect
  497. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(1440.minutes).inspect
  498. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:minutes => 1440).inspect
  499. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", (twz + 24.hours).inspect
  500. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(24.hours).inspect
  501. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:hours => 24).inspect
  502. end
  503. def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition_backwards
  504. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,11,30))
  505. # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
  506. # When we advance a specific number of hours, minutes or seconds, we want to advance exactly that amount
  507. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 86400).inspect
  508. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 86400.seconds).inspect
  509. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(86400).inspect
  510. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(86400.seconds).inspect
  511. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:seconds => -86400).inspect
  512. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1440.minutes).inspect
  513. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(1440.minutes).inspect
  514. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:minutes => -1440).inspect
  515. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 24.hours).inspect
  516. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(24.hours).inspect
  517. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:hours => -24).inspect
  518. end
  519. def test_advance_1_day_across_fall_dst_transition
  520. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
  521. # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
  522. # When we advance 1 day, we want to end up at the same time on the next day
  523. assert_equal "Sun, 29 Oct 2006 10:30:00 EST -05:00", twz.advance(:days => 1).inspect
  524. assert_equal "Sun, 29 Oct 2006 10:30:00 EST -05:00", twz.since(1.days).inspect
  525. assert_equal "Sun, 29 Oct 2006 10:30:00 EST -05:00", (twz + 1.days).inspect
  526. assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", twz.since(1.days + 1.second).inspect
  527. assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", (twz + 1.days + 1.second).inspect
  528. end
  529. def test_advance_1_day_across_fall_dst_transition_backwards
  530. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,10,30))
  531. # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
  532. # When we advance backwards 1 day, we want to end up at the same time on the previous day
  533. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:days => -1).inspect
  534. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1.days).inspect
  535. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.days).inspect
  536. assert_equal "Sat, 28 Oct 2006 10:30:01 EDT -04:00", twz.ago(1.days - 1.second).inspect
  537. end
  538. def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition
  539. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
  540. # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
  541. # When we advance a specific number of hours, minutes or seconds, we want to advance exactly that amount
  542. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", (twz + 86400).inspect
  543. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", (twz + 86400.seconds).inspect
  544. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(86400).inspect
  545. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(86400.seconds).inspect
  546. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:seconds => 86400).inspect
  547. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", (twz + 1440.minutes).inspect
  548. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(1440.minutes).inspect
  549. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:minutes => 1440).inspect
  550. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", (twz + 24.hours).inspect
  551. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(24.hours).inspect
  552. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:hours => 24).inspect
  553. end
  554. def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition_backwards
  555. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,9,30))
  556. # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
  557. # When we advance a specific number of hours, minutes or seconds, we want to advance exactly that amount
  558. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 86400).inspect
  559. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 86400.seconds).inspect
  560. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(86400).inspect
  561. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(86400.seconds).inspect
  562. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:seconds => -86400).inspect
  563. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1440.minutes).inspect
  564. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1440.minutes).inspect
  565. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:minutes => -1440).inspect
  566. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 24.hours).inspect
  567. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(24.hours).inspect
  568. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:hours => -24).inspect
  569. end
  570. def test_advance_1_month_across_spring_dst_transition
  571. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
  572. assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", twz.advance(:months => 1).inspect
  573. assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", twz.months_since(1).inspect
  574. assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", twz.since(1.month).inspect
  575. assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", (twz + 1.month).inspect
  576. end
  577. def test_advance_1_month_across_spring_dst_transition_backwards
  578. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,5,1,10,30))
  579. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:months => -1).inspect
  580. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.months_ago(1).inspect
  581. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(1.month).inspect
  582. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1.month).inspect
  583. end
  584. def test_advance_1_month_across_fall_dst_transition
  585. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
  586. assert_equal "Tue, 28 Nov 2006 10:30:00 EST -05:00", twz.advance(:months => 1).inspect
  587. assert_equal "Tue, 28 Nov 2006 10:30:00 EST -05:00", twz.months_since(1).inspect
  588. assert_equal "Tue, 28 Nov 2006 10:30:00 EST -05:00", twz.since(1.month).inspect
  589. assert_equal "Tue, 28 Nov 2006 10:30:00 EST -05:00", (twz + 1.month).inspect
  590. end
  591. def test_advance_1_month_across_fall_dst_transition_backwards
  592. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,11,28,10,30))
  593. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:months => -1).inspect
  594. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.months_ago(1).inspect
  595. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1.month).inspect
  596. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.month).inspect
  597. end
  598. def test_advance_1_year
  599. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,2,15,10,30))
  600. assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", twz.advance(:years => 1).inspect
  601. assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", twz.years_since(1).inspect
  602. assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", (twz + 1.year).inspect
  603. assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", twz.advance(:years => -1).inspect
  604. assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", twz.years_ago(1).inspect
  605. assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", (twz - 1.year).inspect
  606. end
  607. def test_advance_1_year_during_dst
  608. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,7,15,10,30))
  609. assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", twz.advance(:years => 1).inspect
  610. assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", twz.years_since(1).inspect
  611. assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", (twz + 1.year).inspect
  612. assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", twz.advance(:years => -1).inspect
  613. assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", twz.years_ago(1).inspect
  614. assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", (twz - 1.year).inspect
  615. end
  616. protected
  617. def with_env_tz(new_tz = 'US/Eastern')
  618. old_tz, ENV['TZ'] = ENV['TZ'], new_tz
  619. yield
  620. ensure
  621. old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  622. end
  623. end
  624. class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase
  625. def setup
  626. @t, @dt = Time.utc(2000), DateTime.civil(2000)
  627. end
  628. def teardown
  629. Time.zone = nil
  630. end
  631. def test_in_time_zone
  632. Time.use_zone 'Alaska' do
  633. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect
  634. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect
  635. end
  636. Time.use_zone 'Hawaii' do
  637. assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect
  638. assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect
  639. end
  640. Time.use_zone nil do
  641. assert_equal @t, @t.in_time_zone
  642. assert_equal @dt, @dt.in_time_zone
  643. end
  644. end
  645. def test_nil_time_zone
  646. Time.use_zone nil do
  647. assert !@t.in_time_zone.respond_to?(:period), 'no period method'
  648. assert !@dt.in_time_zone.respond_to?(:period), 'no period method'
  649. end
  650. end
  651. def test_in_time_zone_with_argument
  652. Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone)
  653. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect
  654. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect
  655. assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect
  656. assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect
  657. assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect
  658. assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect
  659. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone(-9.hours).inspect
  660. end
  661. end
  662. def test_in_time_zone_with_invalid_argument
  663. assert_raise(ArgumentError) { @t.in_time_zone("No such timezone exists") }
  664. assert_raise(ArgumentError) { @dt.in_time_zone("No such timezone exists") }
  665. assert_raise(ArgumentError) { @t.in_time_zone(-15.hours) }
  666. assert_raise(ArgumentError) { @dt.in_time_zone(-15.hours) }
  667. assert_raise(ArgumentError) { @t.in_time_zone(Object.new) }
  668. assert_raise(ArgumentError) { @dt.in_time_zone(Object.new) }
  669. end
  670. def test_in_time_zone_with_time_local_instance
  671. with_env_tz 'US/Eastern' do
  672. time = Time.local(1999, 12, 31, 19) # == Time.utc(2000)
  673. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', time.in_time_zone('Alaska').inspect
  674. end
  675. end
  676. def test_localtime
  677. Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  678. assert_equal @dt.in_time_zone.localtime, @dt.in_time_zone.utc.to_time.getlocal
  679. ensure
  680. Time.zone = nil
  681. end
  682. def test_use_zone
  683. Time.zone = 'Alaska'
  684. Time.use_zone 'Hawaii' do
  685. assert_equal ActiveSupport::TimeZone['Hawaii'], Time.zone
  686. end
  687. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  688. end
  689. def test_use_zone_with_exception_raised
  690. Time.zone = 'Alaska'
  691. assert_raise RuntimeError do
  692. Time.use_zone('Hawaii') { raise RuntimeError }
  693. end
  694. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  695. end
  696. def test_use_zone_raises_on_invalid_timezone
  697. Time.zone = 'Alaska'
  698. assert_raise ArgumentError do
  699. Time.use_zone("No such timezone exists") { }
  700. end
  701. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  702. end
  703. def test_time_zone_getter_and_setter
  704. Time.zone = ActiveSupport::TimeZone['Alaska']
  705. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  706. Time.zone = 'Alaska'
  707. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  708. Time.zone = -9.hours
  709. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  710. Time.zone = nil
  711. assert_equal nil, Time.zone
  712. end
  713. def test_time_zone_getter_and_setter_with_zone_default_set
  714. Time.zone_default = ActiveSupport::TimeZone['Alaska']
  715. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  716. Time.zone = ActiveSupport::TimeZone['Hawaii']
  717. assert_equal ActiveSupport::TimeZone['Hawaii'], Time.zone
  718. Time.zone = nil
  719. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  720. ensure
  721. Time.zone = nil
  722. Time.zone_default = nil
  723. end
  724. def test_time_zone_setter_is_thread_safe
  725. Time.use_zone 'Paris' do
  726. t1 = Thread.new { Time.zone = 'Alaska' }.join
  727. t2 = Thread.new { Time.zone = 'Hawaii' }.join
  728. assert t1.stop?, "Thread 1 did not finish running"
  729. assert t2.stop?, "Thread 2 did not finish running"
  730. assert_equal ActiveSupport::TimeZone['Paris'], Time.zone
  731. assert_equal ActiveSupport::TimeZone['Alaska'], t1[:time_zone]
  732. assert_equal ActiveSupport::TimeZone['Hawaii'], t2[:time_zone]
  733. end
  734. end
  735. def test_time_zone_setter_with_tzinfo_timezone_object_wraps_in_rails_time_zone
  736. tzinfo = TZInfo::Timezone.get('America/New_York')
  737. Time.zone = tzinfo
  738. assert_kind_of ActiveSupport::TimeZone, Time.zone
  739. assert_equal tzinfo, Time.zone.tzinfo
  740. assert_equal 'America/New_York', Time.zone.name
  741. assert_equal(-18_000, Time.zone.utc_offset)
  742. end
  743. def test_time_zone_setter_with_tzinfo_timezone_identifier_does_lookup_and_wraps_in_rails_time_zone
  744. Time.zone = 'America/New_York'
  745. assert_kind_of ActiveSupport::TimeZone, Time.zone
  746. assert_equal 'America/New_York', Time.zone.tzinfo.name
  747. assert_equal 'America/New_York', Time.zone.name
  748. assert_equal(-18_000, Time.zone.utc_offset)
  749. end
  750. def test_time_zone_setter_with_invalid_zone
  751. assert_raise(ArgumentError){ Time.zone = "No such timezone exists" }
  752. assert_raise(ArgumentError){ Time.zone = -15.hours }
  753. assert_raise(ArgumentError){ Time.zone = Object.new }
  754. end
  755. def test_find_zone_without_bang_returns_nil_if_time_zone_can_not_be_found
  756. assert_nil Time.find_zone('No such timezone exists')
  757. assert_nil Time.find_zone(-15.hours)
  758. assert_nil Time.find_zone(Object.new)
  759. end
  760. def test_find_zone_with_bang_raises_if_time_zone_can_not_be_found
  761. assert_raise(ArgumentError) { Time.find_zone!('No such timezone exists') }
  762. assert_raise(ArgumentError) { Time.find_zone!(-15.hours) }
  763. assert_raise(ArgumentError) { Time.find_zone!(Object.new) }
  764. end
  765. def test_time_zone_setter_with_find_zone_without_bang
  766. assert_nil Time.zone = Time.find_zone('No such timezone exists')
  767. assert_nil Time.zone = Time.find_zone(-15.hours)
  768. assert_nil Time.zone = Time.find_zone(Object.new)
  769. end
  770. def test_current_returns_time_now_when_zone_not_set
  771. with_env_tz 'US/Eastern' do
  772. Time.stubs(:now).returns Time.local(2000)
  773. assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone)
  774. assert_equal Time.local(2000), Time.current
  775. end
  776. end
  777. def test_current_returns_time_zone_now_when_zone_set
  778. Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  779. with_env_tz 'US/Eastern' do
  780. Time.stubs(:now).returns Time.local(2000)
  781. assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone)
  782. assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name
  783. assert_equal Time.utc(2000), Time.current.time
  784. end
  785. ensure
  786. Time.zone = nil
  787. end
  788. protected
  789. def with_env_tz(new_tz = 'US/Eastern')
  790. old_tz, ENV['TZ'] = ENV['TZ'], new_tz
  791. yield
  792. ensure
  793. old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  794. end
  795. end