2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2010-06-09 17:51:21 -04:00
|
|
|
require 'test/unit'
|
2018-07-05 07:43:42 -04:00
|
|
|
require '-test-/time'
|
2010-06-09 17:51:21 -04:00
|
|
|
|
|
|
|
class TestTimeTZ < Test::Unit::TestCase
|
2012-04-18 03:10:38 -04:00
|
|
|
has_right_tz = true
|
2014-08-17 20:04:32 -04:00
|
|
|
has_lisbon_tz = true
|
2012-04-18 03:10:38 -04:00
|
|
|
force_tz_test = ENV["RUBY_FORCE_TIME_TZ_TEST"] == "yes"
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /linux/
|
|
|
|
force_tz_test = true
|
2019-08-21 00:26:12 -04:00
|
|
|
when /darwin|freebsd|openbsd/
|
2014-08-17 20:04:32 -04:00
|
|
|
has_lisbon_tz = false
|
2012-04-18 03:10:38 -04:00
|
|
|
force_tz_test = true
|
|
|
|
end
|
|
|
|
|
2012-04-17 21:58:49 -04:00
|
|
|
if force_tz_test
|
2014-09-08 04:03:21 -04:00
|
|
|
module Util
|
|
|
|
def with_tz(tz)
|
|
|
|
old = ENV["TZ"]
|
|
|
|
begin
|
|
|
|
ENV["TZ"] = tz
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ENV["TZ"] = old
|
|
|
|
end
|
2010-06-09 17:51:21 -04:00
|
|
|
end
|
2012-04-17 21:58:49 -04:00
|
|
|
end
|
|
|
|
else
|
2014-09-08 04:03:21 -04:00
|
|
|
module Util
|
|
|
|
def with_tz(tz)
|
|
|
|
if ENV["TZ"] == tz
|
|
|
|
yield
|
|
|
|
end
|
2010-06-09 17:51:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-10 10:54:50 -04:00
|
|
|
module Util
|
2014-09-08 04:10:37 -04:00
|
|
|
def have_tz_offset?(tz)
|
2014-09-08 04:03:21 -04:00
|
|
|
with_tz(tz) {!Time.now.utc_offset.zero?}
|
|
|
|
end
|
|
|
|
|
2010-07-07 17:07:01 -04:00
|
|
|
def format_gmtoff(gmtoff, colon=false)
|
2010-06-10 10:54:50 -04:00
|
|
|
if gmtoff < 0
|
|
|
|
expected = "-"
|
|
|
|
gmtoff = -gmtoff
|
|
|
|
else
|
|
|
|
expected = "+"
|
|
|
|
end
|
|
|
|
gmtoff /= 60
|
2010-07-07 17:07:01 -04:00
|
|
|
expected << "%02d" % [gmtoff / 60]
|
|
|
|
expected << ":" if colon
|
|
|
|
expected << "%02d" % [gmtoff % 60]
|
|
|
|
expected
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_gmtoff2(gmtoff)
|
|
|
|
if gmtoff < 0
|
|
|
|
expected = "-"
|
|
|
|
gmtoff = -gmtoff
|
|
|
|
else
|
|
|
|
expected = "+"
|
|
|
|
end
|
|
|
|
expected << "%02d:%02d:%02d" % [gmtoff / 3600, gmtoff % 3600 / 60, gmtoff % 60]
|
2010-06-10 10:54:50 -04:00
|
|
|
expected
|
2010-06-10 08:33:03 -04:00
|
|
|
end
|
2010-06-10 11:15:47 -04:00
|
|
|
|
|
|
|
def group_by(e, &block)
|
|
|
|
if e.respond_to? :group_by
|
|
|
|
e.group_by(&block)
|
|
|
|
else
|
|
|
|
h = {}
|
|
|
|
e.each {|o|
|
|
|
|
(h[yield(o)] ||= []) << o
|
|
|
|
}
|
|
|
|
h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-10 08:33:03 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 10:54:50 -04:00
|
|
|
include Util
|
|
|
|
extend Util
|
|
|
|
|
2014-09-08 04:10:37 -04:00
|
|
|
has_right_tz &&= have_tz_offset?("right/America/Los_Angeles")
|
|
|
|
has_lisbon_tz &&= have_tz_offset?("Europe/Lisbon")
|
2018-02-20 21:21:10 -05:00
|
|
|
CORRECT_TOKYO_DST_1951 = with_tz("Asia/Tokyo") {
|
|
|
|
if Time.local(1951, 5, 6, 12, 0, 0).dst? # noon, DST
|
2018-10-25 21:47:09 -04:00
|
|
|
if Time.local(1951, 5, 6, 1, 0, 0).dst? # DST with fixed tzdata
|
|
|
|
Time.local(1951, 9, 8, 23, 0, 0).dst? ? "2018f" : "2018e"
|
|
|
|
end
|
2018-02-20 21:21:10 -05:00
|
|
|
end
|
|
|
|
}
|
2018-03-31 22:00:36 -04:00
|
|
|
CORRECT_KIRITIMATI_SKIP_1994 = with_tz("Pacific/Kiritimati") {
|
|
|
|
Time.local(1994, 12, 31, 0, 0, 0).year == 1995
|
|
|
|
}
|
2014-09-08 04:03:21 -04:00
|
|
|
|
2014-08-08 21:28:50 -04:00
|
|
|
def time_to_s(t)
|
|
|
|
t.to_s
|
2010-06-10 08:33:03 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 11:15:47 -04:00
|
|
|
|
2010-06-10 08:33:03 -04:00
|
|
|
def assert_time_constructor(tz, expected, method, args, message=nil)
|
|
|
|
m = message ? "#{message}\n" : ""
|
2010-06-10 11:15:47 -04:00
|
|
|
m << "TZ=#{tz} Time.#{method}(#{args.map {|arg| arg.inspect }.join(', ')})"
|
2010-06-10 08:33:03 -04:00
|
|
|
real = time_to_s(Time.send(method, *args))
|
|
|
|
assert_equal(expected, real, m)
|
|
|
|
end
|
|
|
|
|
2018-07-02 03:46:38 -04:00
|
|
|
def test_localtime_zone
|
|
|
|
t = with_tz("America/Los_Angeles") {
|
|
|
|
Time.local(2000, 1, 1)
|
|
|
|
}
|
2018-07-03 07:10:45 -04:00
|
|
|
skip "force_tz_test is false on this environment" unless t
|
2018-07-02 03:46:38 -04:00
|
|
|
z1 = t.zone
|
|
|
|
z2 = with_tz(tz="Asia/Singapore") {
|
|
|
|
t.localtime.zone
|
|
|
|
}
|
|
|
|
assert_equal(z2, z1)
|
|
|
|
end
|
|
|
|
|
2010-06-10 08:33:03 -04:00
|
|
|
def test_america_los_angeles
|
|
|
|
with_tz(tz="America/Los_Angeles") {
|
|
|
|
assert_time_constructor(tz, "2007-03-11 03:00:00 -0700", :local, [2007,3,11,2,0,0])
|
|
|
|
assert_time_constructor(tz, "2007-03-11 03:59:59 -0700", :local, [2007,3,11,2,59,59])
|
2010-07-14 11:38:30 -04:00
|
|
|
assert_equal("PST", Time.new(0x1_0000_0000_0000_0000, 1).zone)
|
|
|
|
assert_equal("PDT", Time.new(0x1_0000_0000_0000_0000, 8).zone)
|
|
|
|
assert_equal(false, Time.new(0x1_0000_0000_0000_0000, 1).isdst)
|
|
|
|
assert_equal(true, Time.new(0x1_0000_0000_0000_0000, 8).isdst)
|
2010-06-10 08:33:03 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_america_managua
|
|
|
|
with_tz(tz="America/Managua") {
|
|
|
|
assert_time_constructor(tz, "1993-01-01 01:00:00 -0500", :local, [1993,1,1,0,0,0])
|
|
|
|
assert_time_constructor(tz, "1993-01-01 01:59:59 -0500", :local, [1993,1,1,0,59,59])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_asia_singapore
|
|
|
|
with_tz(tz="Asia/Singapore") {
|
|
|
|
assert_time_constructor(tz, "1981-12-31 23:59:59 +0730", :local, [1981,12,31,23,59,59])
|
|
|
|
assert_time_constructor(tz, "1982-01-01 00:30:00 +0800", :local, [1982,1,1,0,0,0])
|
|
|
|
assert_time_constructor(tz, "1982-01-01 00:59:59 +0800", :local, [1982,1,1,0,29,59])
|
|
|
|
assert_time_constructor(tz, "1982-01-01 00:30:00 +0800", :local, [1982,1,1,0,30,0])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-06-09 17:51:21 -04:00
|
|
|
def test_asia_tokyo
|
2010-06-10 08:33:03 -04:00
|
|
|
with_tz(tz="Asia/Tokyo") {
|
2018-02-20 21:21:10 -05:00
|
|
|
h = CORRECT_TOKYO_DST_1951 ? 0 : 2
|
|
|
|
assert_time_constructor(tz, "1951-05-06 0#{h+1}:00:00 +1000", :local, [1951,5,6,h,0,0])
|
|
|
|
assert_time_constructor(tz, "1951-05-06 0#{h+1}:59:59 +1000", :local, [1951,5,6,h,59,59])
|
2010-06-10 08:33:03 -04:00
|
|
|
assert_time_constructor(tz, "2010-06-10 06:13:28 +0900", :local, [2010,6,10,6,13,28])
|
2010-06-09 17:51:21 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-12-16 21:53:30 -05:00
|
|
|
def test_asia_kuala_lumpur
|
|
|
|
with_tz(tz="Asia/Kuala_Lumpur") {
|
|
|
|
assert_time_constructor(tz, "1933-01-01 00:20:00 +0720", :local, [1933])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-06-10 08:33:03 -04:00
|
|
|
def test_canada_newfoundland
|
2012-12-06 21:55:30 -05:00
|
|
|
with_tz(tz="America/St_Johns") {
|
2010-06-10 08:33:03 -04:00
|
|
|
assert_time_constructor(tz, "2007-11-03 23:00:59 -0230", :new, [2007,11,3,23,0,59,:dst])
|
|
|
|
assert_time_constructor(tz, "2007-11-03 23:01:00 -0230", :new, [2007,11,3,23,1,0,:dst])
|
|
|
|
assert_time_constructor(tz, "2007-11-03 23:59:59 -0230", :new, [2007,11,3,23,59,59,:dst])
|
|
|
|
assert_time_constructor(tz, "2007-11-04 00:00:00 -0230", :new, [2007,11,4,0,0,0,:dst])
|
|
|
|
assert_time_constructor(tz, "2007-11-04 00:00:59 -0230", :new, [2007,11,4,0,0,59,:dst])
|
|
|
|
assert_time_constructor(tz, "2007-11-03 23:01:00 -0330", :new, [2007,11,3,23,1,0,:std])
|
|
|
|
assert_time_constructor(tz, "2007-11-03 23:59:59 -0330", :new, [2007,11,3,23,59,59,:std])
|
|
|
|
assert_time_constructor(tz, "2007-11-04 00:00:59 -0330", :new, [2007,11,4,0,0,59,:std])
|
|
|
|
assert_time_constructor(tz, "2007-11-04 00:01:00 -0330", :new, [2007,11,4,0,1,0,:std])
|
2010-06-09 17:51:21 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-06-10 08:33:03 -04:00
|
|
|
def test_europe_brussels
|
|
|
|
with_tz(tz="Europe/Brussels") {
|
|
|
|
assert_time_constructor(tz, "1916-04-30 23:59:59 +0100", :local, [1916,4,30,23,59,59])
|
2015-01-06 07:42:41 -05:00
|
|
|
assert_time_constructor(tz, "1916-05-01 01:00:00 +0200", :local, [1916,5,1], "[ruby-core:30672] [Bug #3411]")
|
2010-06-10 08:33:03 -04:00
|
|
|
assert_time_constructor(tz, "1916-05-01 01:59:59 +0200", :local, [1916,5,1,0,59,59])
|
|
|
|
assert_time_constructor(tz, "1916-05-01 01:00:00 +0200", :local, [1916,5,1,1,0,0])
|
|
|
|
assert_time_constructor(tz, "1916-05-01 01:59:59 +0200", :local, [1916,5,1,1,59,59])
|
2010-06-09 17:51:21 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-01-06 07:41:20 -05:00
|
|
|
def test_europe_berlin
|
|
|
|
with_tz(tz="Europe/Berlin") {
|
|
|
|
assert_time_constructor(tz, "2011-10-30 02:00:00 +0100", :local, [2011,10,30,2,0,0], "[ruby-core:67345] [Bug #10698]")
|
|
|
|
assert_time_constructor(tz, "2011-10-30 02:00:00 +0100", :local, [0,0,2,30,10,2011,nil,nil,false,nil])
|
|
|
|
assert_time_constructor(tz, "2011-10-30 02:00:00 +0200", :local, [0,0,2,30,10,2011,nil,nil,true,nil])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-07-14 17:05:00 -04:00
|
|
|
def test_europe_lisbon
|
2014-03-05 08:27:22 -05:00
|
|
|
with_tz("Europe/Lisbon") {
|
2010-07-14 17:05:00 -04:00
|
|
|
assert_equal("LMT", Time.new(-0x1_0000_0000_0000_0000).zone)
|
|
|
|
}
|
2014-08-17 20:04:32 -04:00
|
|
|
end if has_lisbon_tz
|
2010-07-14 17:05:00 -04:00
|
|
|
|
2010-06-10 08:33:03 -04:00
|
|
|
def test_pacific_kiritimati
|
|
|
|
with_tz(tz="Pacific/Kiritimati") {
|
2018-03-31 22:00:36 -04:00
|
|
|
assert_time_constructor(tz, "1994-12-30 00:00:00 -1000", :local, [1994,12,30,0,0,0])
|
|
|
|
assert_time_constructor(tz, "1994-12-30 23:59:59 -1000", :local, [1994,12,30,23,59,59])
|
|
|
|
if CORRECT_KIRITIMATI_SKIP_1994
|
|
|
|
assert_time_constructor(tz, "1995-01-01 00:00:00 +1400", :local, [1994,12,31,0,0,0])
|
|
|
|
assert_time_constructor(tz, "1995-01-01 23:59:59 +1400", :local, [1994,12,31,23,59,59])
|
|
|
|
assert_time_constructor(tz, "1995-01-01 00:00:00 +1400", :local, [1995,1,1,0,0,0])
|
|
|
|
else
|
|
|
|
assert_time_constructor(tz, "1994-12-31 23:59:59 -1000", :local, [1994,12,31,23,59,59])
|
|
|
|
assert_time_constructor(tz, "1995-01-02 00:00:00 +1400", :local, [1995,1,1,0,0,0])
|
|
|
|
assert_time_constructor(tz, "1995-01-02 23:59:59 +1400", :local, [1995,1,1,23,59,59])
|
|
|
|
end
|
2010-06-10 08:33:03 -04:00
|
|
|
assert_time_constructor(tz, "1995-01-02 00:00:00 +1400", :local, [1995,1,2,0,0,0])
|
2010-06-09 17:51:21 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-07-09 07:33:46 -04:00
|
|
|
def test_right_utc
|
|
|
|
with_tz(tz="right/UTC") {
|
|
|
|
assert_time_constructor(tz, "2008-12-31 23:59:59 UTC", :utc, [2008,12,31,23,59,59])
|
|
|
|
assert_time_constructor(tz, "2008-12-31 23:59:60 UTC", :utc, [2008,12,31,23,59,60])
|
|
|
|
assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,24,0,0])
|
|
|
|
assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2009,1,1,0,0,0])
|
|
|
|
}
|
2012-04-18 03:10:38 -04:00
|
|
|
end if has_right_tz
|
2010-07-09 07:33:46 -04:00
|
|
|
|
2018-07-05 07:43:42 -04:00
|
|
|
def test_right_utc_switching
|
|
|
|
with_tz("UTC") { # ensure no leap second timezone
|
|
|
|
assert_equal(4102444800, Time.utc(2100,1,1,0,0,0).to_i)
|
|
|
|
with_tz(tz="right/UTC") {
|
|
|
|
assert_time_constructor(tz, "2008-12-31 23:59:59 UTC", :utc, [2008,12,31,23,59,59])
|
2020-01-28 10:01:57 -05:00
|
|
|
assert_time_constructor(tz, "2008-12-31 23:59:60 UTC", :utc, [2008,12,31,23,59,60])
|
2018-07-05 07:43:42 -04:00
|
|
|
assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,24,0,0])
|
|
|
|
assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2009,1,1,0,0,0])
|
2020-01-28 10:01:57 -05:00
|
|
|
assert_not_equal(4102444800, Time.utc(2100,1,1,0,0,0).to_i)
|
2018-07-05 07:43:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
with_tz("right/UTC") {
|
2018-07-16 10:43:38 -04:00
|
|
|
assert_not_equal(4102444800, Time.utc(2100,1,1,0,0,0).to_i)
|
2018-07-05 07:43:42 -04:00
|
|
|
with_tz(tz="UTC") {
|
|
|
|
assert_time_constructor(tz, "2008-12-31 23:59:59 UTC", :utc, [2008,12,31,23,59,59])
|
|
|
|
assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,23,59,60])
|
|
|
|
assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,24,0,0])
|
|
|
|
assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2009,1,1,0,0,0])
|
2020-01-28 10:01:57 -05:00
|
|
|
assert_equal(4102444800, Time.utc(2100,1,1,0,0,0).to_i)
|
2018-07-05 07:43:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end if has_right_tz
|
|
|
|
|
2010-07-09 07:33:46 -04:00
|
|
|
def test_right_america_los_angeles
|
|
|
|
with_tz(tz="right/America/Los_Angeles") {
|
|
|
|
assert_time_constructor(tz, "2008-12-31 15:59:59 -0800", :local, [2008,12,31,15,59,59])
|
|
|
|
assert_time_constructor(tz, "2008-12-31 15:59:60 -0800", :local, [2008,12,31,15,59,60])
|
|
|
|
assert_time_constructor(tz, "2008-12-31 16:00:00 -0800", :local, [2008,12,31,16,0,0])
|
|
|
|
}
|
2012-04-18 03:10:38 -04:00
|
|
|
end if has_right_tz
|
2010-07-09 07:33:46 -04:00
|
|
|
|
2019-01-07 01:59:46 -05:00
|
|
|
def test_utc_names
|
|
|
|
assert_predicate(Time.new(2019, 1, 1, 0, 0, 0, "UTC"), :utc?)
|
|
|
|
assert_predicate(Time.new(2019, 1, 1, 0, 0, 0, "utc"), :utc?)
|
|
|
|
assert_predicate(Time.new(2019, 1, 1, 0, 0, 0, "Z"), :utc?)
|
2021-02-16 05:36:20 -05:00
|
|
|
assert_predicate(Time.new(2019, 1, 1, 0, 0, 0, "-00:00"), :utc?)
|
|
|
|
assert_not_predicate(Time.new(2019, 1, 1, 0, 0, 0, "+00:00"), :utc?)
|
2019-01-07 01:59:46 -05:00
|
|
|
end
|
|
|
|
|
2019-01-07 02:03:23 -05:00
|
|
|
def test_military_names
|
|
|
|
assert_equal( +1*3600, Time.new(2019, 1, 1, 0, 0, 0, "A").gmtoff)
|
|
|
|
assert_equal( +2*3600, Time.new(2019, 1, 1, 0, 0, 0, "B").gmtoff)
|
|
|
|
assert_equal( +3*3600, Time.new(2019, 1, 1, 0, 0, 0, "C").gmtoff)
|
|
|
|
assert_equal( +4*3600, Time.new(2019, 1, 1, 0, 0, 0, "D").gmtoff)
|
|
|
|
assert_equal( +5*3600, Time.new(2019, 1, 1, 0, 0, 0, "E").gmtoff)
|
|
|
|
assert_equal( +6*3600, Time.new(2019, 1, 1, 0, 0, 0, "F").gmtoff)
|
|
|
|
assert_equal( +7*3600, Time.new(2019, 1, 1, 0, 0, 0, "G").gmtoff)
|
|
|
|
assert_equal( +8*3600, Time.new(2019, 1, 1, 0, 0, 0, "H").gmtoff)
|
|
|
|
assert_equal( +9*3600, Time.new(2019, 1, 1, 0, 0, 0, "I").gmtoff)
|
|
|
|
assert_equal(+10*3600, Time.new(2019, 1, 1, 0, 0, 0, "K").gmtoff)
|
|
|
|
assert_equal(+11*3600, Time.new(2019, 1, 1, 0, 0, 0, "L").gmtoff)
|
|
|
|
assert_equal(+12*3600, Time.new(2019, 1, 1, 0, 0, 0, "M").gmtoff)
|
|
|
|
assert_equal( -1*3600, Time.new(2019, 1, 1, 0, 0, 0, "N").gmtoff)
|
|
|
|
assert_equal( -2*3600, Time.new(2019, 1, 1, 0, 0, 0, "O").gmtoff)
|
|
|
|
assert_equal( -3*3600, Time.new(2019, 1, 1, 0, 0, 0, "P").gmtoff)
|
|
|
|
assert_equal( -4*3600, Time.new(2019, 1, 1, 0, 0, 0, "Q").gmtoff)
|
|
|
|
assert_equal( -5*3600, Time.new(2019, 1, 1, 0, 0, 0, "R").gmtoff)
|
|
|
|
assert_equal( -6*3600, Time.new(2019, 1, 1, 0, 0, 0, "S").gmtoff)
|
|
|
|
assert_equal( -7*3600, Time.new(2019, 1, 1, 0, 0, 0, "T").gmtoff)
|
|
|
|
assert_equal( -8*3600, Time.new(2019, 1, 1, 0, 0, 0, "U").gmtoff)
|
|
|
|
assert_equal( -9*3600, Time.new(2019, 1, 1, 0, 0, 0, "V").gmtoff)
|
|
|
|
assert_equal(-10*3600, Time.new(2019, 1, 1, 0, 0, 0, "W").gmtoff)
|
|
|
|
assert_equal(-11*3600, Time.new(2019, 1, 1, 0, 0, 0, "X").gmtoff)
|
|
|
|
assert_equal(-12*3600, Time.new(2019, 1, 1, 0, 0, 0, "Y").gmtoff)
|
|
|
|
assert_equal( 0, Time.new(2019, 1, 1, 0, 0, 0, "Z").gmtoff)
|
|
|
|
|
|
|
|
assert_equal( +1*3600, Time.at(0, in: "A").gmtoff)
|
|
|
|
assert_equal( +2*3600, Time.at(0, in: "B").gmtoff)
|
|
|
|
assert_equal( +3*3600, Time.at(0, in: "C").gmtoff)
|
|
|
|
assert_equal( +4*3600, Time.at(0, in: "D").gmtoff)
|
|
|
|
assert_equal( +5*3600, Time.at(0, in: "E").gmtoff)
|
|
|
|
assert_equal( +6*3600, Time.at(0, in: "F").gmtoff)
|
|
|
|
assert_equal( +7*3600, Time.at(0, in: "G").gmtoff)
|
|
|
|
assert_equal( +8*3600, Time.at(0, in: "H").gmtoff)
|
|
|
|
assert_equal( +9*3600, Time.at(0, in: "I").gmtoff)
|
|
|
|
assert_equal(+10*3600, Time.at(0, in: "K").gmtoff)
|
|
|
|
assert_equal(+11*3600, Time.at(0, in: "L").gmtoff)
|
|
|
|
assert_equal(+12*3600, Time.at(0, in: "M").gmtoff)
|
|
|
|
assert_equal( -1*3600, Time.at(0, in: "N").gmtoff)
|
|
|
|
assert_equal( -2*3600, Time.at(0, in: "O").gmtoff)
|
|
|
|
assert_equal( -3*3600, Time.at(0, in: "P").gmtoff)
|
|
|
|
assert_equal( -4*3600, Time.at(0, in: "Q").gmtoff)
|
|
|
|
assert_equal( -5*3600, Time.at(0, in: "R").gmtoff)
|
|
|
|
assert_equal( -6*3600, Time.at(0, in: "S").gmtoff)
|
|
|
|
assert_equal( -7*3600, Time.at(0, in: "T").gmtoff)
|
|
|
|
assert_equal( -8*3600, Time.at(0, in: "U").gmtoff)
|
|
|
|
assert_equal( -9*3600, Time.at(0, in: "V").gmtoff)
|
|
|
|
assert_equal(-10*3600, Time.at(0, in: "W").gmtoff)
|
|
|
|
assert_equal(-11*3600, Time.at(0, in: "X").gmtoff)
|
|
|
|
assert_equal(-12*3600, Time.at(0, in: "Y").gmtoff)
|
|
|
|
assert_equal( 0, Time.at(0, in: "Z").gmtoff)
|
|
|
|
end
|
|
|
|
|
2010-06-09 17:51:21 -04:00
|
|
|
MON2NUM = {
|
2010-06-10 08:33:03 -04:00
|
|
|
"Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6,
|
|
|
|
"Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12
|
2010-06-09 17:51:21 -04:00
|
|
|
}
|
|
|
|
|
2010-06-10 11:15:47 -04:00
|
|
|
@testnum = 0
|
2010-06-10 10:54:50 -04:00
|
|
|
def self.gen_test_name(hint)
|
2010-06-10 11:15:47 -04:00
|
|
|
@testnum += 1
|
|
|
|
s = "test_gen_#{@testnum}"
|
|
|
|
s.sub(/gen_/) { "gen" + "_#{hint}_".gsub(/[^0-9A-Za-z]+/, '_') }
|
2010-06-10 10:54:50 -04:00
|
|
|
end
|
|
|
|
|
2014-09-21 23:12:23 -04:00
|
|
|
def self.parse_zdump_line(line)
|
|
|
|
return nil if /\A\#/ =~ line || /\A\s*\z/ =~ line
|
|
|
|
if /\A(\S+)\s+
|
|
|
|
\S+\s+(\S+)\s+(\d+)\s+(\d\d):(\d\d):(\d\d)\s+(\d+)\s+UTC?
|
|
|
|
\s+=\s+
|
|
|
|
\S+\s+(\S+)\s+(\d+)\s+(\d\d):(\d\d):(\d\d)\s+(\d+)\s+\S+
|
|
|
|
\s+isdst=\d+\s+gmtoff=(-?\d+)\n
|
|
|
|
\z/x !~ line
|
|
|
|
raise "unexpected zdump line: #{line.inspect}"
|
|
|
|
end
|
|
|
|
tz, u_mon, u_day, u_hour, u_min, u_sec, u_year,
|
|
|
|
l_mon, l_day, l_hour, l_min, l_sec, l_year, gmtoff = $~.captures
|
|
|
|
u_year = u_year.to_i
|
|
|
|
u_mon = MON2NUM[u_mon]
|
|
|
|
u_day = u_day.to_i
|
|
|
|
u_hour = u_hour.to_i
|
|
|
|
u_min = u_min.to_i
|
|
|
|
u_sec = u_sec.to_i
|
|
|
|
l_year = l_year.to_i
|
|
|
|
l_mon = MON2NUM[l_mon]
|
|
|
|
l_day = l_day.to_i
|
|
|
|
l_hour = l_hour.to_i
|
|
|
|
l_min = l_min.to_i
|
|
|
|
l_sec = l_sec.to_i
|
|
|
|
gmtoff = gmtoff.to_i
|
|
|
|
[tz,
|
|
|
|
[u_year, u_mon, u_day, u_hour, u_min, u_sec],
|
|
|
|
[l_year, l_mon, l_day, l_hour, l_min, l_sec],
|
|
|
|
gmtoff]
|
|
|
|
end
|
|
|
|
|
2012-04-18 03:10:38 -04:00
|
|
|
def self.gen_zdump_test(data)
|
2010-06-10 08:33:03 -04:00
|
|
|
sample = []
|
2012-04-18 03:10:38 -04:00
|
|
|
data.each_line {|line|
|
2014-09-21 23:12:23 -04:00
|
|
|
s = parse_zdump_line(line)
|
|
|
|
sample << s if s
|
2010-06-10 08:33:03 -04:00
|
|
|
}
|
|
|
|
sample.each {|tz, u, l, gmtoff|
|
2010-06-10 11:27:07 -04:00
|
|
|
expected_utc = "%04d-%02d-%02d %02d:%02d:%02d UTC" % u
|
2010-06-10 10:54:50 -04:00
|
|
|
expected = "%04d-%02d-%02d %02d:%02d:%02d %s" % (l+[format_gmtoff(gmtoff)])
|
2010-06-10 11:27:07 -04:00
|
|
|
mesg_utc = "TZ=#{tz} Time.utc(#{u.map {|arg| arg.inspect }.join(', ')})"
|
|
|
|
mesg = "#{mesg_utc}.localtime"
|
2010-06-10 10:54:50 -04:00
|
|
|
define_method(gen_test_name(tz)) {
|
|
|
|
with_tz(tz) {
|
|
|
|
t = nil
|
2010-06-10 11:27:07 -04:00
|
|
|
assert_nothing_raised(mesg) { t = Time.utc(*u) }
|
|
|
|
assert_equal(expected_utc, time_to_s(t), mesg_utc)
|
|
|
|
assert_nothing_raised(mesg) { t.localtime }
|
2010-06-10 10:54:50 -04:00
|
|
|
assert_equal(expected, time_to_s(t), mesg)
|
|
|
|
assert_equal(gmtoff, t.gmtoff)
|
2010-07-07 17:07:01 -04:00
|
|
|
assert_equal(format_gmtoff(gmtoff), t.strftime("%z"))
|
|
|
|
assert_equal(format_gmtoff(gmtoff, true), t.strftime("%:z"))
|
|
|
|
assert_equal(format_gmtoff2(gmtoff), t.strftime("%::z"))
|
2015-02-22 17:26:55 -05:00
|
|
|
assert_equal(Encoding::US_ASCII, t.zone.encoding)
|
2010-06-10 10:54:50 -04:00
|
|
|
}
|
2010-06-10 08:33:03 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-21 23:12:23 -04:00
|
|
|
|
2010-06-10 11:15:47 -04:00
|
|
|
group_by(sample) {|tz, _, _, _| tz }.each {|tz, a|
|
2014-03-05 08:27:22 -05:00
|
|
|
a.each_with_index {|(_, _, l, gmtoff), i|
|
2010-06-10 10:54:50 -04:00
|
|
|
expected = "%04d-%02d-%02d %02d:%02d:%02d %s" % (l+[format_gmtoff(gmtoff)])
|
|
|
|
monotonic_to_past = i == 0 || (a[i-1][2] <=> l) < 0
|
|
|
|
monotonic_to_future = i == a.length-1 || (l <=> a[i+1][2]) < 0
|
|
|
|
if monotonic_to_past && monotonic_to_future
|
|
|
|
define_method(gen_test_name(tz)) {
|
|
|
|
with_tz(tz) {
|
|
|
|
assert_time_constructor(tz, expected, :local, l)
|
|
|
|
assert_time_constructor(tz, expected, :local, l.reverse+[nil, nil, false, nil])
|
|
|
|
assert_time_constructor(tz, expected, :local, l.reverse+[nil, nil, true, nil])
|
|
|
|
assert_time_constructor(tz, expected, :new, l)
|
|
|
|
assert_time_constructor(tz, expected, :new, l+[:std])
|
|
|
|
assert_time_constructor(tz, expected, :new, l+[:dst])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif monotonic_to_past && !monotonic_to_future
|
|
|
|
define_method(gen_test_name(tz)) {
|
|
|
|
with_tz(tz) {
|
|
|
|
assert_time_constructor(tz, expected, :local, l.reverse+[nil, nil, true, nil])
|
|
|
|
assert_time_constructor(tz, expected, :new, l+[:dst])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif !monotonic_to_past && monotonic_to_future
|
|
|
|
define_method(gen_test_name(tz)) {
|
|
|
|
with_tz(tz) {
|
|
|
|
assert_time_constructor(tz, expected, :local, l.reverse+[nil, nil, false, nil])
|
|
|
|
assert_time_constructor(tz, expected, :new, l+[:std])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
define_method(gen_test_name(tz)) {
|
2010-06-10 08:33:03 -04:00
|
|
|
flunk("time in reverse order: TZ=#{tz} #{expected}")
|
2010-06-10 10:54:50 -04:00
|
|
|
}
|
|
|
|
end
|
2010-06-09 17:51:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-04-18 03:10:38 -04:00
|
|
|
gen_zdump_test <<'End'
|
2010-06-10 08:33:03 -04:00
|
|
|
America/Lima Sun Apr 1 03:59:59 1990 UTC = Sat Mar 31 23:59:59 1990 PEST isdst=1 gmtoff=-14400
|
|
|
|
America/Lima Sun Apr 1 04:00:00 1990 UTC = Sat Mar 31 23:00:00 1990 PET isdst=0 gmtoff=-18000
|
|
|
|
America/Lima Sat Jan 1 04:59:59 1994 UTC = Fri Dec 31 23:59:59 1993 PET isdst=0 gmtoff=-18000
|
|
|
|
America/Lima Sat Jan 1 05:00:00 1994 UTC = Sat Jan 1 01:00:00 1994 PEST isdst=1 gmtoff=-14400
|
|
|
|
America/Lima Fri Apr 1 03:59:59 1994 UTC = Thu Mar 31 23:59:59 1994 PEST isdst=1 gmtoff=-14400
|
|
|
|
America/Lima Fri Apr 1 04:00:00 1994 UTC = Thu Mar 31 23:00:00 1994 PET isdst=0 gmtoff=-18000
|
|
|
|
America/Los_Angeles Sun Apr 2 09:59:59 2006 UTC = Sun Apr 2 01:59:59 2006 PST isdst=0 gmtoff=-28800
|
|
|
|
America/Los_Angeles Sun Apr 2 10:00:00 2006 UTC = Sun Apr 2 03:00:00 2006 PDT isdst=1 gmtoff=-25200
|
|
|
|
America/Los_Angeles Sun Oct 29 08:59:59 2006 UTC = Sun Oct 29 01:59:59 2006 PDT isdst=1 gmtoff=-25200
|
|
|
|
America/Los_Angeles Sun Oct 29 09:00:00 2006 UTC = Sun Oct 29 01:00:00 2006 PST isdst=0 gmtoff=-28800
|
|
|
|
America/Los_Angeles Sun Mar 11 09:59:59 2007 UTC = Sun Mar 11 01:59:59 2007 PST isdst=0 gmtoff=-28800
|
|
|
|
America/Los_Angeles Sun Mar 11 10:00:00 2007 UTC = Sun Mar 11 03:00:00 2007 PDT isdst=1 gmtoff=-25200
|
|
|
|
America/Los_Angeles Sun Nov 4 08:59:59 2007 UTC = Sun Nov 4 01:59:59 2007 PDT isdst=1 gmtoff=-25200
|
|
|
|
America/Los_Angeles Sun Nov 4 09:00:00 2007 UTC = Sun Nov 4 01:00:00 2007 PST isdst=0 gmtoff=-28800
|
|
|
|
America/Managua Thu Sep 24 04:59:59 1992 UTC = Wed Sep 23 23:59:59 1992 EST isdst=0 gmtoff=-18000
|
|
|
|
America/Managua Thu Sep 24 05:00:00 1992 UTC = Wed Sep 23 23:00:00 1992 CST isdst=0 gmtoff=-21600
|
|
|
|
America/Managua Fri Jan 1 05:59:59 1993 UTC = Thu Dec 31 23:59:59 1992 CST isdst=0 gmtoff=-21600
|
|
|
|
America/Managua Fri Jan 1 06:00:00 1993 UTC = Fri Jan 1 01:00:00 1993 EST isdst=0 gmtoff=-18000
|
|
|
|
America/Managua Wed Jan 1 04:59:59 1997 UTC = Tue Dec 31 23:59:59 1996 EST isdst=0 gmtoff=-18000
|
|
|
|
America/Managua Wed Jan 1 05:00:00 1997 UTC = Tue Dec 31 23:00:00 1996 CST isdst=0 gmtoff=-21600
|
|
|
|
Asia/Singapore Sun Aug 8 16:30:00 1965 UTC = Mon Aug 9 00:00:00 1965 SGT isdst=0 gmtoff=27000
|
|
|
|
Asia/Singapore Thu Dec 31 16:29:59 1981 UTC = Thu Dec 31 23:59:59 1981 SGT isdst=0 gmtoff=27000
|
|
|
|
Asia/Singapore Thu Dec 31 16:30:00 1981 UTC = Fri Jan 1 00:30:00 1982 SGT isdst=0 gmtoff=28800
|
2018-02-20 21:21:10 -05:00
|
|
|
End
|
2018-10-25 21:47:09 -04:00
|
|
|
gen_zdump_test CORRECT_TOKYO_DST_1951 ? <<'End' + (CORRECT_TOKYO_DST_1951 < "2018f" ? <<'2018e' : <<'2018f') : <<'End'
|
2018-02-20 21:21:10 -05:00
|
|
|
Asia/Tokyo Sat May 5 14:59:59 1951 UTC = Sat May 5 23:59:59 1951 JST isdst=0 gmtoff=32400
|
|
|
|
Asia/Tokyo Sat May 5 15:00:00 1951 UTC = Sun May 6 01:00:00 1951 JDT isdst=1 gmtoff=36000
|
2018-10-25 21:47:09 -04:00
|
|
|
End
|
2018-02-20 21:21:10 -05:00
|
|
|
Asia/Tokyo Sat Sep 8 13:59:59 1951 UTC = Sat Sep 8 23:59:59 1951 JDT isdst=1 gmtoff=36000
|
2018-02-21 00:37:23 -05:00
|
|
|
Asia/Tokyo Sat Sep 8 14:00:00 1951 UTC = Sat Sep 8 23:00:00 1951 JST isdst=0 gmtoff=32400
|
2018-10-25 21:47:09 -04:00
|
|
|
2018e
|
|
|
|
Asia/Tokyo Sat Sep 8 14:59:59 1951 UTC = Sun Sep 9 00:59:59 1951 JDT isdst=1 gmtoff=36000
|
|
|
|
Asia/Tokyo Sat Sep 8 15:00:00 1951 UTC = Sun Sep 9 00:00:00 1951 JST isdst=0 gmtoff=32400
|
|
|
|
2018f
|
2010-06-09 17:51:21 -04:00
|
|
|
Asia/Tokyo Sat May 5 16:59:59 1951 UTC = Sun May 6 01:59:59 1951 JST isdst=0 gmtoff=32400
|
|
|
|
Asia/Tokyo Sat May 5 17:00:00 1951 UTC = Sun May 6 03:00:00 1951 JDT isdst=1 gmtoff=36000
|
|
|
|
Asia/Tokyo Fri Sep 7 15:59:59 1951 UTC = Sat Sep 8 01:59:59 1951 JDT isdst=1 gmtoff=36000
|
|
|
|
Asia/Tokyo Fri Sep 7 16:00:00 1951 UTC = Sat Sep 8 01:00:00 1951 JST isdst=0 gmtoff=32400
|
2018-02-20 21:21:10 -05:00
|
|
|
End
|
|
|
|
gen_zdump_test <<'End'
|
2012-12-06 21:55:30 -05:00
|
|
|
America/St_Johns Sun Mar 11 03:30:59 2007 UTC = Sun Mar 11 00:00:59 2007 NST isdst=0 gmtoff=-12600
|
|
|
|
America/St_Johns Sun Mar 11 03:31:00 2007 UTC = Sun Mar 11 01:01:00 2007 NDT isdst=1 gmtoff=-9000
|
|
|
|
America/St_Johns Sun Nov 4 02:30:59 2007 UTC = Sun Nov 4 00:00:59 2007 NDT isdst=1 gmtoff=-9000
|
|
|
|
America/St_Johns Sun Nov 4 02:31:00 2007 UTC = Sat Nov 3 23:01:00 2007 NST isdst=0 gmtoff=-12600
|
2010-06-09 17:51:21 -04:00
|
|
|
Europe/Brussels Sun Apr 30 22:59:59 1916 UTC = Sun Apr 30 23:59:59 1916 CET isdst=0 gmtoff=3600
|
|
|
|
Europe/Brussels Sun Apr 30 23:00:00 1916 UTC = Mon May 1 01:00:00 1916 CEST isdst=1 gmtoff=7200
|
|
|
|
Europe/Brussels Sat Sep 30 22:59:59 1916 UTC = Sun Oct 1 00:59:59 1916 CEST isdst=1 gmtoff=7200
|
|
|
|
Europe/Brussels Sat Sep 30 23:00:00 1916 UTC = Sun Oct 1 00:00:00 1916 CET isdst=0 gmtoff=3600
|
2010-06-10 08:33:03 -04:00
|
|
|
Europe/London Sun Mar 16 01:59:59 1947 UTC = Sun Mar 16 01:59:59 1947 GMT isdst=0 gmtoff=0
|
|
|
|
Europe/London Sun Mar 16 02:00:00 1947 UTC = Sun Mar 16 03:00:00 1947 BST isdst=1 gmtoff=3600
|
|
|
|
Europe/London Sun Apr 13 00:59:59 1947 UTC = Sun Apr 13 01:59:59 1947 BST isdst=1 gmtoff=3600
|
|
|
|
Europe/London Sun Apr 13 01:00:00 1947 UTC = Sun Apr 13 03:00:00 1947 BDST isdst=1 gmtoff=7200
|
|
|
|
Europe/London Sun Aug 10 00:59:59 1947 UTC = Sun Aug 10 02:59:59 1947 BDST isdst=1 gmtoff=7200
|
|
|
|
Europe/London Sun Aug 10 01:00:00 1947 UTC = Sun Aug 10 02:00:00 1947 BST isdst=1 gmtoff=3600
|
|
|
|
Europe/London Sun Nov 2 01:59:59 1947 UTC = Sun Nov 2 02:59:59 1947 BST isdst=1 gmtoff=3600
|
|
|
|
Europe/London Sun Nov 2 02:00:00 1947 UTC = Sun Nov 2 02:00:00 1947 GMT isdst=0 gmtoff=0
|
2018-04-01 09:02:11 -04:00
|
|
|
End
|
|
|
|
if CORRECT_KIRITIMATI_SKIP_1994
|
|
|
|
gen_zdump_test <<'End'
|
|
|
|
Pacific/Kiritimati Sat Dec 31 09:59:59 1994 UTC = Fri Dec 30 23:59:59 1994 LINT isdst=0 gmtoff=-36000
|
|
|
|
Pacific/Kiritimati Sat Dec 31 10:00:00 1994 UTC = Sun Jan 1 00:00:00 1995 LINT isdst=0 gmtoff=50400
|
|
|
|
End
|
|
|
|
else
|
|
|
|
gen_zdump_test <<'End'
|
2010-06-09 17:51:21 -04:00
|
|
|
Pacific/Kiritimati Sun Jan 1 09:59:59 1995 UTC = Sat Dec 31 23:59:59 1994 LINT isdst=0 gmtoff=-36000
|
|
|
|
Pacific/Kiritimati Sun Jan 1 10:00:00 1995 UTC = Mon Jan 2 00:00:00 1995 LINT isdst=0 gmtoff=50400
|
2012-04-18 03:10:38 -04:00
|
|
|
End
|
2018-04-01 09:02:11 -04:00
|
|
|
end
|
2012-04-18 03:10:38 -04:00
|
|
|
gen_zdump_test <<'End' if has_right_tz
|
2010-06-10 08:33:03 -04:00
|
|
|
right/America/Los_Angeles Fri Jun 30 23:59:60 1972 UTC = Fri Jun 30 16:59:60 1972 PDT isdst=1 gmtoff=-25200
|
|
|
|
right/America/Los_Angeles Wed Dec 31 23:59:60 2008 UTC = Wed Dec 31 15:59:60 2008 PST isdst=0 gmtoff=-28800
|
2010-06-10 08:34:40 -04:00
|
|
|
#right/Asia/Tokyo Fri Jun 30 23:59:60 1972 UTC = Sat Jul 1 08:59:60 1972 JST isdst=0 gmtoff=32400
|
|
|
|
#right/Asia/Tokyo Sat Dec 31 23:59:60 2005 UTC = Sun Jan 1 08:59:60 2006 JST isdst=0 gmtoff=32400
|
2010-06-10 08:33:03 -04:00
|
|
|
right/Europe/Paris Fri Jun 30 23:59:60 1972 UTC = Sat Jul 1 00:59:60 1972 CET isdst=0 gmtoff=3600
|
|
|
|
right/Europe/Paris Wed Dec 31 23:59:60 2008 UTC = Thu Jan 1 00:59:60 2009 CET isdst=0 gmtoff=3600
|
2014-08-17 20:04:32 -04:00
|
|
|
End
|
2014-09-21 23:12:23 -04:00
|
|
|
|
|
|
|
def self.gen_variational_zdump_test(hint, data)
|
|
|
|
sample = []
|
|
|
|
data.each_line {|line|
|
|
|
|
s = parse_zdump_line(line)
|
|
|
|
sample << s if s
|
|
|
|
}
|
|
|
|
|
|
|
|
define_method(gen_test_name(hint)) {
|
|
|
|
results = []
|
|
|
|
sample.each {|tz, u, l, gmtoff|
|
|
|
|
expected_utc = "%04d-%02d-%02d %02d:%02d:%02d UTC" % u
|
|
|
|
expected = "%04d-%02d-%02d %02d:%02d:%02d %s" % (l+[format_gmtoff(gmtoff)])
|
|
|
|
mesg_utc = "TZ=#{tz} Time.utc(#{u.map {|arg| arg.inspect }.join(', ')})"
|
|
|
|
mesg = "#{mesg_utc}.localtime"
|
|
|
|
with_tz(tz) {
|
|
|
|
t = nil
|
|
|
|
assert_nothing_raised(mesg) { t = Time.utc(*u) }
|
|
|
|
assert_equal(expected_utc, time_to_s(t), mesg_utc)
|
|
|
|
assert_nothing_raised(mesg) { t.localtime }
|
|
|
|
|
|
|
|
results << [
|
|
|
|
expected == time_to_s(t),
|
|
|
|
gmtoff == t.gmtoff,
|
|
|
|
format_gmtoff(gmtoff) == t.strftime("%z"),
|
|
|
|
format_gmtoff(gmtoff, true) == t.strftime("%:z"),
|
|
|
|
format_gmtoff2(gmtoff) == t.strftime("%::z")
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 09:02:33 -04:00
|
|
|
assert_include(results, [true, true, true, true, true])
|
2014-09-21 23:12:23 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# tzdata-2014g fixed the offset for lisbon from -0:36:32 to -0:36:45.
|
|
|
|
# [ruby-core:65058] [Bug #10245]
|
|
|
|
gen_variational_zdump_test "lisbon", <<'End' if has_lisbon_tz
|
2010-07-07 17:07:01 -04:00
|
|
|
Europe/Lisbon Mon Jan 1 00:36:31 1912 UTC = Sun Dec 31 23:59:59 1911 LMT isdst=0 gmtoff=-2192
|
2014-09-21 23:12:23 -04:00
|
|
|
Europe/Lisbon Mon Jan 1 00:36:44 1912 UT = Sun Dec 31 23:59:59 1911 LMT isdst=0 gmtoff=-2205
|
2018-04-01 09:16:14 -04:00
|
|
|
Europe/Lisbon Sun Dec 31 23:59:59 1911 UT = Sun Dec 31 23:23:14 1911 LMT isdst=0 gmtoff=-2205
|
2010-06-09 17:51:21 -04:00
|
|
|
End
|
2018-10-07 22:35:31 -04:00
|
|
|
|
|
|
|
class TZ
|
2019-07-29 00:29:42 -04:00
|
|
|
attr_reader :name
|
2018-10-07 22:35:31 -04:00
|
|
|
|
2019-04-23 00:09:34 -04:00
|
|
|
def initialize(name, abbr, offset, abbr2 = nil, offset2 = nil)
|
2018-10-07 22:35:31 -04:00
|
|
|
@name = name
|
|
|
|
@abbr = abbr
|
|
|
|
@offset = offset
|
2019-04-23 00:09:34 -04:00
|
|
|
@abbr2 = abbr2
|
|
|
|
@offset2 = offset2
|
|
|
|
end
|
|
|
|
|
|
|
|
def dst?(t)
|
|
|
|
return false unless @offset2
|
|
|
|
case t when Integer
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
case t.mon
|
|
|
|
when 4..9
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def offset(t)
|
|
|
|
(dst?(t) ? @offset2 : @offset)
|
2018-10-07 22:35:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def local_to_utc(t)
|
2019-04-23 00:09:34 -04:00
|
|
|
t - offset(t)
|
2018-10-07 22:35:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def utc_to_local(t)
|
2019-04-23 00:09:34 -04:00
|
|
|
t + offset(t)
|
2018-10-07 22:35:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def abbr(t)
|
2019-04-23 00:09:34 -04:00
|
|
|
dst?(t) ? @abbr2 : @abbr
|
2018-10-07 22:35:31 -04:00
|
|
|
end
|
2018-11-28 09:08:31 -05:00
|
|
|
|
|
|
|
def ==(other)
|
2019-04-23 00:09:34 -04:00
|
|
|
@name == other.name and abbr(0) == other.abbr(0) and offset(0) == other.offset(0)
|
2018-11-28 09:08:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<TZ: #@name #@abbr #@offset>"
|
|
|
|
end
|
2018-10-07 22:35:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-09 02:56:38 -04:00
|
|
|
module TestTimeTZ::WithTZ
|
2018-11-28 09:08:31 -05:00
|
|
|
def subtest_new(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
2019-04-23 00:09:34 -04:00
|
|
|
abbr, abbr2 = *abbr
|
|
|
|
utc_offset, utc_offset2 = *utc_offset
|
2018-11-28 09:08:31 -05:00
|
|
|
t = time_class.new(2018, 9, 1, 12, 0, 0, tzarg)
|
2019-04-23 00:09:34 -04:00
|
|
|
utc_offset, abbr = utc_offset2, abbr2 if tz.dst?(t)
|
2018-10-07 22:35:31 -04:00
|
|
|
assert_equal([2018, 9, 1, 12, 0, 0, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone])
|
2018-10-09 02:56:38 -04:00
|
|
|
h, m = (-utc_offset / 60).divmod(60)
|
2018-10-21 19:53:39 -04:00
|
|
|
assert_equal(time_class.utc(2018, 9, 1, 12+h, m, 0).to_i, t.to_i)
|
2020-07-12 01:09:48 -04:00
|
|
|
assert_equal(6, t.wday)
|
|
|
|
assert_equal(244, t.yday)
|
2020-12-28 07:13:41 -05:00
|
|
|
assert_equal(t, time_class.new(2018, 9, 1, 12, in: tzarg))
|
2021-01-13 04:25:07 -05:00
|
|
|
assert_raise(ArgumentError) {time_class.new(2018, 9, 1, 12, 0, 0, tzarg, in: tzarg)}
|
2018-10-07 22:35:31 -04:00
|
|
|
end
|
|
|
|
|
2019-04-19 04:53:40 -04:00
|
|
|
def subtest_now(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
|
|
|
t = time_class.now(in: tzarg)
|
|
|
|
assert_equal(tz, t.zone)
|
|
|
|
end
|
|
|
|
|
2018-11-28 09:08:31 -05:00
|
|
|
def subtest_getlocal(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
2019-04-23 00:09:34 -04:00
|
|
|
abbr, abbr2 = *abbr
|
|
|
|
utc_offset, utc_offset2 = *utc_offset
|
|
|
|
utc = time_class.utc(2018, 9, 1, 12, 0, 0)
|
|
|
|
utc_offset, abbr = utc_offset2, abbr2 if tz.dst?(utc)
|
|
|
|
t = utc.getlocal(tzarg)
|
2018-10-09 02:56:38 -04:00
|
|
|
h, m = (utc_offset / 60).divmod(60)
|
|
|
|
assert_equal([2018, 9, 1, 12+h, m, 0, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone])
|
2018-10-21 19:53:39 -04:00
|
|
|
assert_equal(time_class.utc(2018, 9, 1, 12, 0, 0), t)
|
2018-10-07 22:35:31 -04:00
|
|
|
end
|
|
|
|
|
2018-11-28 09:08:31 -05:00
|
|
|
def subtest_strftime(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
2019-04-23 00:09:34 -04:00
|
|
|
abbr, abbr2 = *abbr
|
|
|
|
utc_offset, utc_offset2 = *utc_offset
|
2018-11-28 09:08:31 -05:00
|
|
|
t = time_class.new(2018, 9, 1, 12, 0, 0, tzarg)
|
2019-04-23 00:09:34 -04:00
|
|
|
utc_offset, abbr = utc_offset2, abbr2 if tz.dst?(t)
|
2018-10-09 02:56:38 -04:00
|
|
|
h, m = (utc_offset.abs / 60).divmod(60)
|
|
|
|
h = -h if utc_offset < 0
|
|
|
|
assert_equal("%+.2d%.2d %s" % [h, m, abbr], t.strftime("%z %Z"))
|
2020-07-22 22:32:54 -04:00
|
|
|
assert_equal("34 35 35", t.strftime("%U %V %W"))
|
2018-10-07 22:35:31 -04:00
|
|
|
end
|
2018-10-08 00:03:32 -04:00
|
|
|
|
2018-11-28 09:08:31 -05:00
|
|
|
def subtest_plus(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
2019-04-23 00:09:34 -04:00
|
|
|
abbr, abbr2 = *abbr
|
|
|
|
utc_offset, utc_offset2 = *utc_offset
|
2018-11-28 09:08:31 -05:00
|
|
|
t = time_class.new(2018, 9, 1, 12, 0, 0, tzarg) + 4000
|
2019-04-23 00:09:34 -04:00
|
|
|
utc_offset, abbr = utc_offset2, abbr2 if tz.dst?(t)
|
2018-10-08 00:03:32 -04:00
|
|
|
assert_equal([2018, 9, 1, 13, 6, 40, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone])
|
2018-10-09 02:56:38 -04:00
|
|
|
m, s = (4000-utc_offset).divmod(60)
|
|
|
|
h, m = m.divmod(60)
|
2018-10-21 19:53:39 -04:00
|
|
|
assert_equal(time_class.utc(2018, 9, 1, 12+h, m, s), t)
|
2018-10-08 00:03:32 -04:00
|
|
|
end
|
2018-10-08 04:54:35 -04:00
|
|
|
|
2018-11-28 09:08:32 -05:00
|
|
|
def subtest_at(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
2019-04-23 00:09:34 -04:00
|
|
|
abbr, abbr2 = *abbr
|
|
|
|
utc_offset, utc_offset2 = *utc_offset
|
2018-11-28 09:08:32 -05:00
|
|
|
utc = time_class.utc(2018, 9, 1, 12, 0, 0)
|
2019-04-23 00:09:34 -04:00
|
|
|
utc_offset, abbr = utc_offset2, abbr2 if tz.dst?(utc)
|
|
|
|
h, m = (utc_offset / 60).divmod(60)
|
2018-11-28 09:08:33 -05:00
|
|
|
t = time_class.at(utc, in: tzarg)
|
2018-11-28 09:08:32 -05:00
|
|
|
assert_equal([2018, 9, 1, 12+h, m, 0, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone])
|
|
|
|
assert_equal(utc.to_i, t.to_i)
|
|
|
|
utc = utc.to_i
|
2018-11-28 09:08:33 -05:00
|
|
|
t = time_class.at(utc, in: tzarg)
|
2018-11-28 09:08:32 -05:00
|
|
|
assert_equal([2018, 9, 1, 12+h, m, 0, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone])
|
|
|
|
assert_equal(utc, t.to_i)
|
|
|
|
end
|
|
|
|
|
2020-07-24 07:17:31 -04:00
|
|
|
def subtest_to_a(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
|
|
|
t = time_class.new(2018, 9, 1, 12, 0, 0, tzarg)
|
|
|
|
ary = t.to_a
|
|
|
|
assert_equal(ary, [t.sec, t.min, t.hour, t.mday, t.mon, t.year, t.wday, t.yday, t.isdst, t.zone])
|
|
|
|
end
|
|
|
|
|
2018-11-28 09:08:31 -05:00
|
|
|
def subtest_marshal(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
|
|
|
t = time_class.new(2018, 9, 1, 12, 0, 0, tzarg)
|
2018-10-08 04:54:35 -04:00
|
|
|
t2 = Marshal.load(Marshal.dump(t))
|
|
|
|
assert_equal(t, t2)
|
2018-10-09 02:56:38 -04:00
|
|
|
assert_equal(t.utc_offset, t2.utc_offset)
|
2018-10-11 22:25:38 -04:00
|
|
|
assert_equal(t.utc_offset, (t2+1).utc_offset)
|
2018-11-28 09:08:30 -05:00
|
|
|
assert_instance_of(t.zone.class, t2.zone)
|
2019-04-23 00:09:34 -04:00
|
|
|
assert_equal(t.dst?, t2.dst?)
|
2018-10-09 02:56:38 -04:00
|
|
|
end
|
|
|
|
|
2018-11-28 09:08:35 -05:00
|
|
|
def test_invalid_zone
|
|
|
|
make_timezone("INVALID", "INV", 0)
|
|
|
|
rescue => e
|
|
|
|
assert_kind_of(StandardError, e)
|
|
|
|
else
|
|
|
|
assert false, "ArgumentError expected but nothing was raised."
|
|
|
|
end
|
|
|
|
|
|
|
|
def nametest_marshal_compatibility(time_class, tzname, abbr, utc_offset)
|
|
|
|
data = [
|
|
|
|
"\x04\x08Iu:".b, Marshal.dump(time_class)[3..-1],
|
|
|
|
"\x0d""\xEF\xA7\x1D\x80\x00\x00\x00\x00".b,
|
|
|
|
Marshal.dump({offset: utc_offset, zone: abbr})[3..-1],
|
|
|
|
].join('')
|
|
|
|
t = Marshal.load(data)
|
|
|
|
assert_equal(utc_offset, t.utc_offset)
|
|
|
|
assert_equal(utc_offset, (t+1).utc_offset)
|
|
|
|
# t.zone may be a mere String or timezone object.
|
|
|
|
end
|
|
|
|
|
2018-10-21 19:53:39 -04:00
|
|
|
ZONES = {
|
|
|
|
"Asia/Tokyo" => ["JST", +9*3600],
|
2019-04-23 00:09:34 -04:00
|
|
|
"America/Los_Angeles" => ["PST", -8*3600, "PDT", -7*3600],
|
2018-10-21 19:53:39 -04:00
|
|
|
"Africa/Ndjamena" => ["WAT", +1*3600],
|
|
|
|
}
|
2018-10-09 02:56:38 -04:00
|
|
|
|
2019-04-23 00:09:34 -04:00
|
|
|
def make_timezone(tzname, abbr, utc_offset, abbr2 = nil, utc_offset2 = nil)
|
2018-10-21 19:53:39 -04:00
|
|
|
self.class::TIME_CLASS.find_timezone(tzname)
|
2018-10-09 02:56:38 -04:00
|
|
|
end
|
|
|
|
|
2019-04-23 00:09:34 -04:00
|
|
|
def subtest_dst?(time_class, tz, tzarg, tzname, abbr, utc_offset)
|
|
|
|
t = time_class.new(2018, 6, 22, 12, 0, 0, tzarg)
|
|
|
|
return unless tz.dst?(t)
|
|
|
|
assert_predicate t, :dst?
|
|
|
|
t = time_class.new(2018, 12, 22, 12, 0, 0, tzarg)
|
|
|
|
assert_not_predicate t, :dst?
|
|
|
|
end
|
|
|
|
|
2018-10-21 19:53:39 -04:00
|
|
|
instance_methods(false).grep(/\Asub(?=test_)/) do |subtest|
|
|
|
|
test = $'
|
2019-04-23 00:09:34 -04:00
|
|
|
ZONES.each_pair do |tzname, (abbr, utc_offset, abbr2, utc_offset2)|
|
2018-10-21 19:53:39 -04:00
|
|
|
define_method("#{test}@#{tzname}") do
|
2019-04-23 00:09:34 -04:00
|
|
|
tz = make_timezone(tzname, abbr, utc_offset, abbr2, utc_offset2)
|
2018-10-21 19:53:39 -04:00
|
|
|
time_class = self.class::TIME_CLASS
|
2019-04-23 00:09:34 -04:00
|
|
|
__send__(subtest, time_class, tz, tz, tzname, [abbr, abbr2], [utc_offset, utc_offset2])
|
|
|
|
__send__(subtest, time_class, tz, tzname, tzname, [abbr, abbr2], [utc_offset, utc_offset2])
|
2018-10-21 19:53:39 -04:00
|
|
|
end
|
|
|
|
end
|
2018-10-09 02:56:38 -04:00
|
|
|
end
|
2018-11-28 09:08:35 -05:00
|
|
|
|
|
|
|
instance_methods(false).grep(/\Aname(?=test_)/) do |subtest|
|
|
|
|
test = $'
|
|
|
|
ZONES.each_pair do |tzname, (abbr, utc_offset)|
|
|
|
|
define_method("#{test}@#{tzname}") do
|
|
|
|
time_class = self.class::TIME_CLASS
|
|
|
|
__send__(subtest, time_class, tzname, abbr, utc_offset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-09 02:56:38 -04:00
|
|
|
end
|
|
|
|
|
2018-10-21 19:53:39 -04:00
|
|
|
class TestTimeTZ::DummyTZ < Test::Unit::TestCase
|
2018-10-09 02:56:38 -04:00
|
|
|
include TestTimeTZ::WithTZ
|
|
|
|
|
2018-10-21 19:53:39 -04:00
|
|
|
class TIME_CLASS < ::Time
|
|
|
|
ZONES = TestTimeTZ::WithTZ::ZONES
|
|
|
|
def self.find_timezone(tzname)
|
|
|
|
tz = ZONES[tzname] or raise ArgumentError, "Unknown timezone: #{name}"
|
|
|
|
TestTimeTZ::TZ.new(tzname, *tz)
|
|
|
|
end
|
2018-10-09 02:56:38 -04:00
|
|
|
end
|
|
|
|
|
2019-04-23 00:09:34 -04:00
|
|
|
def self.make_timezone(tzname, abbr, utc_offset, abbr2 = nil, utc_offset2 = nil)
|
|
|
|
TestTimeTZ::TZ.new(tzname, abbr, utc_offset, abbr2, utc_offset2)
|
2018-10-08 04:54:35 -04:00
|
|
|
end
|
2020-11-27 00:16:19 -05:00
|
|
|
|
|
|
|
def test_fractional_second
|
|
|
|
x = Object.new
|
|
|
|
def x.local_to_utc(t); t + 8*3600; end
|
|
|
|
def x.utc_to_local(t); t - 8*3600; end
|
|
|
|
|
|
|
|
t1 = Time.new(2020,11,11,12,13,14.124r, '-08:00')
|
|
|
|
t2 = Time.new(2020,11,11,12,13,14.124r, x)
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
end
|
2010-06-09 17:51:21 -04:00
|
|
|
end
|
2018-11-28 09:08:34 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
require "tzinfo"
|
|
|
|
rescue LoadError
|
|
|
|
else
|
|
|
|
class TestTimeTZ::GemTZInfo < Test::Unit::TestCase
|
|
|
|
include TestTimeTZ::WithTZ
|
|
|
|
|
|
|
|
class TIME_CLASS < ::Time
|
|
|
|
def self.find_timezone(tzname)
|
|
|
|
TZInfo::Timezone.get(tzname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tz
|
|
|
|
@tz ||= TZInfo::Timezone.get(tzname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
require "timezone"
|
|
|
|
rescue LoadError
|
|
|
|
else
|
|
|
|
class TestTimeTZ::GemTimezone < Test::Unit::TestCase
|
|
|
|
include TestTimeTZ::WithTZ
|
|
|
|
|
|
|
|
class TIME_CLASS < ::Time
|
|
|
|
def self.find_timezone(name)
|
2018-11-28 09:08:35 -05:00
|
|
|
Timezone.fetch(name)
|
2018-11-28 09:08:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tz
|
|
|
|
@tz ||= Timezone[tzname]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|