PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/time_with_zone_test.rb

https://bitbucket.org/druly/rails_cherry_pick
Ruby | 947 lines | 793 code | 134 blank | 20 comment | 0 complexity | 298a300a27e1dd7e55c3f11831c6bc31 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_equal true, @twz.eql?(Time.utc(2000))
  166. assert_equal true, @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
  167. assert_equal false, @twz.eql?( Time.utc(2000, 1, 1, 0, 0, 1) )
  168. assert_equal false, @twz.eql?( DateTime.civil(1999, 12, 31, 23, 59, 59) )
  169. end
  170. def test_hash
  171. assert_equal Time.utc(2000).hash, @twz.hash
  172. assert_equal Time.utc(2000).hash, ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]).hash
  173. end
  174. def test_plus_with_integer
  175. assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time
  176. end
  177. def test_plus_with_integer_when_self_wraps_datetime
  178. datetime = DateTime.civil(2000, 1, 1, 0)
  179. twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
  180. assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time
  181. end
  182. def test_plus_when_crossing_time_class_limit
  183. twz = ActiveSupport::TimeWithZone.new(Time.utc(2038, 1, 19), @time_zone)
  184. assert_equal [0, 0, 19, 19, 1, 2038], (twz + 86_400).to_a[0,6]
  185. end
  186. def test_plus_with_duration
  187. assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time
  188. end
  189. def test_minus_with_integer
  190. assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time
  191. end
  192. def test_minus_with_integer_when_self_wraps_datetime
  193. datetime = DateTime.civil(2000, 1, 1, 0)
  194. twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
  195. assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time
  196. end
  197. def test_minus_with_duration
  198. assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time
  199. end
  200. def test_minus_with_time
  201. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1)
  202. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['Hawaii'] ) - Time.utc(2000, 1, 1)
  203. end
  204. def test_minus_with_time_with_zone
  205. twz1 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] )
  206. twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] )
  207. assert_equal 86_400.0, twz2 - twz1
  208. end
  209. def test_minus_with_datetime
  210. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1)
  211. end
  212. def test_minus_with_wrapped_datetime
  213. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1)
  214. assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1)
  215. end
  216. def test_plus_and_minus_enforce_spring_dst_rules
  217. utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start
  218. twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
  219. assert_equal Time.utc(2006,4,2,1,59,59), twz.time
  220. assert_equal false, twz.dst?
  221. assert_equal 'EST', twz.zone
  222. twz = twz + 1
  223. assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT
  224. assert_equal true, twz.dst?
  225. assert_equal 'EDT', twz.zone
  226. twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST
  227. assert_equal Time.utc(2006,4,2,1,59,59), twz.time
  228. assert_equal false, twz.dst?
  229. assert_equal 'EST', twz.zone
  230. end
  231. def test_plus_and_minus_enforce_fall_dst_rules
  232. utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end
  233. twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
  234. assert_equal Time.utc(2006,10,29,1,59,59), twz.time
  235. assert_equal true, twz.dst?
  236. assert_equal 'EDT', twz.zone
  237. twz = twz + 1
  238. assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST
  239. assert_equal false, twz.dst?
  240. assert_equal 'EST', twz.zone
  241. twz = twz - 1
  242. assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT
  243. assert_equal true, twz.dst?
  244. assert_equal 'EDT', twz.zone
  245. end
  246. def test_to_a
  247. 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
  248. end
  249. def test_to_f
  250. result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Hawaii'] ).to_f
  251. assert_equal 946684800.0, result
  252. assert_kind_of Float, result
  253. end
  254. def test_to_i
  255. result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Hawaii'] ).to_i
  256. assert_equal 946684800, result
  257. assert_kind_of Integer, result
  258. end
  259. def test_to_i_with_wrapped_datetime
  260. datetime = DateTime.civil(2000, 1, 1, 0)
  261. twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone)
  262. assert_equal 946684800, twz.to_i
  263. end
  264. def test_to_time
  265. assert_equal @twz, @twz.to_time
  266. end
  267. def test_to_date
  268. # 1 sec before midnight Jan 1 EST
  269. 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
  270. # midnight Jan 1 EST
  271. 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
  272. # 1 sec before midnight Jan 2 EST
  273. 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
  274. # midnight Jan 2 EST
  275. 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
  276. end
  277. def test_to_datetime
  278. assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime
  279. end
  280. def test_acts_like_time
  281. assert @twz.acts_like?(:time)
  282. assert ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:time)
  283. end
  284. def test_acts_like_date
  285. assert_equal false, @twz.acts_like?(:date)
  286. assert_equal false, ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:date)
  287. end
  288. def test_is_a
  289. assert_kind_of Time, @twz
  290. assert_kind_of Time, @twz
  291. assert_kind_of ActiveSupport::TimeWithZone, @twz
  292. end
  293. def test_class_name
  294. assert_equal 'Time', ActiveSupport::TimeWithZone.name
  295. end
  296. def test_method_missing_with_time_return_value
  297. assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1)
  298. assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time
  299. end
  300. def test_marshal_dump_and_load
  301. marshal_str = Marshal.dump(@twz)
  302. mtime = Marshal.load(marshal_str)
  303. assert_equal Time.utc(2000, 1, 1, 0), mtime.utc
  304. assert mtime.utc.utc?
  305. assert_equal ActiveSupport::TimeZone['Eastern Time (US & Canada)'], mtime.time_zone
  306. assert_equal Time.utc(1999, 12, 31, 19), mtime.time
  307. assert mtime.time.utc?
  308. assert_equal @twz.inspect, mtime.inspect
  309. end
  310. def test_marshal_dump_and_load_with_tzinfo_identifier
  311. twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York'))
  312. marshal_str = Marshal.dump(twz)
  313. mtime = Marshal.load(marshal_str)
  314. assert_equal Time.utc(2000, 1, 1, 0), mtime.utc
  315. assert mtime.utc.utc?
  316. assert_equal 'America/New_York', mtime.time_zone.name
  317. assert_equal Time.utc(1999, 12, 31, 19), mtime.time
  318. assert mtime.time.utc?
  319. assert_equal @twz.inspect, mtime.inspect
  320. end
  321. def test_freeze
  322. @twz.freeze
  323. assert @twz.frozen?
  324. end
  325. def test_freeze_preloads_instance_variables
  326. @twz.freeze
  327. assert_nothing_raised do
  328. @twz.period
  329. @twz.time
  330. end
  331. end
  332. def test_method_missing_with_non_time_return_value
  333. @twz.time.expects(:foo).returns('bar')
  334. assert_equal 'bar', @twz.foo
  335. end
  336. def test_date_part_value_methods
  337. twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
  338. twz.expects(:method_missing).never
  339. assert_equal 1999, twz.year
  340. assert_equal 12, twz.month
  341. assert_equal 31, twz.day
  342. assert_equal 14, twz.hour
  343. assert_equal 18, twz.min
  344. assert_equal 17, twz.sec
  345. assert_equal 500, twz.usec
  346. assert_equal 5, twz.wday
  347. assert_equal 365, twz.yday
  348. end
  349. def test_usec_returns_0_when_datetime_is_wrapped
  350. twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone)
  351. assert_equal 0, twz.usec
  352. end
  353. def test_utc_to_local_conversion_saves_period_in_instance_variable
  354. assert_nil @twz.instance_variable_get('@period')
  355. @twz.time
  356. assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period')
  357. end
  358. def test_instance_created_with_local_time_returns_correct_utc_time
  359. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19))
  360. assert_equal Time.utc(2000), twz.utc
  361. end
  362. def test_instance_created_with_local_time_enforces_spring_dst_rules
  363. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST
  364. assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM
  365. assert_equal true, twz.dst?
  366. assert_equal 'EDT', twz.zone
  367. end
  368. def test_instance_created_with_local_time_enforces_fall_dst_rules
  369. 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
  370. assert_equal Time.utc(2006,10,29,1), twz.time
  371. assert_equal true, twz.dst?
  372. assert_equal 'EDT', twz.zone
  373. end
  374. def test_ruby_19_weekday_name_query_methods
  375. ruby_19_or_greater = RUBY_VERSION >= '1.9'
  376. %w(sunday? monday? tuesday? wednesday? thursday? friday? saturday?).each do |name|
  377. assert_equal ruby_19_or_greater, @twz.respond_to?(name)
  378. end
  379. end
  380. def test_utc_to_local_conversion_with_far_future_datetime
  381. assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6]
  382. end
  383. def test_local_to_utc_conversion_with_far_future_datetime
  384. assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f
  385. end
  386. def test_change
  387. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  388. assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.change(:year => 2001).inspect
  389. assert_equal "Wed, 31 Mar 1999 19:00:00 EST -05:00", @twz.change(:month => 3).inspect
  390. assert_equal "Wed, 03 Mar 1999 19:00:00 EST -05:00", @twz.change(:month => 2).inspect
  391. assert_equal "Wed, 15 Dec 1999 19:00:00 EST -05:00", @twz.change(:day => 15).inspect
  392. assert_equal "Fri, 31 Dec 1999 06:00:00 EST -05:00", @twz.change(:hour => 6).inspect
  393. assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.change(:min => 15).inspect
  394. assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.change(:sec => 30).inspect
  395. end
  396. def test_advance
  397. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  398. assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.advance(:years => 2).inspect
  399. assert_equal "Fri, 31 Mar 2000 19:00:00 EST -05:00", @twz.advance(:months => 3).inspect
  400. assert_equal "Tue, 04 Jan 2000 19:00:00 EST -05:00", @twz.advance(:days => 4).inspect
  401. assert_equal "Sat, 01 Jan 2000 01:00:00 EST -05:00", @twz.advance(:hours => 6).inspect
  402. assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.advance(:minutes => 15).inspect
  403. assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.advance(:seconds => 30).inspect
  404. end
  405. def beginning_of_year
  406. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  407. assert_equal "Fri, 01 Jan 1999 00:00:00 EST -05:00", @twz.beginning_of_year.inspect
  408. end
  409. def end_of_year
  410. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  411. assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_year.inspect
  412. end
  413. def beginning_of_month
  414. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  415. assert_equal "Fri, 01 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_month.inspect
  416. end
  417. def end_of_month
  418. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  419. assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_month.inspect
  420. end
  421. def beginning_of_day
  422. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  423. assert_equal "Fri, 31 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_day.inspect
  424. end
  425. def end_of_day
  426. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect
  427. assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect
  428. end
  429. def test_beginning_of_hour
  430. utc = Time.utc(2000, 1, 1, 0, 30)
  431. twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
  432. assert_equal "Fri, 31 Dec 1999 19:30:00 EST -05:00", twz.inspect
  433. assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", twz.beginning_of_hour.inspect
  434. end
  435. def test_end_of_hour
  436. utc = Time.utc(2000, 1, 1, 0, 30)
  437. twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
  438. assert_equal "Fri, 31 Dec 1999 19:30:00 EST -05:00", twz.inspect
  439. assert_equal "Fri, 31 Dec 1999 19:59:59 EST -05:00", twz.end_of_hour.inspect
  440. end
  441. def test_since
  442. assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect
  443. end
  444. def test_ago
  445. assert_equal "Fri, 31 Dec 1999 18:59:59 EST -05:00", @twz.ago(1).inspect
  446. end
  447. def test_seconds_since_midnight
  448. assert_equal 19 * 60 * 60, @twz.seconds_since_midnight
  449. end
  450. def test_advance_1_year_from_leap_day
  451. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2004,2,29))
  452. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.advance(:years => 1).inspect
  453. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.years_since(1).inspect
  454. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.since(1.year).inspect
  455. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", (twz + 1.year).inspect
  456. end
  457. def test_advance_1_month_from_last_day_of_january
  458. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2005,1,31))
  459. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.advance(:months => 1).inspect
  460. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.months_since(1).inspect
  461. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.since(1.month).inspect
  462. assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", (twz + 1.month).inspect
  463. end
  464. def test_advance_1_month_from_last_day_of_january_during_leap_year
  465. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2000,1,31))
  466. assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", twz.advance(:months => 1).inspect
  467. assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", twz.months_since(1).inspect
  468. assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", twz.since(1.month).inspect
  469. assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", (twz + 1.month).inspect
  470. end
  471. def test_advance_1_month_into_spring_dst_gap
  472. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,3,2,2))
  473. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.advance(:months => 1).inspect
  474. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.months_since(1).inspect
  475. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.since(1.month).inspect
  476. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", (twz + 1.month).inspect
  477. end
  478. def test_advance_1_second_into_spring_dst_gap
  479. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,1,59,59))
  480. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.advance(:seconds => 1).inspect
  481. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", (twz + 1).inspect
  482. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.since(1).inspect
  483. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.since(1.second).inspect
  484. assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", (twz + 1.second).inspect
  485. end
  486. def test_advance_1_day_across_spring_dst_transition
  487. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
  488. # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
  489. # When we advance 1 day, we want to end up at the same time on the next day
  490. assert_equal "Sun, 02 Apr 2006 10:30:00 EDT -04:00", twz.advance(:days => 1).inspect
  491. assert_equal "Sun, 02 Apr 2006 10:30:00 EDT -04:00", twz.since(1.days).inspect
  492. assert_equal "Sun, 02 Apr 2006 10:30:00 EDT -04:00", (twz + 1.days).inspect
  493. assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", twz.since(1.days + 1.second).inspect
  494. assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", (twz + 1.days + 1.second).inspect
  495. end
  496. def test_advance_1_day_across_spring_dst_transition_backwards
  497. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,10,30))
  498. # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
  499. # When we advance back 1 day, we want to end up at the same time on the previous day
  500. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:days => -1).inspect
  501. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(1.days).inspect
  502. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1.days).inspect
  503. assert_equal "Sat, 01 Apr 2006 10:30:01 EST -05:00", twz.ago(1.days - 1.second).inspect
  504. end
  505. def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition
  506. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
  507. # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
  508. # When we advance a specific number of hours, minutes or seconds, we want to advance exactly that amount
  509. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", (twz + 86400).inspect
  510. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", (twz + 86400.seconds).inspect
  511. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(86400).inspect
  512. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(86400.seconds).inspect
  513. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:seconds => 86400).inspect
  514. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", (twz + 1440.minutes).inspect
  515. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(1440.minutes).inspect
  516. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:minutes => 1440).inspect
  517. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", (twz + 24.hours).inspect
  518. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(24.hours).inspect
  519. assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:hours => 24).inspect
  520. end
  521. def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition_backwards
  522. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,11,30))
  523. # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
  524. # When we advance a specific number of hours, minutes or seconds, we want to advance exactly that amount
  525. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 86400).inspect
  526. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 86400.seconds).inspect
  527. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(86400).inspect
  528. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(86400.seconds).inspect
  529. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:seconds => -86400).inspect
  530. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1440.minutes).inspect
  531. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(1440.minutes).inspect
  532. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:minutes => -1440).inspect
  533. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 24.hours).inspect
  534. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(24.hours).inspect
  535. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:hours => -24).inspect
  536. end
  537. def test_advance_1_day_across_fall_dst_transition
  538. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
  539. # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
  540. # When we advance 1 day, we want to end up at the same time on the next day
  541. assert_equal "Sun, 29 Oct 2006 10:30:00 EST -05:00", twz.advance(:days => 1).inspect
  542. assert_equal "Sun, 29 Oct 2006 10:30:00 EST -05:00", twz.since(1.days).inspect
  543. assert_equal "Sun, 29 Oct 2006 10:30:00 EST -05:00", (twz + 1.days).inspect
  544. assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", twz.since(1.days + 1.second).inspect
  545. assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", (twz + 1.days + 1.second).inspect
  546. end
  547. def test_advance_1_day_across_fall_dst_transition_backwards
  548. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,10,30))
  549. # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
  550. # When we advance backwards 1 day, we want to end up at the same time on the previous day
  551. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:days => -1).inspect
  552. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1.days).inspect
  553. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.days).inspect
  554. assert_equal "Sat, 28 Oct 2006 10:30:01 EDT -04:00", twz.ago(1.days - 1.second).inspect
  555. end
  556. def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition
  557. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
  558. # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
  559. # When we advance a specific number of hours, minutes or seconds, we want to advance exactly that amount
  560. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", (twz + 86400).inspect
  561. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", (twz + 86400.seconds).inspect
  562. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(86400).inspect
  563. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(86400.seconds).inspect
  564. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:seconds => 86400).inspect
  565. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", (twz + 1440.minutes).inspect
  566. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(1440.minutes).inspect
  567. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:minutes => 1440).inspect
  568. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", (twz + 24.hours).inspect
  569. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(24.hours).inspect
  570. assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:hours => 24).inspect
  571. end
  572. def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition_backwards
  573. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,9,30))
  574. # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
  575. # When we advance a specific number of hours, minutes or seconds, we want to advance exactly that amount
  576. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 86400).inspect
  577. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 86400.seconds).inspect
  578. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(86400).inspect
  579. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(86400.seconds).inspect
  580. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:seconds => -86400).inspect
  581. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1440.minutes).inspect
  582. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1440.minutes).inspect
  583. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:minutes => -1440).inspect
  584. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 24.hours).inspect
  585. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(24.hours).inspect
  586. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:hours => -24).inspect
  587. end
  588. def test_advance_1_month_across_spring_dst_transition
  589. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
  590. assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", twz.advance(:months => 1).inspect
  591. assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", twz.months_since(1).inspect
  592. assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", twz.since(1.month).inspect
  593. assert_equal "Mon, 01 May 2006 10:30:00 EDT -04:00", (twz + 1.month).inspect
  594. end
  595. def test_advance_1_month_across_spring_dst_transition_backwards
  596. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,5,1,10,30))
  597. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:months => -1).inspect
  598. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.months_ago(1).inspect
  599. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(1.month).inspect
  600. assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1.month).inspect
  601. end
  602. def test_advance_1_month_across_fall_dst_transition
  603. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
  604. assert_equal "Tue, 28 Nov 2006 10:30:00 EST -05:00", twz.advance(:months => 1).inspect
  605. assert_equal "Tue, 28 Nov 2006 10:30:00 EST -05:00", twz.months_since(1).inspect
  606. assert_equal "Tue, 28 Nov 2006 10:30:00 EST -05:00", twz.since(1.month).inspect
  607. assert_equal "Tue, 28 Nov 2006 10:30:00 EST -05:00", (twz + 1.month).inspect
  608. end
  609. def test_advance_1_month_across_fall_dst_transition_backwards
  610. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,11,28,10,30))
  611. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.advance(:months => -1).inspect
  612. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.months_ago(1).inspect
  613. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1.month).inspect
  614. assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.month).inspect
  615. end
  616. def test_advance_1_year
  617. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,2,15,10,30))
  618. assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", twz.advance(:years => 1).inspect
  619. assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", twz.years_since(1).inspect
  620. assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", (twz + 1.year).inspect
  621. assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", twz.advance(:years => -1).inspect
  622. assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", twz.years_ago(1).inspect
  623. assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", (twz - 1.year).inspect
  624. end
  625. def test_advance_1_year_during_dst
  626. twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,7,15,10,30))
  627. assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", twz.advance(:years => 1).inspect
  628. assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", twz.years_since(1).inspect
  629. assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", (twz + 1.year).inspect
  630. assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", twz.advance(:years => -1).inspect
  631. assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", twz.years_ago(1).inspect
  632. assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", (twz - 1.year).inspect
  633. end
  634. protected
  635. def with_env_tz(new_tz = 'US/Eastern')
  636. old_tz, ENV['TZ'] = ENV['TZ'], new_tz
  637. yield
  638. ensure
  639. old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  640. end
  641. end
  642. class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase
  643. def setup
  644. @t, @dt = Time.utc(2000), DateTime.civil(2000)
  645. end
  646. def teardown
  647. Time.zone = nil
  648. end
  649. def test_in_time_zone
  650. Time.use_zone 'Alaska' do
  651. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect
  652. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect
  653. end
  654. Time.use_zone 'Hawaii' do
  655. assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect
  656. assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect
  657. end
  658. Time.use_zone nil do
  659. assert_equal @t, @t.in_time_zone
  660. assert_equal @dt, @dt.in_time_zone
  661. end
  662. end
  663. def test_nil_time_zone
  664. Time.use_zone nil do
  665. assert !@t.in_time_zone.respond_to?(:period), 'no period method'
  666. assert !@dt.in_time_zone.respond_to?(:period), 'no period method'
  667. end
  668. end
  669. def test_in_time_zone_with_argument
  670. Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone)
  671. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect
  672. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect
  673. assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect
  674. assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect
  675. assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect
  676. assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect
  677. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone(-9.hours).inspect
  678. end
  679. end
  680. def test_in_time_zone_with_invalid_argument
  681. assert_raise(ArgumentError) { @t.in_time_zone("No such timezone exists") }
  682. assert_raise(ArgumentError) { @dt.in_time_zone("No such timezone exists") }
  683. assert_raise(ArgumentError) { @t.in_time_zone(-15.hours) }
  684. assert_raise(ArgumentError) { @dt.in_time_zone(-15.hours) }
  685. assert_raise(ArgumentError) { @t.in_time_zone(Object.new) }
  686. assert_raise(ArgumentError) { @dt.in_time_zone(Object.new) }
  687. end
  688. def test_in_time_zone_with_time_local_instance
  689. with_env_tz 'US/Eastern' do
  690. time = Time.local(1999, 12, 31, 19) # == Time.utc(2000)
  691. assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', time.in_time_zone('Alaska').inspect
  692. end
  693. end
  694. def test_localtime
  695. Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  696. assert_equal @dt.in_time_zone.localtime, @dt.in_time_zone.utc.to_time.getlocal
  697. ensure
  698. Time.zone = nil
  699. end
  700. def test_use_zone
  701. Time.zone = 'Alaska'
  702. Time.use_zone 'Hawaii' do
  703. assert_equal ActiveSupport::TimeZone['Hawaii'], Time.zone
  704. end
  705. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  706. end
  707. def test_use_zone_with_exception_raised
  708. Time.zone = 'Alaska'
  709. assert_raise RuntimeError do
  710. Time.use_zone('Hawaii') { raise RuntimeError }
  711. end
  712. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  713. end
  714. def test_use_zone_raises_on_invalid_timezone
  715. Time.zone = 'Alaska'
  716. assert_raise ArgumentError do
  717. Time.use_zone("No such timezone exists") { }
  718. end
  719. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  720. end
  721. def test_time_zone_getter_and_setter
  722. Time.zone = ActiveSupport::TimeZone['Alaska']
  723. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  724. Time.zone = 'Alaska'
  725. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  726. Time.zone = -9.hours
  727. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  728. Time.zone = nil
  729. assert_equal nil, Time.zone
  730. end
  731. def test_time_zone_getter_and_setter_with_zone_default_set
  732. Time.zone_default = ActiveSupport::TimeZone['Alaska']
  733. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  734. Time.zone = ActiveSupport::TimeZone['Hawaii']
  735. assert_equal ActiveSupport::TimeZone['Hawaii'], Time.zone
  736. Time.zone = nil
  737. assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone
  738. ensure
  739. Time.zone = nil
  740. Time.zone_default = nil
  741. end
  742. def test_time_zone_setter_is_thread_safe
  743. Time.use_zone 'Paris' do
  744. t1 = Thread.new { Time.zone = 'Alaska' }.join
  745. t2 = Thread.new { Time.zone = 'Hawaii' }.join
  746. assert t1.stop?, "Thread 1 did not finish running"
  747. assert t2.stop?, "Thread 2 did not finish running"
  748. assert_equal ActiveSupport::TimeZone['Paris'], Time.zone
  749. assert_equal ActiveSupport::TimeZone['Alaska'], t1[:time_zone]
  750. assert_equal ActiveSupport::TimeZone['Hawaii'], t2[:time_zone]
  751. end
  752. end
  753. def test_time_zone_setter_with_tzinfo_timezone_object_wraps_in_rails_time_zone
  754. tzinfo = TZInfo::Timezone.get('America/New_York')
  755. Time.zone = tzinfo
  756. assert_kind_of ActiveSupport::TimeZone, Time.zone
  757. assert_equal tzinfo, Time.zone.tzinfo
  758. assert_equal 'America/New_York', Time.zone.name
  759. assert_equal(-18_000, Time.zone.utc_offset)
  760. end
  761. def test_time_zone_setter_with_tzinfo_timezone_identifier_does_lookup_and_wraps_in_rails_time_zone
  762. Time.zone = 'America/New_York'
  763. assert_kind_of ActiveSupport::TimeZone, Time.zone
  764. assert_equal 'America/New_York', Time.zone.tzinfo.name
  765. assert_equal 'America/New_York', Time.zone.name
  766. assert_equal(-18_000, Time.zone.utc_offset)
  767. end
  768. def test_time_zone_setter_with_invalid_zone
  769. assert_raise(ArgumentError){ Time.zone = "No such timezone exists" }
  770. assert_raise(ArgumentError){ Time.zone = -15.hours }
  771. assert_raise(ArgumentError){ Time.zone = Object.new }
  772. end
  773. def test_find_zone_without_bang_returns_nil_if_time_zone_can_not_be_found
  774. assert_nil Time.find_zone('No such timezone exists')
  775. assert_nil Time.find_zone(-15.hours)
  776. assert_nil Time.find_zone(Object.new)
  777. end
  778. def test_find_zone_with_bang_raises_if_time_zone_can_not_be_found
  779. assert_raise(ArgumentError) { Time.find_zone!('No such timezone exists') }
  780. assert_raise(ArgumentError) { Time.find_zone!(-15.hours) }
  781. assert_raise(ArgumentError) { Time.find_zone!(Object.new) }
  782. end
  783. def test_time_zone_setter_with_find_zone_without_bang
  784. assert_nil Time.zone = Time.find_zone('No such timezone exists')
  785. assert_nil Time.zone = Time.find_zone(-15.hours)
  786. assert_nil Time.zone = Time.find_zone(Object.new)
  787. end
  788. def test_current_returns_time_now_when_zone_not_set
  789. with_env_tz 'US/Eastern' do
  790. Time.stubs(:now).returns Time.local(2000)
  791. assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone)
  792. assert_equal Time.local(2000), Time.current
  793. end
  794. end
  795. def test_current_returns_time_zone_now_when_zone_set
  796. Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  797. with_env_tz 'US/Eastern' do
  798. Time.stubs(:now).returns Time.local(2000)
  799. assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone)
  800. assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name
  801. assert_equal Time.utc(2000), Time.current.time
  802. end
  803. ensure
  804. Time.zone = nil
  805. end
  806. protected
  807. def with_env_tz(new_tz = 'US/Eastern')
  808. old_tz, ENV['TZ'] = ENV['TZ'], new_tz
  809. yield
  810. ensure
  811. old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  812. end
  813. end