From ee58c638b8d10d2ea10faadbc7b34515d2f2e351 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 8 Oct 2018 02:35:31 +0000 Subject: Timezone support by Time [Feature #14850] * strftime.c (rb_strftime): support timezone object by `%z`. * time.c (time_init_1, time_new_timew, time_getlocaltime): accept timezone object as `off`. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64952 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_time_tz.rb | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'test') 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 -- cgit v1.2.3