PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/Ruby/Tests/Libraries/Rails-3.0.0/activesupport/test/core_ext/time_with_zone_test.rb

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