diff options
Diffstat (limited to 'test/ruby/test_time_tz.rb')
-rw-r--r-- | test/ruby/test_time_tz.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/ruby/test_time_tz.rb b/test/ruby/test_time_tz.rb index 7c26f49187..c3c272dca3 100644 --- a/test/ruby/test_time_tz.rb +++ b/test/ruby/test_time_tz.rb @@ -1,6 +1,7 @@ # frozen_string_literal: false require 'test/unit' require '-test-/time' +require 'time' class TestTimeTZ < Test::Unit::TestCase has_right_tz = true @@ -476,4 +477,63 @@ Europe/Lisbon Mon Jan 1 00:36:31 1912 UTC = Sun Dec 31 23:59:59 1911 LMT isdst Europe/Lisbon Mon Jan 1 00:36:44 1912 UT = Sun Dec 31 23:59:59 1911 LMT isdst=0 gmtoff=-2205 Europe/Lisbon Sun Dec 31 23:59:59 1911 UT = Sun Dec 31 23:23:14 1911 LMT isdst=0 gmtoff=-2205 End + + class TZ + attr_reader :name, :abbr, :offset + + def initialize(name, abbr, offset) + @name = name + @abbr = abbr + @offset = offset + end + + def add_offset(t, ofs) + Time::TM.new(*Time.send(:apply_offset, *t.to_a[0, 6].reverse, ofs)) + rescue => e + raise e.class, sprintf("%s: %p %+d", e.message, t, ofs) + end + + def local_to_utc(t) + add_offset(t, +@offset) + end + + def utc_to_local(t) + add_offset(t, -@offset) + end + + def abbr(t) + @abbr + end + end +end + +class TestTimeTZ::WithTZ < Test::Unit::TestCase + def tz + @tz ||= TestTimeTZ::TZ.new(tzname, abbr, +9*3600) + end + + def tzname + "Asia/Tokyo" + end + + def abbr + "JST" + end + + def test_new_with_timezone + t = Time.new(2018, 9, 1, 12, 0, 0, tz) + assert_equal([2018, 9, 1, 12, 0, 0, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone]) + assert_equal(Time.utc(2018, 9, 1, 3, 0, 0).to_i, t.to_i) + end + + def test_getlocal_with_timezone + t = Time.utc(2018, 9, 1, 12, 0, 0).getlocal(tz) + assert_equal([2018, 9, 1, 21, 0, 0, tz], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.zone]) + assert_equal(Time.utc(2018, 9, 1, 12, 0, 0), t) + end + + def test_strftime_with_timezone + t = Time.new(2018, 9, 1, 12, 0, 0, tz) + assert_equal("+0900 #{abbr}", t.strftime("%z %Z")) + end end |