PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/time_zone_test.rb

https://github.com/ghar/rails
Ruby | 309 lines | 264 code | 43 blank | 2 comment | 1 complexity | 6d75a2d33240e49d8895f7ce3768f305 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'active_support/time'
  3. class TimeZoneTest < Test::Unit::TestCase
  4. def test_utc_to_local
  5. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  6. assert_equal Time.utc(1999, 12, 31, 19), zone.utc_to_local(Time.utc(2000, 1)) # standard offset -0500
  7. assert_equal Time.utc(2000, 6, 30, 20), zone.utc_to_local(Time.utc(2000, 7)) # dst offset -0400
  8. end
  9. def test_local_to_utc
  10. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  11. assert_equal Time.utc(2000, 1, 1, 5), zone.local_to_utc(Time.utc(2000, 1)) # standard offset -0500
  12. assert_equal Time.utc(2000, 7, 1, 4), zone.local_to_utc(Time.utc(2000, 7)) # dst offset -0400
  13. end
  14. def test_period_for_local
  15. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  16. assert_instance_of TZInfo::TimezonePeriod, zone.period_for_local(Time.utc(2000))
  17. end
  18. ActiveSupport::TimeZone::MAPPING.keys.each do |name|
  19. define_method("test_map_#{name.downcase.gsub(/[^a-z]/, '_')}_to_tzinfo") do
  20. zone = ActiveSupport::TimeZone[name]
  21. assert_respond_to zone.tzinfo, :period_for_local
  22. end
  23. end
  24. def test_from_integer_to_map
  25. assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-28800] # PST
  26. end
  27. def test_from_duration_to_map
  28. assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-480.minutes] # PST
  29. end
  30. ActiveSupport::TimeZone.all.each do |zone|
  31. name = zone.name.downcase.gsub(/[^a-z]/, '_')
  32. define_method("test_from_#{name}_to_map") do
  33. assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[zone.name]
  34. end
  35. define_method("test_utc_offset_for_#{name}") do
  36. period = zone.tzinfo.current_period
  37. assert_equal period.utc_offset, zone.utc_offset
  38. end
  39. end
  40. def test_now
  41. with_env_tz 'US/Eastern' do
  42. Time.stubs(:now).returns(Time.local(2000))
  43. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  44. assert_instance_of ActiveSupport::TimeWithZone, zone.now
  45. assert_equal Time.utc(2000,1,1,5), zone.now.utc
  46. assert_equal Time.utc(2000), zone.now.time
  47. assert_equal zone, zone.now.time_zone
  48. end
  49. end
  50. def test_now_enforces_spring_dst_rules
  51. with_env_tz 'US/Eastern' do
  52. Time.stubs(:now).returns(Time.local(2006,4,2,2)) # 2AM springs forward to 3AM
  53. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  54. assert_equal Time.utc(2006,4,2,3), zone.now.time
  55. assert_equal true, zone.now.dst?
  56. end
  57. end
  58. def test_now_enforces_fall_dst_rules
  59. with_env_tz 'US/Eastern' do
  60. Time.stubs(:now).returns(Time.at(1162098000)) # equivalent to 1AM DST
  61. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  62. assert_equal Time.utc(2006,10,29,1), zone.now.time
  63. assert_equal true, zone.now.dst?
  64. end
  65. end
  66. def test_unknown_timezones_delegation_to_tzinfo
  67. zone = ActiveSupport::TimeZone['America/Montevideo']
  68. assert_equal ActiveSupport::TimeZone, zone.class
  69. assert_equal zone.object_id, ActiveSupport::TimeZone['America/Montevideo'].object_id
  70. assert_equal Time.utc(2010, 1, 31, 22), zone.utc_to_local(Time.utc(2010, 2)) # daylight saving offset -0200
  71. assert_equal Time.utc(2010, 3, 31, 21), zone.utc_to_local(Time.utc(2010, 4)) # standard offset -0300
  72. end
  73. def test_today
  74. Time.stubs(:now).returns(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST
  75. assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
  76. Time.stubs(:now).returns(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST
  77. assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
  78. Time.stubs(:now).returns(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST
  79. assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
  80. Time.stubs(:now).returns(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
  81. assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
  82. end
  83. def test_local
  84. time = ActiveSupport::TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45)
  85. assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time
  86. assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
  87. end
  88. def test_local_with_old_date
  89. time = ActiveSupport::TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45)
  90. assert_equal [45,30,15,5,2,1850], time.to_a[0,6]
  91. assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
  92. end
  93. def test_local_enforces_spring_dst_rules
  94. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  95. twz = zone.local(2006,4,2,1,59,59) # 1 second before DST start
  96. assert_equal Time.utc(2006,4,2,1,59,59), twz.time
  97. assert_equal Time.utc(2006,4,2,6,59,59), twz.utc
  98. assert_equal false, twz.dst?
  99. assert_equal 'EST', twz.zone
  100. twz2 = zone.local(2006,4,2,2) # 2AM does not exist because at 2AM, time springs forward to 3AM
  101. assert_equal Time.utc(2006,4,2,3), twz2.time # twz is created for 3AM
  102. assert_equal Time.utc(2006,4,2,7), twz2.utc
  103. assert_equal true, twz2.dst?
  104. assert_equal 'EDT', twz2.zone
  105. twz3 = zone.local(2006,4,2,2,30) # 2:30AM does not exist because at 2AM, time springs forward to 3AM
  106. assert_equal Time.utc(2006,4,2,3,30), twz3.time # twz is created for 3:30AM
  107. assert_equal Time.utc(2006,4,2,7,30), twz3.utc
  108. assert_equal true, twz3.dst?
  109. assert_equal 'EDT', twz3.zone
  110. end
  111. def test_local_enforces_fall_dst_rules
  112. # 1AM during fall DST transition is ambiguous, it could be either DST or non-DST 1AM
  113. # Mirroring Time.local behavior, this method selects the DST time
  114. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  115. twz = zone.local(2006,10,29,1)
  116. assert_equal Time.utc(2006,10,29,1), twz.time
  117. assert_equal Time.utc(2006,10,29,5), twz.utc
  118. assert_equal true, twz.dst?
  119. assert_equal 'EDT', twz.zone
  120. end
  121. def test_at
  122. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  123. secs = 946684800.0
  124. twz = zone.at(secs)
  125. assert_equal Time.utc(1999,12,31,19), twz.time
  126. assert_equal Time.utc(2000), twz.utc
  127. assert_equal zone, twz.time_zone
  128. assert_equal secs, twz.to_f
  129. end
  130. def test_at_with_old_date
  131. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  132. secs = DateTime.civil(1850).to_f
  133. twz = zone.at(secs)
  134. assert_equal [1850, 1, 1, 0], [twz.utc.year, twz.utc.mon, twz.utc.day, twz.utc.hour]
  135. assert_equal zone, twz.time_zone
  136. assert_equal secs, twz.to_f
  137. end
  138. def test_parse
  139. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  140. twz = zone.parse('1999-12-31 19:00:00')
  141. assert_equal Time.utc(1999,12,31,19), twz.time
  142. assert_equal Time.utc(2000), twz.utc
  143. assert_equal zone, twz.time_zone
  144. end
  145. def test_parse_string_with_timezone
  146. (-11..13).each do |timezone_offset|
  147. zone = ActiveSupport::TimeZone[timezone_offset]
  148. twz = zone.parse('1999-12-31 19:00:00')
  149. assert_equal twz, zone.parse(twz.to_s)
  150. end
  151. end
  152. def test_parse_with_old_date
  153. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  154. twz = zone.parse('1850-12-31 19:00:00')
  155. assert_equal [0,0,19,31,12,1850], twz.to_a[0,6]
  156. assert_equal zone, twz.time_zone
  157. end
  158. def test_parse_far_future_date_with_time_zone_offset_in_string
  159. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  160. twz = zone.parse('2050-12-31 19:00:00 -10:00') # i.e., 2050-01-01 05:00:00 UTC
  161. assert_equal [0,0,0,1,1,2051], twz.to_a[0,6]
  162. assert_equal zone, twz.time_zone
  163. end
  164. def test_parse_returns_nil_when_string_without_date_information_is_passed_in
  165. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  166. assert_nil zone.parse('foobar')
  167. assert_nil zone.parse(' ')
  168. end
  169. def test_parse_with_incomplete_date
  170. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  171. zone.stubs(:now).returns zone.local(1999,12,31)
  172. twz = zone.parse('19:00:00')
  173. assert_equal Time.utc(1999,12,31,19), twz.time
  174. end
  175. def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize
  176. tzinfo = TZInfo::Timezone.get('America/New_York')
  177. zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo)
  178. assert_equal nil, zone.instance_variable_get('@utc_offset')
  179. assert_equal(-18_000, zone.utc_offset)
  180. end
  181. def test_seconds_to_utc_offset_with_colon
  182. assert_equal "-06:00", ActiveSupport::TimeZone.seconds_to_utc_offset(-21_600)
  183. assert_equal "+00:00", ActiveSupport::TimeZone.seconds_to_utc_offset(0)
  184. assert_equal "+05:00", ActiveSupport::TimeZone.seconds_to_utc_offset(18_000)
  185. end
  186. def test_seconds_to_utc_offset_without_colon
  187. assert_equal "-0600", ActiveSupport::TimeZone.seconds_to_utc_offset(-21_600, false)
  188. assert_equal "+0000", ActiveSupport::TimeZone.seconds_to_utc_offset(0, false)
  189. assert_equal "+0500", ActiveSupport::TimeZone.seconds_to_utc_offset(18_000, false)
  190. end
  191. def test_seconds_to_utc_offset_with_negative_offset
  192. assert_equal "-01:00", ActiveSupport::TimeZone.seconds_to_utc_offset(-3_600)
  193. assert_equal "-00:59", ActiveSupport::TimeZone.seconds_to_utc_offset(-3_599)
  194. assert_equal "-05:30", ActiveSupport::TimeZone.seconds_to_utc_offset(-19_800)
  195. end
  196. def test_formatted_offset_positive
  197. zone = ActiveSupport::TimeZone['New Delhi']
  198. assert_equal "+05:30", zone.formatted_offset
  199. assert_equal "+0530", zone.formatted_offset(false)
  200. end
  201. def test_formatted_offset_negative
  202. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  203. assert_equal "-05:00", zone.formatted_offset
  204. assert_equal "-0500", zone.formatted_offset(false)
  205. end
  206. def test_formatted_offset_zero
  207. zone = ActiveSupport::TimeZone['London']
  208. assert_equal "+00:00", zone.formatted_offset
  209. assert_equal "UTC", zone.formatted_offset(true, 'UTC')
  210. end
  211. def test_zone_compare
  212. zone1 = ActiveSupport::TimeZone['Central Time (US & Canada)'] # offset -0600
  213. zone2 = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] # offset -0500
  214. assert zone1 < zone2
  215. assert zone2 > zone1
  216. assert zone1 == zone1
  217. end
  218. def test_zone_match
  219. zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  220. assert zone =~ /Eastern/
  221. assert zone =~ /New_York/
  222. assert zone !~ /Nonexistent_Place/
  223. end
  224. def test_to_s
  225. assert_equal "(GMT+05:30) New Delhi", ActiveSupport::TimeZone['New Delhi'].to_s
  226. end
  227. def test_all_sorted
  228. all = ActiveSupport::TimeZone.all
  229. 1.upto( all.length-1 ) do |i|
  230. assert all[i-1] < all[i]
  231. end
  232. end
  233. def test_index
  234. assert_nil ActiveSupport::TimeZone["bogus"]
  235. assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone["Central Time (US & Canada)"]
  236. assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[8]
  237. assert_raise(ArgumentError) { ActiveSupport::TimeZone[false] }
  238. end
  239. def test_unknown_zone_should_have_tzinfo_but_exception_on_utc_offset
  240. zone = ActiveSupport::TimeZone.create("bogus")
  241. assert_instance_of TZInfo::TimezoneProxy, zone.tzinfo
  242. assert_raise(TZInfo::InvalidTimezoneIdentifier) { zone.utc_offset }
  243. end
  244. def test_unknown_zone_with_utc_offset
  245. zone = ActiveSupport::TimeZone.create("bogus", -21_600)
  246. assert_equal(-21_600, zone.utc_offset)
  247. end
  248. def test_unknown_zones_dont_store_mapping_keys
  249. ActiveSupport::TimeZone["bogus"]
  250. assert !ActiveSupport::TimeZone.zones_map.key?("bogus")
  251. end
  252. def test_new
  253. assert_equal ActiveSupport::TimeZone["Central Time (US & Canada)"], ActiveSupport::TimeZone.new("Central Time (US & Canada)")
  254. end
  255. def test_us_zones
  256. assert ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Hawaii"])
  257. assert !ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Kuala Lumpur"])
  258. end
  259. protected
  260. def with_env_tz(new_tz = 'US/Eastern')
  261. old_tz, ENV['TZ'] = ENV['TZ'], new_tz
  262. yield
  263. ensure
  264. old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  265. end
  266. end