diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-04-13 15:14:02 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-04-13 15:14:02 +0000 |
commit | d6387f706834957d48e61716bc8053ec8b6aa6c5 (patch) | |
tree | 788d459744449f124b2d525a2c99218b0ec3b3b5 /ext/date | |
parent | d8cac3300f383a7b06c3958b30d2a5f2ee383c12 (diff) |
merge revision(s) 66745,67397,67398,67399,67400,67409,67410,67411,67412,67425,67426,67473,67474: [Backport #15742]
Bump version to date-2.0.0.
I forgot to change it when Ruby 2.6.0 was released.
date_parse.c: extract Japanese era initials
* expand tabs.
date_parse.c: removed 'r' which is not in JIS X 0301 yet
Added tests for end of Heisei
date: use del_hash to extract an element destructively
* expand tabs.
date_parse.c: renamed JAPANESE prefix as JISX0301
date_parse.c: name JISX0301_DEFAULT_ERA
date: make zone a substring to copy encoding and taintedness
* expand tabs.
date_core.c: [DOC] Heisei will be assumed if no-era [ci skip]
date: support for Reiwa, new Japanese era
[Feature #15742]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67527 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/date')
-rw-r--r-- | ext/date/date_core.c | 28 | ||||
-rw-r--r-- | ext/date/date_parse.c | 51 | ||||
-rw-r--r-- | ext/date/date_strptime.c | 6 |
3 files changed, 41 insertions, 44 deletions
diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 49027e92b1..67ed6171a7 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -3692,7 +3692,7 @@ rt_rewrite_frags(VALUE hash) { VALUE seconds; - seconds = ref_hash("seconds"); + seconds = del_hash("seconds"); if (!NIL_P(seconds)) { VALUE offset, d, h, min, s, fr; @@ -3717,7 +3717,6 @@ rt_rewrite_frags(VALUE hash) set_hash("min", min); set_hash("sec", s); set_hash("sec_fraction", fr); - del_hash("seconds"); } return hash; } @@ -4307,16 +4306,6 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass) hash = date__parse(vstr, vcomp); - { - VALUE zone = ref_hash("zone"); - - if (!NIL_P(zone)) { - rb_enc_copy(zone, vstr); - OBJ_INFECT(zone, vstr); - set_hash("zone", zone); - } - } - return hash; } @@ -4619,6 +4608,10 @@ date_s__jisx0301(VALUE klass, VALUE str) * some typical JIS X 0301 formats. * * 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 ...> */ static VALUE date_s_jisx0301(int argc, VALUE *argv, VALUE klass) @@ -7046,10 +7039,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; } @@ -8156,6 +8153,11 @@ datetime_s_httpdate(int argc, VALUE *argv, VALUE klass) * * 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 ...> */ static VALUE datetime_s_jisx0301(int argc, VALUE *argv, VALUE klass) diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index 69ac37f0a3..e0634c39f1 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -1212,6 +1212,9 @@ parse_iso2(VALUE str, VALUE hash) 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) 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) { 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 @@ -1859,30 +1863,26 @@ parse_ddd_cb(VALUE m, VALUE hash) set_hash("zone", s5); if (*cs5 == '[') { - VALUE vbuf = 0; - char *buf = ALLOCV_N(char, vbuf, l5 + 1); - char *s1, *s2, *s3; + const char *s1, *s2; VALUE zone; - memcpy(buf, cs5, l5); - buf[l5 - 1] = '\0'; - - s1 = buf + 1; - s2 = strchr(buf, ':'); + l5 -= 2; + s1 = cs5 + 1; + s2 = memchr(s1, ':', l5); if (s2) { - *s2 = '\0'; s2++; + zone = rb_str_subseq(s5, s2 - cs5, l5 - (s2 - s1)); + s5 = rb_str_subseq(s5, 1, s2 - s1); } - if (s2) - s3 = s2; - else - s3 = s1; - zone = rb_str_new2(s3); + else { + zone = rb_str_subseq(s5, 1, l5); + if (isdigit((unsigned char)*s1)) + s5 = rb_str_append(rb_str_new_cstr("+"), zone); + else + s5 = zone; + } set_hash("zone", zone); - if (isdigit((unsigned char)*s1)) - *--s1 = '+'; - set_hash("offset", date_zone_to_diff(rb_str_new2(s1))); - ALLOCV_END(vbuf); + set_hash("offset", date_zone_to_diff(s5)); } RB_GC_GUARD(s5); } @@ -2175,7 +2175,7 @@ date__parse(VALUE str, VALUE comp) #endif { - if (RTEST(ref_hash("_bc"))) { + if (RTEST(del_hash("_bc"))) { VALUE y; y = ref_hash("cwyear"); @@ -2190,7 +2190,7 @@ date__parse(VALUE str, VALUE comp) } } - if (RTEST(ref_hash("_comp"))) { + if (RTEST(del_hash("_comp"))) { VALUE y; y = ref_hash("cwyear"); @@ -2213,9 +2213,6 @@ date__parse(VALUE str, VALUE comp) } - del_hash("_bc"); - del_hash("_comp"); - { VALUE zone = ref_hash("zone"); if (!NIL_P(zone) && NIL_P(ref_hash("offset"))) @@ -2954,7 +2951,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])); @@ -2979,7 +2976,7 @@ static int 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/date_strptime.c b/ext/date/date_strptime.c index 4383eb6fa1..26d9fd11bf 100644 --- a/ext/date/date_strptime.c +++ b/ext/date/date_strptime.c @@ -669,7 +669,7 @@ date__strptime(const char *str, size_t slen, if (fail_p()) return Qnil; - cent = ref_hash("_cent"); + cent = del_hash("_cent"); if (!NIL_P(cent)) { VALUE year; @@ -679,10 +679,9 @@ date__strptime(const char *str, size_t slen, year = ref_hash("year"); if (!NIL_P(year)) set_hash("year", f_add(year, f_mul(cent, INT2FIX(100)))); - del_hash("_cent"); } - merid = ref_hash("_merid"); + merid = del_hash("_merid"); if (!NIL_P(merid)) { VALUE hour; @@ -691,7 +690,6 @@ date__strptime(const char *str, size_t slen, hour = f_mod(hour, INT2FIX(12)); set_hash("hour", f_add(hour, merid)); } - del_hash("_merid"); } return hash; |