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

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_time_tz.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 338 lines | 312 code | 24 blank | 2 comment | 19 complexity | d955bd72947090b8dd8fdfa6dd48ba34 MD5 | raw file
  1. require 'test/unit'
  2. class TestTimeTZ < Test::Unit::TestCase
  3. def with_tz(tz)
  4. if /linux/ =~ RUBY_PLATFORM || ENV["RUBY_FORCE_TIME_TZ_TEST"] == "yes"
  5. old = ENV["TZ"]
  6. begin
  7. ENV["TZ"] = tz
  8. yield
  9. ensure
  10. ENV["TZ"] = old
  11. end
  12. else
  13. if ENV["TZ"] == tz
  14. yield
  15. end
  16. end
  17. end
  18. module Util
  19. def format_gmtoff(gmtoff, colon=false)
  20. if gmtoff < 0
  21. expected = "-"
  22. gmtoff = -gmtoff
  23. else
  24. expected = "+"
  25. end
  26. gmtoff /= 60
  27. expected << "%02d" % [gmtoff / 60]
  28. expected << ":" if colon
  29. expected << "%02d" % [gmtoff % 60]
  30. expected
  31. end
  32. def format_gmtoff2(gmtoff)
  33. if gmtoff < 0
  34. expected = "-"
  35. gmtoff = -gmtoff
  36. else
  37. expected = "+"
  38. end
  39. expected << "%02d:%02d:%02d" % [gmtoff / 3600, gmtoff % 3600 / 60, gmtoff % 60]
  40. expected
  41. end
  42. def group_by(e, &block)
  43. if e.respond_to? :group_by
  44. e.group_by(&block)
  45. else
  46. h = {}
  47. e.each {|o|
  48. (h[yield(o)] ||= []) << o
  49. }
  50. h
  51. end
  52. end
  53. end
  54. include Util
  55. extend Util
  56. def time_to_s(t)
  57. if RUBY_VERSION < "1.9"
  58. t.strftime("%Y-%m-%d %H:%M:%S ") + format_gmtoff(t.gmtoff)
  59. else
  60. t.to_s
  61. end
  62. end
  63. def assert_time_constructor(tz, expected, method, args, message=nil)
  64. m = message ? "#{message}\n" : ""
  65. m << "TZ=#{tz} Time.#{method}(#{args.map {|arg| arg.inspect }.join(', ')})"
  66. real = time_to_s(Time.send(method, *args))
  67. assert_equal(expected, real, m)
  68. end
  69. def test_america_los_angeles
  70. with_tz(tz="America/Los_Angeles") {
  71. assert_time_constructor(tz, "2007-03-11 03:00:00 -0700", :local, [2007,3,11,2,0,0])
  72. assert_time_constructor(tz, "2007-03-11 03:59:59 -0700", :local, [2007,3,11,2,59,59])
  73. assert_equal("PST", Time.new(0x1_0000_0000_0000_0000, 1).zone)
  74. assert_equal("PDT", Time.new(0x1_0000_0000_0000_0000, 8).zone)
  75. assert_equal(false, Time.new(0x1_0000_0000_0000_0000, 1).isdst)
  76. assert_equal(true, Time.new(0x1_0000_0000_0000_0000, 8).isdst)
  77. }
  78. end
  79. def test_america_managua
  80. with_tz(tz="America/Managua") {
  81. assert_time_constructor(tz, "1993-01-01 01:00:00 -0500", :local, [1993,1,1,0,0,0])
  82. assert_time_constructor(tz, "1993-01-01 01:59:59 -0500", :local, [1993,1,1,0,59,59])
  83. }
  84. end
  85. def test_asia_singapore
  86. with_tz(tz="Asia/Singapore") {
  87. assert_time_constructor(tz, "1981-12-31 23:59:59 +0730", :local, [1981,12,31,23,59,59])
  88. assert_time_constructor(tz, "1982-01-01 00:30:00 +0800", :local, [1982,1,1,0,0,0])
  89. assert_time_constructor(tz, "1982-01-01 00:59:59 +0800", :local, [1982,1,1,0,29,59])
  90. assert_time_constructor(tz, "1982-01-01 00:30:00 +0800", :local, [1982,1,1,0,30,0])
  91. }
  92. end
  93. def test_asia_tokyo
  94. with_tz(tz="Asia/Tokyo") {
  95. assert_time_constructor(tz, "1951-05-06 03:00:00 +1000", :local, [1951,5,6,2,0,0])
  96. assert_time_constructor(tz, "1951-05-06 03:59:59 +1000", :local, [1951,5,6,2,59,59])
  97. assert_time_constructor(tz, "2010-06-10 06:13:28 +0900", :local, [2010,6,10,6,13,28])
  98. }
  99. end
  100. def test_canada_newfoundland
  101. with_tz(tz="Canada/Newfoundland") {
  102. assert_time_constructor(tz, "2007-11-03 23:00:59 -0230", :new, [2007,11,3,23,0,59,:dst])
  103. assert_time_constructor(tz, "2007-11-03 23:01:00 -0230", :new, [2007,11,3,23,1,0,:dst])
  104. assert_time_constructor(tz, "2007-11-03 23:59:59 -0230", :new, [2007,11,3,23,59,59,:dst])
  105. assert_time_constructor(tz, "2007-11-04 00:00:00 -0230", :new, [2007,11,4,0,0,0,:dst])
  106. assert_time_constructor(tz, "2007-11-04 00:00:59 -0230", :new, [2007,11,4,0,0,59,:dst])
  107. assert_time_constructor(tz, "2007-11-03 23:01:00 -0330", :new, [2007,11,3,23,1,0,:std])
  108. assert_time_constructor(tz, "2007-11-03 23:59:59 -0330", :new, [2007,11,3,23,59,59,:std])
  109. assert_time_constructor(tz, "2007-11-04 00:00:59 -0330", :new, [2007,11,4,0,0,59,:std])
  110. assert_time_constructor(tz, "2007-11-04 00:01:00 -0330", :new, [2007,11,4,0,1,0,:std])
  111. }
  112. end
  113. def test_europe_brussels
  114. with_tz(tz="Europe/Brussels") {
  115. assert_time_constructor(tz, "1916-04-30 23:59:59 +0100", :local, [1916,4,30,23,59,59])
  116. assert_time_constructor(tz, "1916-05-01 01:00:00 +0200", :local, [1916,5,1], "[ruby-core:30672]")
  117. assert_time_constructor(tz, "1916-05-01 01:59:59 +0200", :local, [1916,5,1,0,59,59])
  118. assert_time_constructor(tz, "1916-05-01 01:00:00 +0200", :local, [1916,5,1,1,0,0])
  119. assert_time_constructor(tz, "1916-05-01 01:59:59 +0200", :local, [1916,5,1,1,59,59])
  120. }
  121. end
  122. def test_europe_lisbon
  123. with_tz(tz="Europe/Lisbon") {
  124. assert_equal("LMT", Time.new(-0x1_0000_0000_0000_0000).zone)
  125. }
  126. end
  127. def test_europe_moscow
  128. with_tz(tz="Europe/Moscow") {
  129. assert_time_constructor(tz, "1992-03-29 00:00:00 +0400", :local, [1992,3,28,23,0,0])
  130. assert_time_constructor(tz, "1992-03-29 00:59:59 +0400", :local, [1992,3,28,23,59,59])
  131. }
  132. end
  133. def test_pacific_kiritimati
  134. with_tz(tz="Pacific/Kiritimati") {
  135. assert_time_constructor(tz, "1994-12-31 23:59:59 -1000", :local, [1994,12,31,23,59,59])
  136. assert_time_constructor(tz, "1995-01-02 00:00:00 +1400", :local, [1995,1,1,0,0,0])
  137. assert_time_constructor(tz, "1995-01-02 23:59:59 +1400", :local, [1995,1,1,23,59,59])
  138. assert_time_constructor(tz, "1995-01-02 00:00:00 +1400", :local, [1995,1,2,0,0,0])
  139. }
  140. end
  141. def test_right_utc
  142. with_tz(tz="right/UTC") {
  143. assert_time_constructor(tz, "2008-12-31 23:59:59 UTC", :utc, [2008,12,31,23,59,59])
  144. assert_time_constructor(tz, "2008-12-31 23:59:60 UTC", :utc, [2008,12,31,23,59,60])
  145. assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,24,0,0])
  146. assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2009,1,1,0,0,0])
  147. }
  148. end
  149. def test_right_america_los_angeles
  150. with_tz(tz="right/America/Los_Angeles") {
  151. assert_time_constructor(tz, "2008-12-31 15:59:59 -0800", :local, [2008,12,31,15,59,59])
  152. assert_time_constructor(tz, "2008-12-31 15:59:60 -0800", :local, [2008,12,31,15,59,60])
  153. assert_time_constructor(tz, "2008-12-31 16:00:00 -0800", :local, [2008,12,31,16,0,0])
  154. }
  155. end
  156. MON2NUM = {
  157. "Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6,
  158. "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12
  159. }
  160. @testnum = 0
  161. def self.gen_test_name(hint)
  162. @testnum += 1
  163. s = "test_gen_#{@testnum}"
  164. s.sub(/gen_/) { "gen" + "_#{hint}_".gsub(/[^0-9A-Za-z]+/, '_') }
  165. end
  166. def self.gen_zdump_test
  167. sample = []
  168. ZDUMP_SAMPLE.each_line {|line|
  169. next if /\A\#/ =~ line || /\A\s*\z/ =~ line
  170. /\A(\S+)\s+
  171. \S+\s+(\S+)\s+(\d+)\s+(\d\d):(\d\d):(\d\d)\s+(\d+)\s+UTC
  172. \s+=\s+
  173. \S+\s+(\S+)\s+(\d+)\s+(\d\d):(\d\d):(\d\d)\s+(\d+)\s+\S+
  174. \s+isdst=\d+\s+gmtoff=(-?\d+)\n
  175. \z/x =~ line
  176. tz, u_mon, u_day, u_hour, u_min, u_sec, u_year,
  177. l_mon, l_day, l_hour, l_min, l_sec, l_year, gmtoff = $~.captures
  178. u_year = u_year.to_i
  179. u_mon = MON2NUM[u_mon]
  180. u_day = u_day.to_i
  181. u_hour = u_hour.to_i
  182. u_min = u_min.to_i
  183. u_sec = u_sec.to_i
  184. l_year = l_year.to_i
  185. l_mon = MON2NUM[l_mon]
  186. l_day = l_day.to_i
  187. l_hour = l_hour.to_i
  188. l_min = l_min.to_i
  189. l_sec = l_sec.to_i
  190. gmtoff = gmtoff.to_i
  191. sample << [tz,
  192. [u_year, u_mon, u_day, u_hour, u_min, u_sec],
  193. [l_year, l_mon, l_day, l_hour, l_min, l_sec],
  194. gmtoff]
  195. }
  196. sample.each {|tz, u, l, gmtoff|
  197. expected_utc = "%04d-%02d-%02d %02d:%02d:%02d UTC" % u
  198. expected = "%04d-%02d-%02d %02d:%02d:%02d %s" % (l+[format_gmtoff(gmtoff)])
  199. mesg_utc = "TZ=#{tz} Time.utc(#{u.map {|arg| arg.inspect }.join(', ')})"
  200. mesg = "#{mesg_utc}.localtime"
  201. define_method(gen_test_name(tz)) {
  202. with_tz(tz) {
  203. t = nil
  204. assert_nothing_raised(mesg) { t = Time.utc(*u) }
  205. assert_equal(expected_utc, time_to_s(t), mesg_utc)
  206. assert_nothing_raised(mesg) { t.localtime }
  207. assert_equal(expected, time_to_s(t), mesg)
  208. assert_equal(gmtoff, t.gmtoff)
  209. assert_equal(format_gmtoff(gmtoff), t.strftime("%z"))
  210. assert_equal(format_gmtoff(gmtoff, true), t.strftime("%:z"))
  211. assert_equal(format_gmtoff2(gmtoff), t.strftime("%::z"))
  212. }
  213. }
  214. }
  215. group_by(sample) {|tz, _, _, _| tz }.each {|tz, a|
  216. a.each_with_index {|(_, u, l, gmtoff), i|
  217. expected = "%04d-%02d-%02d %02d:%02d:%02d %s" % (l+[format_gmtoff(gmtoff)])
  218. monotonic_to_past = i == 0 || (a[i-1][2] <=> l) < 0
  219. monotonic_to_future = i == a.length-1 || (l <=> a[i+1][2]) < 0
  220. if monotonic_to_past && monotonic_to_future
  221. define_method(gen_test_name(tz)) {
  222. with_tz(tz) {
  223. assert_time_constructor(tz, expected, :local, l)
  224. assert_time_constructor(tz, expected, :local, l.reverse+[nil, nil, false, nil])
  225. assert_time_constructor(tz, expected, :local, l.reverse+[nil, nil, true, nil])
  226. assert_time_constructor(tz, expected, :new, l)
  227. assert_time_constructor(tz, expected, :new, l+[:std])
  228. assert_time_constructor(tz, expected, :new, l+[:dst])
  229. }
  230. }
  231. elsif monotonic_to_past && !monotonic_to_future
  232. define_method(gen_test_name(tz)) {
  233. with_tz(tz) {
  234. assert_time_constructor(tz, expected, :local, l.reverse+[nil, nil, true, nil])
  235. assert_time_constructor(tz, expected, :new, l+[:dst])
  236. }
  237. }
  238. elsif !monotonic_to_past && monotonic_to_future
  239. define_method(gen_test_name(tz)) {
  240. with_tz(tz) {
  241. assert_time_constructor(tz, expected, :local, l.reverse+[nil, nil, false, nil])
  242. assert_time_constructor(tz, expected, :new, l+[:std])
  243. }
  244. }
  245. else
  246. define_method(gen_test_name(tz)) {
  247. flunk("time in reverse order: TZ=#{tz} #{expected}")
  248. }
  249. end
  250. }
  251. }
  252. end
  253. ZDUMP_SAMPLE = <<'End'
  254. America/Lima Sun Apr 1 03:59:59 1990 UTC = Sat Mar 31 23:59:59 1990 PEST isdst=1 gmtoff=-14400
  255. America/Lima Sun Apr 1 04:00:00 1990 UTC = Sat Mar 31 23:00:00 1990 PET isdst=0 gmtoff=-18000
  256. America/Lima Sat Jan 1 04:59:59 1994 UTC = Fri Dec 31 23:59:59 1993 PET isdst=0 gmtoff=-18000
  257. America/Lima Sat Jan 1 05:00:00 1994 UTC = Sat Jan 1 01:00:00 1994 PEST isdst=1 gmtoff=-14400
  258. America/Lima Fri Apr 1 03:59:59 1994 UTC = Thu Mar 31 23:59:59 1994 PEST isdst=1 gmtoff=-14400
  259. America/Lima Fri Apr 1 04:00:00 1994 UTC = Thu Mar 31 23:00:00 1994 PET isdst=0 gmtoff=-18000
  260. America/Los_Angeles Sun Apr 2 09:59:59 2006 UTC = Sun Apr 2 01:59:59 2006 PST isdst=0 gmtoff=-28800
  261. America/Los_Angeles Sun Apr 2 10:00:00 2006 UTC = Sun Apr 2 03:00:00 2006 PDT isdst=1 gmtoff=-25200
  262. America/Los_Angeles Sun Oct 29 08:59:59 2006 UTC = Sun Oct 29 01:59:59 2006 PDT isdst=1 gmtoff=-25200
  263. America/Los_Angeles Sun Oct 29 09:00:00 2006 UTC = Sun Oct 29 01:00:00 2006 PST isdst=0 gmtoff=-28800
  264. America/Los_Angeles Sun Mar 11 09:59:59 2007 UTC = Sun Mar 11 01:59:59 2007 PST isdst=0 gmtoff=-28800
  265. America/Los_Angeles Sun Mar 11 10:00:00 2007 UTC = Sun Mar 11 03:00:00 2007 PDT isdst=1 gmtoff=-25200
  266. America/Los_Angeles Sun Nov 4 08:59:59 2007 UTC = Sun Nov 4 01:59:59 2007 PDT isdst=1 gmtoff=-25200
  267. America/Los_Angeles Sun Nov 4 09:00:00 2007 UTC = Sun Nov 4 01:00:00 2007 PST isdst=0 gmtoff=-28800
  268. America/Managua Thu Sep 24 04:59:59 1992 UTC = Wed Sep 23 23:59:59 1992 EST isdst=0 gmtoff=-18000
  269. America/Managua Thu Sep 24 05:00:00 1992 UTC = Wed Sep 23 23:00:00 1992 CST isdst=0 gmtoff=-21600
  270. America/Managua Fri Jan 1 05:59:59 1993 UTC = Thu Dec 31 23:59:59 1992 CST isdst=0 gmtoff=-21600
  271. America/Managua Fri Jan 1 06:00:00 1993 UTC = Fri Jan 1 01:00:00 1993 EST isdst=0 gmtoff=-18000
  272. America/Managua Wed Jan 1 04:59:59 1997 UTC = Tue Dec 31 23:59:59 1996 EST isdst=0 gmtoff=-18000
  273. America/Managua Wed Jan 1 05:00:00 1997 UTC = Tue Dec 31 23:00:00 1996 CST isdst=0 gmtoff=-21600
  274. Asia/Singapore Sun Aug 8 16:30:00 1965 UTC = Mon Aug 9 00:00:00 1965 SGT isdst=0 gmtoff=27000
  275. Asia/Singapore Thu Dec 31 16:29:59 1981 UTC = Thu Dec 31 23:59:59 1981 SGT isdst=0 gmtoff=27000
  276. Asia/Singapore Thu Dec 31 16:30:00 1981 UTC = Fri Jan 1 00:30:00 1982 SGT isdst=0 gmtoff=28800
  277. Asia/Tokyo Sat May 5 16:59:59 1951 UTC = Sun May 6 01:59:59 1951 JST isdst=0 gmtoff=32400
  278. Asia/Tokyo Sat May 5 17:00:00 1951 UTC = Sun May 6 03:00:00 1951 JDT isdst=1 gmtoff=36000
  279. Asia/Tokyo Fri Sep 7 15:59:59 1951 UTC = Sat Sep 8 01:59:59 1951 JDT isdst=1 gmtoff=36000
  280. Asia/Tokyo Fri Sep 7 16:00:00 1951 UTC = Sat Sep 8 01:00:00 1951 JST isdst=0 gmtoff=32400
  281. Canada/Newfoundland Sun Mar 11 03:30:59 2007 UTC = Sun Mar 11 00:00:59 2007 NST isdst=0 gmtoff=-12600
  282. Canada/Newfoundland Sun Mar 11 03:31:00 2007 UTC = Sun Mar 11 01:01:00 2007 NDT isdst=1 gmtoff=-9000
  283. Canada/Newfoundland Sun Nov 4 02:30:59 2007 UTC = Sun Nov 4 00:00:59 2007 NDT isdst=1 gmtoff=-9000
  284. Canada/Newfoundland Sun Nov 4 02:31:00 2007 UTC = Sat Nov 3 23:01:00 2007 NST isdst=0 gmtoff=-12600
  285. Europe/Brussels Sun Apr 30 22:59:59 1916 UTC = Sun Apr 30 23:59:59 1916 CET isdst=0 gmtoff=3600
  286. Europe/Brussels Sun Apr 30 23:00:00 1916 UTC = Mon May 1 01:00:00 1916 CEST isdst=1 gmtoff=7200
  287. Europe/Brussels Sat Sep 30 22:59:59 1916 UTC = Sun Oct 1 00:59:59 1916 CEST isdst=1 gmtoff=7200
  288. Europe/Brussels Sat Sep 30 23:00:00 1916 UTC = Sun Oct 1 00:00:00 1916 CET isdst=0 gmtoff=3600
  289. Europe/London Sun Mar 16 01:59:59 1947 UTC = Sun Mar 16 01:59:59 1947 GMT isdst=0 gmtoff=0
  290. Europe/London Sun Mar 16 02:00:00 1947 UTC = Sun Mar 16 03:00:00 1947 BST isdst=1 gmtoff=3600
  291. Europe/London Sun Apr 13 00:59:59 1947 UTC = Sun Apr 13 01:59:59 1947 BST isdst=1 gmtoff=3600
  292. Europe/London Sun Apr 13 01:00:00 1947 UTC = Sun Apr 13 03:00:00 1947 BDST isdst=1 gmtoff=7200
  293. Europe/London Sun Aug 10 00:59:59 1947 UTC = Sun Aug 10 02:59:59 1947 BDST isdst=1 gmtoff=7200
  294. Europe/London Sun Aug 10 01:00:00 1947 UTC = Sun Aug 10 02:00:00 1947 BST isdst=1 gmtoff=3600
  295. Europe/London Sun Nov 2 01:59:59 1947 UTC = Sun Nov 2 02:59:59 1947 BST isdst=1 gmtoff=3600
  296. Europe/London Sun Nov 2 02:00:00 1947 UTC = Sun Nov 2 02:00:00 1947 GMT isdst=0 gmtoff=0
  297. Europe/Moscow Sat Jan 18 23:59:59 1992 UTC = Sun Jan 19 01:59:59 1992 MSK isdst=0 gmtoff=7200
  298. Europe/Moscow Sun Jan 19 00:00:00 1992 UTC = Sun Jan 19 03:00:00 1992 MSK isdst=0 gmtoff=10800
  299. Europe/Moscow Sat Mar 28 19:59:59 1992 UTC = Sat Mar 28 22:59:59 1992 MSK isdst=0 gmtoff=10800
  300. Europe/Moscow Sat Mar 28 20:00:00 1992 UTC = Sun Mar 29 00:00:00 1992 MSD isdst=1 gmtoff=14400
  301. Europe/Moscow Sat Sep 26 18:59:59 1992 UTC = Sat Sep 26 22:59:59 1992 MSD isdst=1 gmtoff=14400
  302. Europe/Moscow Sat Sep 26 19:00:00 1992 UTC = Sat Sep 26 22:00:00 1992 MSK isdst=0 gmtoff=10800
  303. Pacific/Kiritimati Sun Jan 1 09:59:59 1995 UTC = Sat Dec 31 23:59:59 1994 LINT isdst=0 gmtoff=-36000
  304. Pacific/Kiritimati Sun Jan 1 10:00:00 1995 UTC = Mon Jan 2 00:00:00 1995 LINT isdst=0 gmtoff=50400
  305. right/America/Los_Angeles Fri Jun 30 23:59:60 1972 UTC = Fri Jun 30 16:59:60 1972 PDT isdst=1 gmtoff=-25200
  306. right/America/Los_Angeles Wed Dec 31 23:59:60 2008 UTC = Wed Dec 31 15:59:60 2008 PST isdst=0 gmtoff=-28800
  307. #right/Asia/Tokyo Fri Jun 30 23:59:60 1972 UTC = Sat Jul 1 08:59:60 1972 JST isdst=0 gmtoff=32400
  308. #right/Asia/Tokyo Sat Dec 31 23:59:60 2005 UTC = Sun Jan 1 08:59:60 2006 JST isdst=0 gmtoff=32400
  309. right/Europe/Paris Fri Jun 30 23:59:60 1972 UTC = Sat Jul 1 00:59:60 1972 CET isdst=0 gmtoff=3600
  310. right/Europe/Paris Wed Dec 31 23:59:60 2008 UTC = Thu Jan 1 00:59:60 2009 CET isdst=0 gmtoff=3600
  311. Europe/Lisbon Mon Jan 1 00:36:31 1912 UTC = Sun Dec 31 23:59:59 1911 LMT isdst=0 gmtoff=-2192
  312. End
  313. gen_zdump_test
  314. end