diff --git a/ext/date/date_core.c b/ext/date/date_core.c index d1605bb..09bf0c7 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -248,6 +248,11 @@ f_negative_p(VALUE x) #define date_sg_t double #endif +#define JULIAN_EPOCH_DATE "-4712-01-01" +#define JULIAN_EPOCH_DATETIME JULIAN_EPOCH_DATE "T00:00:00+00:00" +#define JULIAN_EPOCH_DATETIME_RFC3339 "Mon, 1 Jan -4712 00:00:00 +0000" +#define JULIAN_EPOCH_DATETIME_HTTPDATE "Mon, 01 Jan -4712 00:00:00 GMT" + /* A set of nth, jd, df and sf denote ajd + 1/2. Each ajd begin at * noon of GMT (assume equal to UTC). However, this begins at * midnight. @@ -4425,7 +4430,7 @@ date_s_strptime(int argc, VALUE *argv, VALUE klass) switch (argc) { case 0: - str = rb_str_new2("-4712-01-01"); + str = rb_str_new2(JULIAN_EPOCH_DATE); case 1: fmt = rb_str_new2("%F"); case 2: @@ -4478,8 +4483,7 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass) { VALUE vstr, vcomp, hash, opt; - rb_scan_args(argc, argv, "11:", &vstr, &vcomp, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "11:", &vstr, &vcomp, &opt); check_limit(vstr, opt); StringValue(vstr); if (!rb_enc_str_asciicompat_p(vstr)) @@ -4566,12 +4570,11 @@ date_s_parse(int argc, VALUE *argv, VALUE klass) { VALUE str, comp, sg, opt; - rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01"); + str = rb_str_new2(JULIAN_EPOCH_DATE); case 1: comp = Qtrue; case 2: @@ -4646,12 +4649,11 @@ date_s_iso8601(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01"); + str = rb_str_new2(JULIAN_EPOCH_DATE); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -4717,12 +4719,11 @@ date_s_rfc3339(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01T00:00:00+00:00"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -4786,12 +4787,11 @@ date_s_xmlschema(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01"); + str = rb_str_new2(JULIAN_EPOCH_DATE); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -4857,11 +4857,11 @@ date_s_rfc2822(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME_RFC3339); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -4925,11 +4925,11 @@ date_s_httpdate(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("Mon, 01 Jan -4712 00:00:00 GMT"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME_HTTPDATE); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -4997,12 +4997,11 @@ date_s_jisx0301(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01"); + str = rb_str_new2(JULIAN_EPOCH_DATE); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -8375,7 +8374,7 @@ datetime_s_strptime(int argc, VALUE *argv, VALUE klass) switch (argc) { case 0: - str = rb_str_new2("-4712-01-01T00:00:00+00:00"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME); case 1: fmt = rb_str_new2("%FT%T%z"); case 2: @@ -8423,12 +8422,11 @@ datetime_s_parse(int argc, VALUE *argv, VALUE klass) { VALUE str, comp, sg, opt; - rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01T00:00:00+00:00"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME); case 1: comp = Qtrue; case 2: @@ -8470,12 +8468,11 @@ datetime_s_iso8601(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01T00:00:00+00:00"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -8510,12 +8507,11 @@ datetime_s_rfc3339(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01T00:00:00+00:00"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -8550,12 +8546,11 @@ datetime_s_xmlschema(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01T00:00:00+00:00"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -8591,12 +8586,11 @@ datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME_RFC3339); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -8631,12 +8625,11 @@ datetime_s_httpdate(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("Mon, 01 Jan -4712 00:00:00 GMT"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME_HTTPDATE); case 1: sg = INT2FIX(DEFAULT_SG); } @@ -8676,12 +8669,11 @@ datetime_s_jisx0301(int argc, VALUE *argv, VALUE klass) { VALUE str, sg, opt; - rb_scan_args(argc, argv, "02:", &str, &sg, &opt); - if (!NIL_P(opt)) argc--; + argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt); switch (argc) { case 0: - str = rb_str_new2("-4712-01-01T00:00:00+00:00"); + str = rb_str_new2(JULIAN_EPOCH_DATETIME); case 1: sg = INT2FIX(DEFAULT_SG); } diff --git a/test/date/test_date_strftime.rb b/test/date/test_date_strftime.rb index 976e48f..6ad69c0 100644 --- a/test/date/test_date_strftime.rb +++ b/test/date/test_date_strftime.rb @@ -416,9 +416,10 @@ def test__different_format assert_equal(d2, DateTime.rfc3339('2001-02-03T04:05:06.123456+00:00', limit: 64)) assert_equal(d2, DateTime.jisx0301('H13.02.03T04:05:06.123456+00:00', limit: 64)) - assert_raise(ArgumentError) { DateTime.iso8601('2001-02-03T04:05:06.123456+00:00', limit: 1) } - assert_raise(ArgumentError) { DateTime.rfc3339('2001-02-03T04:05:06.123456+00:00', limit: 1) } - assert_raise(ArgumentError) { DateTime.jisx0301('H13.02.03T04:05:06.123456+00:00', limit: 1) } + exceeds = /string length \(\d+\) exceeds/ + assert_raise_with_message(ArgumentError, exceeds) {DateTime.iso8601('2001-02-03T04:05:06.123456+00:00', limit: 1)} + assert_raise_with_message(ArgumentError, exceeds) {DateTime.rfc3339('2001-02-03T04:05:06.123456+00:00', limit: 1)} + assert_raise_with_message(ArgumentError, exceeds) {DateTime.jisx0301('H13.02.03T04:05:06.123456+00:00', limit: 1)} %w(M06.01.01 M45.07.29