From 59c8daa4a0ace710e0738e5654d473fabf98d1d8 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 1 Apr 2019 05:21:36 +0000 Subject: [PATCH 1/6] date_parse.c: extract Japanese era initials git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67397 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/date/date_parse.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index 69ac37f..c9f9307 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -1212,6 +1212,8 @@ parse_iso2(VALUE str, VALUE hash) return 1; } +#define JAPANESE_ERA_INITIALS "mtshr" + static int gengo(int c) { @@ -1252,11 +1254,11 @@ parse_jis(VALUE str, VALUE hash) { static const char pat_source[] = #ifndef TIGHT_PARSER - "\\b([mtsh])(\\d+)\\.(\\d+)\\.(\\d+)" + "\\b([" JAPANESE_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" #else BOS FPW_COM FPT_COM - "([mtsh])(\\d+)\\.(\\d+)\\.(\\d+)" + "([" JAPANESE_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" TEE_FPT COM_FPW EOS #endif @@ -2979,7 +2981,7 @@ static int jisx0301(VALUE str, VALUE hash) { static const char pat_source[] = - "\\A\\s*([mtsh])?(\\d{2})\\.(\\d{2})\\.(\\d{2})" + "\\A\\s*([" JAPANESE_ERA_INITIALS "])?(\\d{2})\\.(\\d{2})\\.(\\d{2})" "(?:t" "(?:(\\d{2}):(\\d{2})(?::(\\d{2})(?:[,.](\\d*))?)?" "(z|[-+]\\d{2}(?::?\\d{2})?)?)?)?\\s*\\z"; From 03e66cb985ba1e640c7b60985a3b939e6284bbe9 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 1 Apr 2019 05:28:25 +0000 Subject: [PATCH 2/6] date_parse.c: removed 'r' which is not in JIS X 0301 yet git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67399 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/date/date_parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index c9f9307..17bf7eb 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -1212,7 +1212,7 @@ parse_iso2(VALUE str, VALUE hash) return 1; } -#define JAPANESE_ERA_INITIALS "mtshr" +#define JAPANESE_ERA_INITIALS "mtsh" static int gengo(int c) From b4718844b8eed009f77e3a3a1724d5594dee9f1e Mon Sep 17 00:00:00 2001 From: svn Date: Mon, 1 Apr 2019 05:21:39 +0000 Subject: [PATCH 3/6] * expand tabs. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67398 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/date/date_parse.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index 17bf7eb..fcda847 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -1254,11 +1254,11 @@ parse_jis(VALUE str, VALUE hash) { static const char pat_source[] = #ifndef TIGHT_PARSER - "\\b([" JAPANESE_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" + "\\b([" JAPANESE_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" #else BOS FPW_COM FPT_COM - "([" JAPANESE_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" + "([" JAPANESE_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" TEE_FPT COM_FPW EOS #endif @@ -2981,7 +2981,7 @@ static int jisx0301(VALUE str, VALUE hash) { static const char pat_source[] = - "\\A\\s*([" JAPANESE_ERA_INITIALS "])?(\\d{2})\\.(\\d{2})\\.(\\d{2})" + "\\A\\s*([" JAPANESE_ERA_INITIALS "])?(\\d{2})\\.(\\d{2})\\.(\\d{2})" "(?:t" "(?:(\\d{2}):(\\d{2})(?::(\\d{2})(?:[,.](\\d*))?)?" "(z|[-+]\\d{2}(?::?\\d{2})?)?)?)?\\s*\\z"; From 82006db340e06e02fdd89c0518eec090fa7b409f Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 2 Apr 2019 04:36:33 +0000 Subject: [PATCH 4/6] date_parse.c: renamed JAPANESE prefix as JISX0301 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67411 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/date/date_parse.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index fcda847..5a6273a 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -1212,7 +1212,7 @@ parse_iso2(VALUE str, VALUE hash) return 1; } -#define JAPANESE_ERA_INITIALS "mtsh" +#define JISX0301_ERA_INITIALS "mtsh" static int gengo(int c) @@ -1254,11 +1254,11 @@ parse_jis(VALUE str, VALUE hash) { static const char pat_source[] = #ifndef TIGHT_PARSER - "\\b([" JAPANESE_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" + "\\b([" JISX0301_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" #else BOS FPW_COM FPT_COM - "([" JAPANESE_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" + "([" JISX0301_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)" TEE_FPT COM_FPW EOS #endif @@ -2981,7 +2981,7 @@ static int jisx0301(VALUE str, VALUE hash) { static const char pat_source[] = - "\\A\\s*([" JAPANESE_ERA_INITIALS "])?(\\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"; From 136c5cf444bfc41f6544bdbc68df5b1308dcbe8f Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 2 Apr 2019 04:36:34 +0000 Subject: [PATCH 5/6] date_parse.c: name JISX0301_DEFAULT_ERA git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67412 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/date/date_parse.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index 5a6273a..783952f 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -1213,6 +1213,7 @@ parse_iso2(VALUE str, VALUE hash) } #define JISX0301_ERA_INITIALS "mtsh" +#define JISX0301_DEFAULT_ERA 'H' /* obsolete */ static int gengo(int c) @@ -2956,7 +2957,7 @@ jisx0301_cb(VALUE m, VALUE hash) 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])); From 21c7ea35a732e883fc32fbb16e735f435b68adf0 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 9 Apr 2019 01:27:36 +0000 Subject: [PATCH 6/6] date: support for Reiwa, new Japanese era [Feature #15742] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67474 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/date/date_core.c | 6 ++- ext/date/date_parse.c | 3 +- test/date/test_date_parse.rb | 72 +++++++++++++++++++++++++++++++++ test/date/test_date_strftime.rb | 7 +++- 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 918e612..c8d37e9 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -7190,10 +7190,14 @@ jisx0301_date_format(char *fmt, size_t size, VALUE jd, VALUE y) 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; } diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index 783952f..f06c07b 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -1212,7 +1212,7 @@ parse_iso2(VALUE str, VALUE hash) return 1; } -#define JISX0301_ERA_INITIALS "mtsh" +#define JISX0301_ERA_INITIALS "mtshr" #define JISX0301_DEFAULT_ERA 'H' /* obsolete */ static int @@ -1225,6 +1225,7 @@ gengo(int c) 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; diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb index 5bc2ceb..d1163b2 100644 --- a/test/date/test_date_parse.rb +++ b/test/date/test_date_parse.rb @@ -1020,6 +1020,15 @@ def test__jisx0301 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 @@ def test__jisx0301 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 @@ def test_jisx0301 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 a33eaa3..dc237a9 100644 --- a/test/date/test_date_strftime.rb +++ b/test/date/test_date_strftime.rb @@ -406,6 +406,8 @@ def test__different_format 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.04.30', Date.parse('2019-04-30').jisx0301) + assert_equal('R01.05.01', Date.parse('2019-05-01').jisx0301) %w(M06.01.01 M45.07.29 @@ -414,7 +416,10 @@ def test__different_format S01.12.25 S64.01.07 H01.01.08 - H18.09.01).each do |s| + H18.09.01 + H31.04.30 + R01.05.01 + ).each do |s| assert_equal(s, Date.parse(s).jisx0301) end