PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/training-web/vendor/bundle/gems/tzinfo-0.3.37/test/tc_timezone.rb

https://bitbucket.org/ohimmelreich/asalia-training
Ruby | 964 lines | 739 code | 206 blank | 19 comment | 6 complexity | ae6819ab42c75706da58515f7cd8dece MD5 | raw file
  1. $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
  2. require 'test/unit'
  3. require File.join(File.dirname(__FILE__), 'test_utils')
  4. require 'tzinfo'
  5. include TZInfo
  6. class TCTimezone < Test::Unit::TestCase
  7. class BlockCalled < StandardError
  8. end
  9. class TestTimezone < Timezone
  10. def self.new(identifier, period_for_utc = nil, periods_for_local = nil, expected = nil)
  11. t = super()
  12. t.send(:setup, identifier, period_for_utc, periods_for_local, expected)
  13. t
  14. end
  15. def identifier
  16. @identifier
  17. end
  18. def period_for_utc(utc)
  19. utc = TimeOrDateTime.wrap(utc)
  20. raise "Unexpected utc #{utc} in period_for_utc" unless @expected.eql?(utc)
  21. @period_for_utc
  22. end
  23. def periods_for_local(local)
  24. local = TimeOrDateTime.wrap(local)
  25. raise "Unexpected local #{local} in periods_for_local" unless @expected.eql?(local)
  26. @periods_for_local.clone
  27. end
  28. private
  29. def setup(identifier, period_for_utc, periods_for_local, expected)
  30. @identifier = identifier
  31. @period_for_utc = period_for_utc
  32. @periods_for_local = periods_for_local || []
  33. @expected = TimeOrDateTime.wrap(expected)
  34. end
  35. end
  36. def setup
  37. @orig_default_dst = Timezone.default_dst
  38. end
  39. def teardown
  40. Timezone.default_dst = @orig_default_dst
  41. end
  42. def test_default_dst_initial_value
  43. assert_nil(Timezone.default_dst)
  44. end
  45. def test_set_default_dst
  46. Timezone.default_dst = true
  47. assert_equal(true, Timezone.default_dst)
  48. Timezone.default_dst = false
  49. assert_equal(false, Timezone.default_dst)
  50. Timezone.default_dst = nil
  51. assert_nil(Timezone.default_dst)
  52. Timezone.default_dst = 0
  53. assert_equal(true, Timezone.default_dst)
  54. end
  55. def test_get_valid_1
  56. tz = Timezone.get('Europe/London')
  57. assert_kind_of(DataTimezone, tz)
  58. assert_equal('Europe/London', tz.identifier)
  59. end
  60. def test_get_valid_2
  61. tz = Timezone.get('UTC')
  62. assert_kind_of(LinkedTimezone, tz)
  63. assert_equal('UTC', tz.identifier)
  64. end
  65. def test_get_valid_3
  66. tz = Timezone.get('America/Argentina/Buenos_Aires')
  67. assert_kind_of(DataTimezone, tz)
  68. assert_equal('America/Argentina/Buenos_Aires', tz.identifier)
  69. end
  70. def test_get_same_instance
  71. tz1 = Timezone.get('Europe/London')
  72. tz2 = Timezone.get('Europe/London')
  73. assert_same(tz1, tz2)
  74. end
  75. def test_get_not_exist
  76. assert_raises(InvalidTimezoneIdentifier) { Timezone.get('Nowhere/Special') }
  77. end
  78. def test_get_invalid
  79. assert_raises(InvalidTimezoneIdentifier) { Timezone.get('../Definitions/UTC') }
  80. end
  81. def test_get_nil
  82. assert_raises(InvalidTimezoneIdentifier) { Timezone.get(nil) }
  83. end
  84. def test_get_plus
  85. tz = Timezone.get('Etc/GMT+1')
  86. assert_equal('Etc/GMT+1', tz.identifier)
  87. end
  88. def test_get_minus
  89. tz = Timezone.get('Etc/GMT-1')
  90. assert_equal('Etc/GMT-1', tz.identifier)
  91. end
  92. def test_get_case
  93. Timezone.get('Europe/Prague')
  94. assert_raises(InvalidTimezoneIdentifier) { Timezone.get('Europe/prague') }
  95. end
  96. def test_get_proxy_valid
  97. proxy = Timezone.get_proxy('Europe/London')
  98. assert_kind_of(TimezoneProxy, proxy)
  99. assert_equal('Europe/London', proxy.identifier)
  100. end
  101. def test_get_proxy_not_exist
  102. proxy = Timezone.get_proxy('Not/There')
  103. assert_kind_of(TimezoneProxy, proxy)
  104. assert_equal('Not/There', proxy.identifier)
  105. end
  106. def test_get_proxy_invalid
  107. proxy = Timezone.get_proxy('../Invalid/Identifier')
  108. assert_kind_of(TimezoneProxy, proxy)
  109. assert_equal('../Invalid/Identifier', proxy.identifier)
  110. end
  111. def test_get_tainted_loaded
  112. Timezone.get('Europe/Andorra')
  113. safe_test do
  114. tz = Timezone.get('Europe/Andorra'.taint)
  115. assert_equal('Europe/Andorra', tz.identifier)
  116. end
  117. end
  118. # Disable test in Ruby 1.9 and Ruby 2.0. See:
  119. # http://groups.google.com/group/ruby-talk-google/browse_thread/thread/170a7205555cedfc
  120. # It doesn't appear to be possible to require a file from the load path in Ruby 1.9.
  121. if RUBY_VERSION !~ /\A(1\.9|2)\./
  122. def test_get_tainted_not_loaded
  123. safe_test do
  124. tz = Timezone.get('Europe/Amsterdam'.taint)
  125. assert_equal('Europe/Amsterdam', tz.identifier)
  126. end
  127. end
  128. end
  129. def test_new_no_args
  130. tz = Timezone.new
  131. assert_raises(UnknownTimezone) { tz.identifier }
  132. assert_raises(UnknownTimezone) { tz.friendly_identifier }
  133. assert_raises(UnknownTimezone) { tz.utc_to_local(DateTime.new(2006,1,1,1,0,0)) }
  134. assert_raises(UnknownTimezone) { tz.local_to_utc(DateTime.new(2006,1,1,1,0,0)) }
  135. assert_raises(UnknownTimezone) { tz.period_for_utc(DateTime.new(2006,1,1,1,0,0)) }
  136. assert_raises(UnknownTimezone) { tz.periods_for_local(DateTime.new(2006,1,1,1,0,0)) }
  137. assert_raises(UnknownTimezone) { tz.period_for_local(DateTime.new(2006,1,1,1,0,0)) }
  138. assert_raises(UnknownTimezone) { tz.now }
  139. assert_raises(UnknownTimezone) { tz.current_period_and_time }
  140. end
  141. def test_new_nil
  142. tz = Timezone.new(nil)
  143. assert_raises(UnknownTimezone) { tz.identifier }
  144. assert_raises(UnknownTimezone) { tz.friendly_identifier }
  145. assert_raises(UnknownTimezone) { tz.utc_to_local(DateTime.new(2006,1,1,1,0,0)) }
  146. assert_raises(UnknownTimezone) { tz.local_to_utc(DateTime.new(2006,1,1,1,0,0)) }
  147. assert_raises(UnknownTimezone) { tz.period_for_utc(DateTime.new(2006,1,1,1,0,0)) }
  148. assert_raises(UnknownTimezone) { tz.periods_for_local(DateTime.new(2006,1,1,1,0,0)) }
  149. assert_raises(UnknownTimezone) { tz.period_for_local(DateTime.new(2006,1,1,1,0,0)) }
  150. assert_raises(UnknownTimezone) { tz.now }
  151. assert_raises(UnknownTimezone) { tz.current_period_and_time }
  152. end
  153. def test_new_arg
  154. tz = Timezone.new('Europe/London')
  155. assert_same(Timezone.get('Europe/London'), tz)
  156. end
  157. def test_new_arg_not_exist
  158. assert_raises(InvalidTimezoneIdentifier) { Timezone.new('Nowhere/Special') }
  159. end
  160. def test_all
  161. all = Timezone.all
  162. expected = Indexes::Timezones.timezones.collect {|identifier| Timezone.get_proxy(identifier)}
  163. assert_equal(expected, all)
  164. end
  165. def test_all_identifiers
  166. all = Timezone.all_identifiers
  167. assert_equal(Indexes::Timezones.timezones, all)
  168. end
  169. def test_all_data_zones
  170. all_data = Timezone.all_data_zones
  171. expected = Indexes::Timezones.data_timezones.collect {|identifier| Timezone.get_proxy(identifier)}
  172. assert_equal(expected, all_data)
  173. end
  174. def test_all_data_zone_identifiers
  175. all_data = Timezone.all_data_zone_identifiers
  176. assert_equal(Indexes::Timezones.data_timezones, all_data)
  177. end
  178. def test_all_linked_zones
  179. all_linked = Timezone.all_linked_zones
  180. expected = Indexes::Timezones.linked_timezones.collect {|identifier| Timezone.get_proxy(identifier)}
  181. assert_equal(expected, all_linked)
  182. end
  183. def test_all_linked_zone_identifiers
  184. all_linked = Timezone.all_linked_zone_identifiers
  185. assert_equal(Indexes::Timezones.linked_timezones, all_linked)
  186. end
  187. def test_all_country_zones
  188. # Probably should relax this test - just need all the zones, don't care
  189. # about order.
  190. expected = Country.all.inject([]) {|result,country|
  191. result += country.zones
  192. }
  193. expected.uniq!
  194. all_country_zones = Timezone.all_country_zones
  195. assert_equal(expected, all_country_zones)
  196. all_country_zone_identifiers = Timezone.all_country_zone_identifiers
  197. assert_equal(all_country_zone_identifiers.length, all_country_zones.length)
  198. all_country_zones.each {|zone|
  199. assert_kind_of(TimezoneProxy, zone)
  200. assert(all_country_zone_identifiers.include?(zone.identifier))
  201. }
  202. end
  203. def test_all_country_zone_identifiers
  204. # Probably should relax this test - just need all the zones, don't care
  205. # about order.
  206. expected = Country.all.inject([]) {|result,country|
  207. result += country.zone_identifiers
  208. }
  209. expected.uniq!
  210. assert_equal(expected, Timezone.all_country_zone_identifiers)
  211. end
  212. def test_us_zones
  213. # Probably should relax this test - just need all the zones, don't care
  214. # about order.
  215. us_zones = Timezone.us_zones
  216. assert_equal(Country.get('US').zones.uniq, us_zones)
  217. us_zone_identifiers = Timezone.us_zone_identifiers
  218. assert_equal(us_zone_identifiers.length, us_zones.length)
  219. us_zones.each {|zone|
  220. assert_kind_of(TimezoneProxy, zone)
  221. assert(us_zone_identifiers.include?(zone.identifier))
  222. }
  223. end
  224. def test_us_zone_identifiers
  225. # Probably should relax this test - just need all the zones, don't care
  226. # about order.
  227. assert_equal(Country.get('US').zone_identifiers.uniq, Timezone.us_zone_identifiers)
  228. end
  229. def test_identifier
  230. assert_raises(UnknownTimezone) { Timezone.new.identifier }
  231. assert_equal('Europe/Paris', TestTimezone.new('Europe/Paris').identifier)
  232. end
  233. def test_name
  234. assert_raises(UnknownTimezone) { Timezone.new.name }
  235. assert_equal('Europe/Paris', TestTimezone.new('Europe/Paris').name)
  236. end
  237. def test_friendly_identifier
  238. assert_equal('Paris', TestTimezone.new('Europe/Paris').friendly_identifier(true))
  239. assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').friendly_identifier(false))
  240. assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').friendly_identifier)
  241. assert_equal('Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier(true))
  242. assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier(false))
  243. assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').friendly_identifier)
  244. assert_equal('Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier(true))
  245. assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier(false))
  246. assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').friendly_identifier)
  247. assert_equal('McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier(true))
  248. assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier(false))
  249. assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').friendly_identifier)
  250. assert_equal('GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier(true))
  251. assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier(false))
  252. assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').friendly_identifier)
  253. assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier(true))
  254. assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier(false))
  255. assert_equal('UTC', TestTimezone.new('UTC').friendly_identifier)
  256. end
  257. def test_to_s
  258. assert_equal('Europe - Paris', TestTimezone.new('Europe/Paris').to_s)
  259. assert_equal('America - Knox, Indiana', TestTimezone.new('America/Indiana/Knox').to_s)
  260. assert_equal('Antarctica - Dumont D\'Urville', TestTimezone.new('Antarctica/DumontDUrville').to_s)
  261. assert_equal('Antarctica - McMurdo', TestTimezone.new('Antarctica/McMurdo').to_s)
  262. assert_equal('Etc - GMT+1', TestTimezone.new('Etc/GMT+1').to_s)
  263. assert_equal('UTC', TestTimezone.new('UTC').to_s)
  264. end
  265. def test_period_for_local
  266. dt = DateTime.new(2005,2,18,16,24,23)
  267. dt2 = DateTime.new(2005,2,18,16,24,23).new_offset(Rational(5,24))
  268. t = Time.utc(2005,2,18,16,24,23)
  269. t2 = Time.local(2005,2,18,16,24,23)
  270. ts = t.to_i
  271. o1 = TimezoneOffsetInfo.new(0, 0, :GMT)
  272. o2 = TimezoneOffsetInfo.new(0, 3600, :BST)
  273. period = TimezonePeriod.new(
  274. TimezoneTransitionInfo.new(o1, o2, 1099184400),
  275. TimezoneTransitionInfo.new(o2, o1, 1111885200))
  276. dt_period = TestTimezone.new('Europe/London', nil, [period], dt).period_for_local(dt)
  277. dt2_period = TestTimezone.new('Europe/London', nil, [period], dt2).period_for_local(dt2)
  278. t_period = TestTimezone.new('Europe/London', nil, [period], t).period_for_local(t)
  279. t2_period = TestTimezone.new('Europe/London', nil, [period], t2).period_for_local(t2)
  280. ts_period = TestTimezone.new('Europe/London', nil, [period], ts).period_for_local(ts)
  281. assert_equal(period, dt_period)
  282. assert_equal(period, dt2_period)
  283. assert_equal(period, t_period)
  284. assert_equal(period, t2_period)
  285. assert_equal(period, ts_period)
  286. end
  287. def test_period_for_local_invalid
  288. dt = DateTime.new(2004,4,4,2,30,0)
  289. tz = TestTimezone.new('America/New_York', nil, [], dt)
  290. assert_raises(PeriodNotFound) do
  291. tz.period_for_local(dt)
  292. end
  293. end
  294. def test_period_for_local_ambiguous
  295. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  296. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  297. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  298. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  299. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  300. p1 = TimezonePeriod.new(t1, t2)
  301. p2 = TimezonePeriod.new(t2, t3)
  302. dt = DateTime.new(2004,10,31,1, 0,0)
  303. t = Time.utc(2004,10,31,1,30,0)
  304. i = Time.utc(2004,10,31,1,59,59).to_i
  305. dt_tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  306. t_tz = TestTimezone.new('America/New_York', nil, [p1, p2], t)
  307. i_tz = TestTimezone.new('America/New_York', nil, [p1, p2], i)
  308. assert_raises(AmbiguousTime) { dt_tz.period_for_local(dt) }
  309. assert_raises(AmbiguousTime) { t_tz.period_for_local(t) }
  310. assert_raises(AmbiguousTime) { i_tz.period_for_local(i) }
  311. end
  312. def test_period_for_local_not_found
  313. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  314. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  315. t1 = TimezoneTransitionInfo.new(o1, o2, 1067148000)
  316. t2 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  317. t3 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  318. p1 = TimezonePeriod.new(t1, t2)
  319. p2 = TimezonePeriod.new(t2, t3)
  320. dt = DateTime.new(2004,4,4,2,0,0)
  321. t = Time.utc(2004,4,4,2,30,0)
  322. i = Time.utc(2004,4,4,2,59,59).to_i
  323. dt_tz = TestTimezone.new('America/New_York', nil, [], dt)
  324. t_tz = TestTimezone.new('America/New_York', nil, [], t)
  325. i_tz = TestTimezone.new('America/New_York', nil, [], i)
  326. assert_raises(PeriodNotFound) { dt_tz.period_for_local(dt) }
  327. assert_raises(PeriodNotFound) { t_tz.period_for_local(t) }
  328. assert_raises(PeriodNotFound) { i_tz.period_for_local(i) }
  329. end
  330. def test_period_for_local_default_dst_set_true
  331. Timezone.default_dst = true
  332. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  333. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  334. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  335. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  336. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  337. p1 = TimezonePeriod.new(t1, t2)
  338. p2 = TimezonePeriod.new(t2, t3)
  339. dt = DateTime.new(2004,10,31,1,30,0)
  340. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  341. assert_equal(p1, tz.period_for_local(dt))
  342. assert_equal(p1, tz.period_for_local(dt, true))
  343. assert_equal(p2, tz.period_for_local(dt, false))
  344. assert_raises(AmbiguousTime) { tz.period_for_local(dt, nil) }
  345. end
  346. def test_period_for_local_default_dst_set_false
  347. Timezone.default_dst = false
  348. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  349. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  350. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  351. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  352. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  353. p1 = TimezonePeriod.new(t1, t2)
  354. p2 = TimezonePeriod.new(t2, t3)
  355. dt = DateTime.new(2004,10,31,1,30,0)
  356. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  357. assert_equal(p2, tz.period_for_local(dt))
  358. assert_equal(p1, tz.period_for_local(dt, true))
  359. assert_equal(p2, tz.period_for_local(dt, false))
  360. assert_raises(AmbiguousTime) { tz.period_for_local(dt, nil) }
  361. end
  362. def test_period_for_local_dst_flag_resolved
  363. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  364. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  365. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  366. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  367. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  368. p1 = TimezonePeriod.new(t1, t2)
  369. p2 = TimezonePeriod.new(t2, t3)
  370. dt = DateTime.new(2004,10,31,1,30,0)
  371. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  372. assert_equal(p1, tz.period_for_local(dt, true))
  373. assert_equal(p2, tz.period_for_local(dt, false))
  374. assert_equal(p1, tz.period_for_local(dt, true) {|periods| raise BlockCalled, 'should not be called' })
  375. assert_equal(p2, tz.period_for_local(dt, false) {|periods| raise BlockCalled, 'should not be called' })
  376. end
  377. def test_period_for_local_dst_block_called
  378. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  379. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  380. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  381. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  382. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  383. p1 = TimezonePeriod.new(t1, t2)
  384. p2 = TimezonePeriod.new(t2, t3)
  385. dt = DateTime.new(2004,10,31,1,30,0)
  386. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  387. assert_raises(BlockCalled) {
  388. tz.period_for_local(dt) {|periods|
  389. assert_equal([p1, p2], periods)
  390. # raise exception to test that the block was called
  391. raise BlockCalled, 'should be raised'
  392. }
  393. }
  394. assert_equal(p1, tz.period_for_local(dt) {|periods| periods.first})
  395. assert_equal(p2, tz.period_for_local(dt) {|periods| periods.last})
  396. assert_equal(p1, tz.period_for_local(dt) {|periods| [periods.first]})
  397. assert_equal(p2, tz.period_for_local(dt) {|periods| [periods.last]})
  398. end
  399. def test_period_for_local_dst_cannot_resolve
  400. # At midnight local time on Aug 5 1915 in Warsaw, the clocks were put back
  401. # 24 minutes and both periods were non-DST. Hence the block should be
  402. # called regardless of the value of the Boolean dst parameter.
  403. o0 = TimezoneOffsetInfo.new(5040, 0, :LMT)
  404. o1 = TimezoneOffsetInfo.new(5040, 0, :WMT)
  405. o2 = TimezoneOffsetInfo.new(3600, 0, :CET)
  406. o3 = TimezoneOffsetInfo.new(3600, 3600, :CEST)
  407. t1 = TimezoneTransitionInfo.new(o1, o0, 288925853, 120)
  408. t2 = TimezoneTransitionInfo.new(o2, o1, 290485733, 120)
  409. t3 = TimezoneTransitionInfo.new(o3, o2, 29051813, 12)
  410. p1 = TimezonePeriod.new(t1, t2)
  411. p2 = TimezonePeriod.new(t2, t3)
  412. dt = DateTime.new(1915,8,4,23,40,0)
  413. tz = TestTimezone.new('Europe/Warsaw', nil, [p1, p2], dt)
  414. assert_raises(BlockCalled) {
  415. tz.period_for_local(dt, true) {|periods|
  416. assert_equal([p1, p2], periods)
  417. raise BlockCalled, 'should be raised'
  418. }
  419. }
  420. assert_raises(BlockCalled) {
  421. tz.period_for_local(dt, false) {|periods|
  422. assert_equal([p1, p2], periods)
  423. raise BlockCalled, 'should be raised'
  424. }
  425. }
  426. end
  427. def test_period_for_local_block_ambiguous
  428. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  429. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  430. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  431. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  432. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  433. p1 = TimezonePeriod.new(t1, t2)
  434. p2 = TimezonePeriod.new(t2, t3)
  435. dt = DateTime.new(2004,10,31,1,30,0)
  436. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  437. assert_raises(AmbiguousTime) do
  438. tz.period_for_local(dt) {|periods| nil}
  439. end
  440. assert_raises(AmbiguousTime) do
  441. tz.period_for_local(dt) {|periods| periods}
  442. end
  443. assert_raises(AmbiguousTime) do
  444. tz.period_for_local(dt) {|periods| []}
  445. end
  446. assert_raises(AmbiguousTime) do
  447. tz.period_for_local(dt) {|periods| raise AmbiguousTime, 'Ambiguous time'}
  448. end
  449. end
  450. def test_utc_to_local
  451. dt = DateTime.new(2005,6,18,16,24,23)
  452. dt2 = DateTime.new(2005,6,18,16,24,23).new_offset(Rational(5,24))
  453. t = Time.utc(2005,6,18,16,24,23)
  454. t2 = Time.local(2005,6,18,16,24,23)
  455. ts = t.to_i
  456. o1 = TimezoneOffsetInfo.new(0, 0, :GMT)
  457. o2 = TimezoneOffsetInfo.new(0, 3600, :BST)
  458. period = TimezonePeriod.new(
  459. TimezoneTransitionInfo.new(o2, o1, 1111885200),
  460. TimezoneTransitionInfo.new(o1, o2, 1130634000))
  461. assert_equal(DateTime.new(2005,6,18,17,24,23), TestTimezone.new('Europe/London', period, [], dt).utc_to_local(dt))
  462. assert_equal(DateTime.new(2005,6,18,17,24,23), TestTimezone.new('Europe/London', period, [], dt2).utc_to_local(dt2))
  463. assert_equal(Time.utc(2005,6,18,17,24,23), TestTimezone.new('Europe/London', period, [], t).utc_to_local(t))
  464. assert_equal(Time.utc(2005,6,18,17,24,23), TestTimezone.new('Europe/London', period, [], t2).utc_to_local(t2))
  465. assert_equal(Time.utc(2005,6,18,17,24,23).to_i, TestTimezone.new('Europe/London', period, [], ts).utc_to_local(ts))
  466. end
  467. def test_utc_to_local_offset
  468. dt = DateTime.new(2005,6,18,16,24,23)
  469. dt2 = DateTime.new(2005,6,18,16,24,23).new_offset(Rational(5,24))
  470. t = Time.utc(2005,6,18,16,24,23)
  471. t2 = Time.local(2005,6,18,16,24,23)
  472. o1 = TimezoneOffsetInfo.new(0, 0, :GMT)
  473. o2 = TimezoneOffsetInfo.new(0, 3600, :BST)
  474. period = TimezonePeriod.new(
  475. TimezoneTransitionInfo.new(o2, o1, 1111885200),
  476. TimezoneTransitionInfo.new(o1, o2, 1130634000))
  477. assert_equal(0, TestTimezone.new('Europe/London', period, [], dt).utc_to_local(dt).offset)
  478. assert_equal(0, TestTimezone.new('Europe/London', period, [], dt2).utc_to_local(dt2).offset)
  479. assert_equal(0, TestTimezone.new('Europe/London', period, [], t).utc_to_local(t).utc_offset)
  480. assert_equal('UTC', TestTimezone.new('Europe/London', period, [], t).utc_to_local(t).zone)
  481. assert_equal(0, TestTimezone.new('Europe/London', period, [], t2).utc_to_local(t2).utc_offset)
  482. assert_equal('UTC', TestTimezone.new('Europe/London', period, [], t2).utc_to_local(t2).zone)
  483. end
  484. def test_local_to_utc
  485. dt = DateTime.new(2005,6,18,16,24,23)
  486. dt2 = DateTime.new(2005,6,18,16,24,23)
  487. t = Time.utc(2005,6,18,16,24,23)
  488. t2 = Time.local(2005,6,18,16,24,23)
  489. ts = t.to_i
  490. o1 = TimezoneOffsetInfo.new(0, 0, :GMT)
  491. o2 = TimezoneOffsetInfo.new(0, 3600, :BST)
  492. period = TimezonePeriod.new(
  493. TimezoneTransitionInfo.new(o2, o1, 1111885200),
  494. TimezoneTransitionInfo.new(o1, o2, 1130634000))
  495. assert_equal(DateTime.new(2005,6,18,15,24,23), TestTimezone.new('Europe/London', nil, [period], dt).local_to_utc(dt))
  496. assert_equal(DateTime.new(2005,6,18,15,24,23), TestTimezone.new('Europe/London', nil, [period], dt2).local_to_utc(dt2))
  497. assert_equal(Time.utc(2005,6,18,15,24,23), TestTimezone.new('Europe/London', nil, [period], t).local_to_utc(t))
  498. assert_equal(Time.utc(2005,6,18,15,24,23), TestTimezone.new('Europe/London', nil, [period], t2).local_to_utc(t2))
  499. assert_equal(Time.utc(2005,6,18,15,24,23).to_i, TestTimezone.new('Europe/London', nil, [period], ts).local_to_utc(ts))
  500. end
  501. def test_local_to_utc_offset
  502. dt = DateTime.new(2005,6,18,16,24,23)
  503. dt2 = DateTime.new(2005,6,18,16,24,23).new_offset(Rational(5,24))
  504. t = Time.utc(2005,6,18,16,24,23)
  505. t2 = Time.local(2005,6,18,16,24,23)
  506. o1 = TimezoneOffsetInfo.new(0, 0, :GMT)
  507. o2 = TimezoneOffsetInfo.new(0, 3600, :BST)
  508. period = TimezonePeriod.new(
  509. TimezoneTransitionInfo.new(o2, o1, 1111885200),
  510. TimezoneTransitionInfo.new(o1, o2, 1130634000))
  511. assert_equal(0, TestTimezone.new('Europe/London', nil, [period], dt).local_to_utc(dt).offset)
  512. assert_equal(0, TestTimezone.new('Europe/London', nil, [period], dt2).local_to_utc(dt2).offset)
  513. assert_equal(0, TestTimezone.new('Europe/London', nil, [period], t).local_to_utc(t).utc_offset)
  514. assert_equal('UTC', TestTimezone.new('Europe/London', nil, [period], t).local_to_utc(t).zone)
  515. assert_equal(0, TestTimezone.new('Europe/London', nil, [period], t2).local_to_utc(t2).utc_offset)
  516. assert_equal('UTC', TestTimezone.new('Europe/London', nil, [period], t2).local_to_utc(t2).zone)
  517. end
  518. def test_local_to_utc_invalid
  519. dt = DateTime.new(2004,4,4,2,30,0)
  520. tz = TestTimezone.new('America/New_York', nil, [], dt)
  521. assert_raises(PeriodNotFound) { tz.local_to_utc(dt) }
  522. t = Time.utc(2004,4,4,2,30,0)
  523. tz = TestTimezone.new('America/New_York', nil, [], t)
  524. assert_raises(PeriodNotFound) { tz.local_to_utc(t) }
  525. i = Time.utc(2004,4,4,2,30,0).to_i
  526. tz = TestTimezone.new('America/New_York', nil, [], i)
  527. assert_raises(PeriodNotFound) { tz.local_to_utc(i) }
  528. end
  529. def test_local_to_utc_ambiguous
  530. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  531. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  532. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  533. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  534. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  535. p1 = TimezonePeriod.new(t1, t2)
  536. p2 = TimezonePeriod.new(t2, t3)
  537. dt = DateTime.new(2004,10,31,1,30,0)
  538. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  539. assert_raises(AmbiguousTime) { tz.local_to_utc(dt) }
  540. t = Time.utc(2004,10,31,1,30,0)
  541. tz = TestTimezone.new('America/New_York', nil, [p1, p2], t)
  542. assert_raises(AmbiguousTime) { tz.local_to_utc(t) }
  543. i = Time.utc(2004,10,31,1,30,0).to_i
  544. tz = TestTimezone.new('America/New_York', nil, [p1, p2], i)
  545. assert_raises(AmbiguousTime) { tz.local_to_utc(i) }
  546. end
  547. def test_local_to_utc_not_found
  548. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  549. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  550. t1 = TimezoneTransitionInfo.new(o1, o2, 1067148000)
  551. t2 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  552. t3 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  553. p1 = TimezonePeriod.new(t1, t2)
  554. p2 = TimezonePeriod.new(t2, t3)
  555. dt = DateTime.new(2004,4,4,2,0,0)
  556. t = Time.utc(2004,4,4,2,30,0)
  557. i = Time.utc(2004,4,4,2,59,59).to_i
  558. dt_tz = TestTimezone.new('America/New_York', nil, [], dt)
  559. t_tz = TestTimezone.new('America/New_York', nil, [], t)
  560. i_tz = TestTimezone.new('America/New_York', nil, [], i)
  561. assert_raises(PeriodNotFound) { dt_tz.local_to_utc(dt) }
  562. assert_raises(PeriodNotFound) { t_tz.local_to_utc(t) }
  563. assert_raises(PeriodNotFound) { i_tz.local_to_utc(i) }
  564. end
  565. def test_local_to_utc_default_dst_set_true
  566. Timezone.default_dst = true
  567. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  568. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  569. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  570. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  571. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  572. p1 = TimezonePeriod.new(t1, t2)
  573. p2 = TimezonePeriod.new(t2, t3)
  574. dt = DateTime.new(2004,10,31,1,30,0)
  575. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  576. assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt))
  577. assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt, true))
  578. assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt, false))
  579. assert_raises(AmbiguousTime) { tz.local_to_utc(dt, nil) }
  580. assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt) {|periods| raise BlockCalled, 'should not be called' })
  581. end
  582. def test_local_to_utc_default_dst_set_false
  583. Timezone.default_dst = false
  584. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  585. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  586. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  587. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  588. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  589. p1 = TimezonePeriod.new(t1, t2)
  590. p2 = TimezonePeriod.new(t2, t3)
  591. dt = DateTime.new(2004,10,31,1,30,0)
  592. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  593. assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt))
  594. assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt, false))
  595. assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt, true))
  596. assert_raises(AmbiguousTime) { tz.local_to_utc(dt, nil) }
  597. assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt) {|periods| raise BlockCalled, 'should not be called' })
  598. end
  599. def test_local_to_utc_dst_flag_resolved
  600. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  601. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  602. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  603. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  604. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  605. p1 = TimezonePeriod.new(t1, t2)
  606. p2 = TimezonePeriod.new(t2, t3)
  607. dt = DateTime.new(2004,10,31,1,30,0)
  608. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  609. assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt, true))
  610. assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt, false))
  611. assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt, true) {|periods| raise BlockCalled, 'should not be called' })
  612. assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt, false) {|periods| raise BlockCalled, 'should not be called' })
  613. end
  614. def test_local_to_utc_dst_block_called
  615. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  616. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  617. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  618. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  619. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  620. p1 = TimezonePeriod.new(t1, t2)
  621. p2 = TimezonePeriod.new(t2, t3)
  622. dt = DateTime.new(2004,10,31,1,30,0)
  623. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  624. assert_raises(BlockCalled) {
  625. tz.local_to_utc(dt) {|periods|
  626. assert_equal([p1, p2], periods)
  627. # raise exception to test that the block was called
  628. raise BlockCalled, 'should be raised'
  629. }
  630. }
  631. assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt) {|periods| periods.first})
  632. assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt) {|periods| periods.last})
  633. assert_equal(DateTime.new(2004,10,31,5,30,0), tz.local_to_utc(dt) {|periods| [periods.first]})
  634. assert_equal(DateTime.new(2004,10,31,6,30,0), tz.local_to_utc(dt) {|periods| [periods.last]})
  635. end
  636. def test_local_to_utc_dst_cannot_resolve
  637. # At midnight local time on Aug 5 1915 in Warsaw, the clocks were put back
  638. # 24 minutes and both periods were non-DST. Hence the block should be
  639. # called regardless of the value of the Boolean dst parameter.
  640. o0 = TimezoneOffsetInfo.new(5040, 0, :LMT)
  641. o1 = TimezoneOffsetInfo.new(5040, 0, :WMT)
  642. o2 = TimezoneOffsetInfo.new(3600, 0, :CET)
  643. o3 = TimezoneOffsetInfo.new(3600, 3600, :CEST)
  644. t1 = TimezoneTransitionInfo.new(o1, o0, 288925853, 120)
  645. t2 = TimezoneTransitionInfo.new(o2, o1, 290485733, 120)
  646. t3 = TimezoneTransitionInfo.new(o3, o2, 29051813, 12)
  647. p1 = TimezonePeriod.new(t1, t2)
  648. p2 = TimezonePeriod.new(t2, t3)
  649. dt = DateTime.new(1915,8,4,23,40,0)
  650. tz = TestTimezone.new('Europe/Warsaw', nil, [p1, p2], dt)
  651. assert_raises(BlockCalled) do
  652. tz.local_to_utc(dt, true) do |periods|
  653. assert_equal([p1, p2], periods)
  654. raise BlockCalled, 'should be raised'
  655. end
  656. end
  657. assert_raises(BlockCalled) do
  658. tz.local_to_utc(dt, false) do |periods|
  659. assert_equal([p1, p2], periods)
  660. raise BlockCalled, 'should be raised'
  661. end
  662. end
  663. assert_equal(DateTime.new(1915,8,4,22,16,0), tz.local_to_utc(dt) {|periods| periods.first})
  664. assert_equal(DateTime.new(1915,8,4,22,40,0), tz.local_to_utc(dt) {|periods| periods.last})
  665. assert_equal(DateTime.new(1915,8,4,22,16,0), tz.local_to_utc(dt) {|periods| [periods.first]})
  666. assert_equal(DateTime.new(1915,8,4,22,40,0), tz.local_to_utc(dt) {|periods| [periods.last]})
  667. end
  668. def test_local_to_utc_block_ambiguous
  669. o1 = TimezoneOffsetInfo.new(-18000, 0, :EST)
  670. o2 = TimezoneOffsetInfo.new(-18000, 3600, :EDT)
  671. t1 = TimezoneTransitionInfo.new(o2, o1, 1081062000)
  672. t2 = TimezoneTransitionInfo.new(o1, o2, 1099202400)
  673. t3 = TimezoneTransitionInfo.new(o2, o1, 1112511600)
  674. p1 = TimezonePeriod.new(t1, t2)
  675. p2 = TimezonePeriod.new(t2, t3)
  676. dt = DateTime.new(2004,10,31,1,30,0)
  677. tz = TestTimezone.new('America/New_York', nil, [p1, p2], dt)
  678. dt = DateTime.new(2004,10,31,1,30,0)
  679. tz = Timezone.get('America/New_York')
  680. assert_raises(AmbiguousTime) { tz.local_to_utc(dt) {|periods| nil} }
  681. assert_raises(AmbiguousTime) { tz.local_to_utc(dt) {|periods| periods} }
  682. assert_raises(AmbiguousTime) { tz.local_to_utc(dt) {|periods| []} }
  683. assert_raises(AmbiguousTime) { tz.local_to_utc(dt) {|periods| raise AmbiguousTime, 'Ambiguous time'} }
  684. end
  685. def test_now
  686. assert_kind_of(Time, Timezone.get('Europe/London').now)
  687. end
  688. def test_current_period
  689. assert_kind_of(TimezonePeriod, Timezone.get('Europe/London').current_period)
  690. end
  691. def test_current_period_and_time
  692. current = Timezone.get('Europe/London').current_period_and_time
  693. assert_equal(2, current.length)
  694. assert_kind_of(Time, current[0])
  695. assert_kind_of(TimezonePeriod, current[1])
  696. end
  697. def test_current_time_and_period
  698. current = Timezone.get('Europe/London').current_time_and_period
  699. assert_equal(2, current.length)
  700. assert_kind_of(Time, current[0])
  701. assert_kind_of(TimezonePeriod, current[1])
  702. end
  703. def test_compare
  704. assert_equal(0, TestTimezone.new('Europe/London') <=> TestTimezone.new('Europe/London'))
  705. assert_equal(-1, TestTimezone.new('Europe/London') <=> TestTimezone.new('Europe/london'))
  706. assert_equal(-1, TestTimezone.new('Europe/London') <=> TestTimezone.new('Europe/Paris'))
  707. assert_equal(1, TestTimezone.new('Europe/Paris') <=> TestTimezone.new('Europe/London'))
  708. assert_equal(-1, TestTimezone.new('America/New_York') <=> TestTimezone.new('Europe/Paris'))
  709. assert_equal(1, TestTimezone.new('Europe/Paris') <=> TestTimezone.new('America/New_York'))
  710. end
  711. def test_equality
  712. assert_equal(true, TestTimezone.new('Europe/London') == TestTimezone.new('Europe/London'))
  713. assert_equal(false, TestTimezone.new('Europe/London') == TestTimezone.new('Europe/london'))
  714. assert_equal(false, TestTimezone.new('Europe/London') == TestTimezone.new('Europe/Paris'))
  715. assert(!(TestTimezone.new('Europe/London') == Object.new))
  716. end
  717. def test_eql
  718. assert_equal(true, TestTimezone.new('Europe/London').eql?(TestTimezone.new('Europe/London')))
  719. assert_equal(false, TestTimezone.new('Europe/London').eql?(TestTimezone.new('Europe/london')))
  720. assert_equal(false, TestTimezone.new('Europe/London').eql?(TestTimezone.new('Europe/Paris')))
  721. assert(!TestTimezone.new('Europe/London').eql?(Object.new))
  722. end
  723. def test_hash
  724. assert_equal('Europe/London'.hash, TestTimezone.new('Europe/London').hash)
  725. assert_equal('America/New_York'.hash, TestTimezone.new('America/New_York').hash)
  726. end
  727. def test_marshal_data
  728. tz = Timezone.get('Europe/London')
  729. assert_kind_of(DataTimezone, tz)
  730. assert_same(tz, Marshal.load(Marshal.dump(tz)))
  731. end
  732. def test_marshal_linked
  733. tz = Timezone.get('UTC')
  734. assert_kind_of(LinkedTimezone, tz)
  735. assert_same(tz, Marshal.load(Marshal.dump(tz)))
  736. end
  737. def test_strftime_datetime
  738. tz = Timezone.get('Europe/London')
  739. assert_equal('23:12:02 BST', tz.strftime('%H:%M:%S %Z', DateTime.new(1965, 7, 15, 22, 12, 2)))
  740. assert_equal('BST', tz.strftime('%Z', DateTime.new(1965, 7, 15, 22, 12, 2)))
  741. assert_equal('%ZBST', tz.strftime('%%Z%Z', DateTime.new(1965, 7, 15, 22, 12, 2)))
  742. assert_equal('BST BST', tz.strftime('%Z %Z', DateTime.new(1965, 7, 15, 22, 12, 2)))
  743. end
  744. def test_strftime_time
  745. tz = Timezone.get('Europe/London')
  746. assert_equal('23:12:02 BST', tz.strftime('%H:%M:%S %Z', Time.utc(2006, 7, 15, 22, 12, 2)))
  747. assert_equal('BST', tz.strftime('%Z', Time.utc(2006, 7, 15, 22, 12, 2)))
  748. assert_equal('%ZBST', tz.strftime('%%Z%Z', Time.utc(2006, 7, 15, 22, 12, 2)))
  749. assert_equal('BST BST', tz.strftime('%Z %Z', Time.utc(2006, 7, 15, 22, 12, 2)))
  750. end
  751. def test_strftime_int
  752. tz = Timezone.get('Europe/London')
  753. assert_equal('23:12:02 BST', tz.strftime('%H:%M:%S %Z', Time.utc(2006, 7, 15, 22, 12, 2).to_i))
  754. assert_equal('BST', tz.strftime('%Z', Time.utc(2006, 7, 15, 22, 12, 2).to_i))
  755. assert_equal('%ZBST', tz.strftime('%%Z%Z', Time.utc(2006, 7, 15, 22, 12, 2).to_i))
  756. assert_equal('BST BST', tz.strftime('%Z %Z', Time.utc(2006, 7, 15, 22, 12, 2).to_i))
  757. end
  758. end