aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-10-08 02:35:31 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-10-08 02:35:31 +0000
commita7ad1769b1f5721feb8f6a0db15ba0172ba46620 (patch)
tree7716f26c1b8415e0e639bf093e339cdabb13c5aa /test
parent1969d5bfcdecb7d0c38efbfb720641bc6cd8f153 (diff)
downloadruby-a7ad1769b1f5721feb8f6a0db15ba0172ba46620.tar.gz
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
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_time_tz.rb60
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