1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

time.c: support military time zone names

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2019-01-07 07:03:23 +00:00
parent 4f9ab3a01a
commit 8371a9a4ce
2 changed files with 67 additions and 0 deletions

View file

@ -260,6 +260,60 @@ class TestTimeTZ < Test::Unit::TestCase
assert_predicate(Time.new(2019, 1, 1, 0, 0, 0, "Z"), :utc?)
end
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
MON2NUM = {
"Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6,
"Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12

13
time.c
View file

@ -2066,9 +2066,21 @@ utc_offset_arg(VALUE arg)
if (s[0] == 'Z') {
return UTC_ZONE;
}
/* Military Time Zone Names */
if (s[0] >= 'A' && s[0] <= 'I') {
n = (int)s[0] - 'A' + 1;
}
else if (s[0] >= 'K' && s[0] <= 'M') {
n = (int)s[0] - 'A';
}
else if (s[0] >= 'N' && s[0] <= 'Y') {
n = 'M' - (int)s[0];
}
else {
goto invalid_utc_offset;
}
n *= 3600;
return INT2FIX(n);
case 3:
if (STRNCASECMP("UTC", s, 3) == 0) {
return UTC_ZONE;
@ -2356,6 +2368,7 @@ time_init_1(int argc, VALUE *argv, VALUE time)
*
* +tz+ specifies the timezone.
* It can be an offset from UTC, given either as a string such as "+09:00"
* or a single letter "A".."Z" excluding "J" so-called military time zone,
* or as a number of seconds such as 32400.
* Or it can be a timezone object,
* see {Timezone argument}[#class-Time-label-Timezone+argument] for details.