Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 36 additions & 44 deletions ext/date/date_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions test/date/test_date_strftime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down