ruby-changes:71703
From: usa <ko1@a...>
Date: Tue, 12 Apr 2022 20:03:25 +0900 (JST)
Subject: [ruby-changes:71703] fa7f75ddda (ruby_2_6): Backport date-2.0.3 for Reiwa Support [Backport #18514]
https://git.ruby-lang.org/ruby.git/commit/?id=fa7f75ddda From fa7f75ddda0eb01bc78363ced78d270cb55931ba Mon Sep 17 00:00:00 2001 From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Tue, 12 Apr 2022 11:03:11 +0000 Subject: Backport date-2.0.3 for Reiwa Support [Backport #18514] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67956 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/date/date_core.c | 15 ++++++++- ext/date/date_parse.c | 12 ++++--- ext/date/lib/date.rb | 2 +- test/date/test_date_parse.rb | 72 +++++++++++++++++++++++++++++++++++++++++ test/date/test_date_strftime.rb | 7 +++- version.h | 4 +-- 6 files changed, 103 insertions(+), 9 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 918e61234c..1734ec0349 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -4755,6 +4755,10 @@ date_s__jisx0301(int argc, VALUE *argv, VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/date/date_core.c#L4755 * * Date.jisx0301('H13.02.03') #=> #<Date: 2001-02-03 ...> * + * For no-era year, legacy format, Heisei is assumed. + * + * Date.jisx0301('13.02.03') #=> #<Date: 2001-02-03 ...> + * * Raise an ArgumentError when the string length is longer than _limit_. * You can stop this check by passing `limit: nil`, but note that * it may take a long time to parse. @@ -7190,10 +7194,14 @@ jisx0301_date_format(char *fmt, size_t size, VALUE jd, VALUE y) https://github.com/ruby/ruby/blob/trunk/ext/date/date_core.c#L7194 c = 'S'; s = 1925; } - else { + else if (d < 2458605) { c = 'H'; s = 1988; } + else { + c = 'R'; + s = 2018; + } snprintf(fmt, size, "%c%02ld" ".%%m.%%d", c, FIX2INT(y) - s); return fmt; } @@ -8358,6 +8366,11 @@ datetime_s_httpdate(int argc, VALUE *argv, VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/date/date_core.c#L8366 * DateTime.jisx0301('H13.02.03T04:05:06+07:00') * #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> * + * For no-era year, legacy format, Heisei is assumed. + * + * DateTime.jisx0301('13.02.03T04:05:06+07:00') + * #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> + * * Raise an ArgumentError when the string length is longer than _limit_. * You can stop this check by passing `limit: nil`, but note that * it may take a long time to parse. diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index 69ac37f0a3..f06c07bae4 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -1212,6 +1212,9 @@ parse_iso2(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L1212 return 1; } +#define JISX0301_ERA_INITIALS "mtshr" +#define JISX0301_DEFAULT_ERA 'H' /* obsolete */ + static int gengo(int c) { @@ -1222,6 +1225,7 @@ gengo(int c) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L1225 case 'T': case 't': e = 1911; break; case 'S': case 's': e = 1925; break; case 'H': case 'h': e = 1988; break; + case 'R': case 'r': e = 2018; break; default: e = 0; break; } return e; @@ -1252,11 +1256,11 @@ parse_jis(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L1256 { static const char pat_source[] = #ifndef TIGHT_PARSER - "\\b([mtsh])(\\d+)\\.(\\d+)\\.(\\d+)" + "\\b([" JISX0301_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" #else BOS FPW_COM FPT_COM - "([mtsh])(\\d+)\\.(\\d+)\\.(\\d+)" + "([" JISX0301_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" TEE_FPT COM_FPW EOS #endif @@ -2954,7 +2958,7 @@ jisx0301_cb(VALUE m, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L2958 s[i] = rb_reg_nth_match(i, m); } - ep = gengo(NIL_P(s[1]) ? 'h' : *RSTRING_PTR(s[1])); + ep = gengo(NIL_P(s[1]) ? JISX0301_DEFAULT_ERA : *RSTRING_PTR(s[1])); set_hash("year", f_add(str2num(s[2]), INT2FIX(ep))); set_hash("mon", str2num(s[3])); set_hash("mday", str2num(s[4])); @@ -2979,7 +2983,7 @@ static int https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L2983 jisx0301(VALUE str, VALUE hash) { static const char pat_source[] = - "\\A\\s*([mtsh])?(\\d{2})\\.(\\d{2})\\.(\\d{2})" + "\\A\\s*([" JISX0301_ERA_INITIALS "])?(\\d{2})\\.(\\d{2})\\.(\\d{2})" "(?:t" "(?:(\\d{2}):(\\d{2})(?::(\\d{2})(?:[,.](\\d*))?)?" "(z|[-+]\\d{2}(?::?\\d{2})?)?)?)?\\s*\\z"; diff --git a/ext/date/lib/date.rb b/ext/date/lib/date.rb index 3556b9295f..cea63b1259 100644 --- a/ext/date/lib/date.rb +++ b/ext/date/lib/date.rb @@ -4,7 +4,7 @@ https://github.com/ruby/ruby/blob/trunk/ext/date/lib/date.rb#L4 require 'date_core' class Date - VERSION = '2.0.2' # :nodoc: + VERSION = '2.0.3' # :nodoc: class Infinity < Numeric # :nodoc: diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb index 5bc2cebf82..d1163b29cb 100644 --- a/test/date/test_date_parse.rb +++ b/test/date/test_date_parse.rb @@ -1020,6 +1020,15 @@ class TestDateParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/date/test_date_parse.rb#L1020 h = Date._jisx0301('S63.02.03') assert_equal([1988, 2, 3, nil, nil, nil, nil], h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.04.30') + assert_equal([2019, 4, 30, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.05.01') + assert_equal([2019, 5, 1, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('R01.05.01') + assert_equal([2019, 5, 1, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) h = Date._jisx0301('H13.02.03T04:05:06') assert_equal([2001, 2, 3, 4, 5, 6, nil], @@ -1034,6 +1043,45 @@ class TestDateParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/date/test_date_parse.rb#L1043 assert_equal([2001, 2, 3, 4, 5, 6, 3600], h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.04.30T04:05:06') + assert_equal([2019, 4, 30, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.04.30T04:05:06,07') + assert_equal([2019, 4, 30, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.04.30T04:05:06Z') + assert_equal([2019, 4, 30, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.04.30T04:05:06.07+0100') + assert_equal([2019, 4, 30, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._jisx0301('H31.05.01T04:05:06') + assert_equal([2019, 5, 1, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.05.01T04:05:06,07') + assert_equal([2019, 5, 1, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.05.01T04:05:06Z') + assert_equal([2019, 5, 1, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('H31.05.01T04:05:06.07+0100') + assert_equal([2019, 5, 1, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + + h = Date._jisx0301('R01.05.01T04:05:06') + assert_equal([2019, 5, 1, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('R01.05.01T04:05:06,07') + assert_equal([2019, 5, 1, 4, 5, 6, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('R01.05.01T04:05:06Z') + assert_equal([2019, 5, 1, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('R01.05.01T04:05:06.07+0100') + assert_equal([2019, 5, 1, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + h = Date._jisx0301('') assert_equal({}, h) @@ -1126,9 +1174,33 @@ class TestDateParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/date/test_date_parse.rb#L1174 assert_equal(Date.new(2001,2,3), d) assert_equal(Date::ITALY + 10, d.start) + d = Date.jisx0301('H31.04.30', Date::ITALY + 10) + assert_equal(Date.new(2019,4,30), d) + assert_equal(Date::ITALY + 10, d.start) + + d = Date.jisx0301('H31.05.01', Date::ITALY + 10) + assert_equal(Date.new(2019,5,1), d) + assert_equal(Date::ITALY + 10, d.start) + + d = Date.jisx0301('R01.05.01', Date::ITALY + 10) + assert_equal(Date.new(2019,5,1), d) + assert_equal(Date::ITALY + 10, d.start) + d = DateTime.jisx0301('H13.02.03T04:05:06+07:00', Date::ITALY + 10) assert_equal(DateTime.new(2001,2,3,4,5,6,'+07:00'), d) assert_equal(Date::ITALY + 10, d.start) + + d = DateTime.jisx0301('H31.04.30T04:05:06+07:00', Date::ITALY + 10) + assert_equal(DateTime.new(2019,4,30,4,5,6,'+07:00'), d) + assert_equal(Date::ITALY + 10, d.start) + + d = DateTime.jisx0301('H31.05.01T04:05:06+07:00', Date::ITALY + 10) + assert_equal(DateTime.new(2019,5,1,4,5,6,'+07:00'), d) + assert_equal(Date::ITALY + 10, d.start) + + d = DateTime.jisx0301('R01.05.01T04:05:06+07:00', Date::ITALY + 10) + assert_equal(DateTime.new(2019,5,1,4,5,6,'+07:00'), d) + assert_equal(Date::ITALY + 10, d.start) end def test_given_string diff --git a/test/date/test_date_strftime.rb b/test/date/test_date_strftime.rb index a33eaa340f..dc237a909d 100644 --- a/test/date/test_date_strftime.rb +++ b/test/date/test_date_strftime.rb @@ -406,6 +406,8 @@ class TestDateStrftime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/date/test_date_strftime.rb#L406 assert_equal('S64.01.07', Date.parse('1989-01-07').jisx0301) assert_equal('H01.01.08', Date.parse('1989-01-08').jisx0301) assert_equal('H18.09.01', Date.parse('2006-09-01').jisx0301) + assert_equal('H31 (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/