PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/time_with_zone_test.rb

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