Skip to content

Commit

Permalink
FEAT: implemented date creation using numbers for the time parts
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Sep 6, 2024
1 parent d0ce370 commit 1c1374b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/core/t-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,13 @@ static const REBI64 DAYS_OF_JAN_1ST_1970 = 719468; // number of days for 1st Jan
REBI64 secs = NO_TIME;
REBINT tz = 0;
REBDAT date;
REBCNT year, month, day;
REBCNT year, month, day, hour, minute;
REBDEC second;

if (IS_DATE(arg)) {
*val = *arg;
if (IS_TIME(++arg)) {
arg++;
if (IS_TIME(arg) || IS_INTEGER(arg)) {
// make date! [1-1-2000 100:0]
// we must get date parts here so can be used
// for time normalization later
Expand Down Expand Up @@ -634,13 +636,31 @@ static const REBI64 DAYS_OF_JAN_1ST_1970 = 719468; // number of days for 1st Jan
secs = VAL_TIME(arg);
arg++;
}
else if (IS_INTEGER(arg)) {
hour = VAL_UNT64(arg++);
if (!IS_INTEGER(arg)) return FALSE;
minute = VAL_UNT64(arg++);
if (IS_INTEGER(arg)) {
second = (REBDEC)VAL_INT64(arg);
}
else if (IS_DECIMAL(arg)) {
second = VAL_DECIMAL(arg);
}
else return FALSE;

if (hour > 23 || minute >= 60 || second >= 60.0) return FALSE;
secs = HOUR_TIME(hour) + MIN_TIME(minute) + DEC_TO_SECS(second);
arg++;

}

if (IS_TIME(arg)) {
tz = (REBINT)(VAL_TIME(arg) / (ZONE_MINS * MIN_SEC));
if (tz < -MAX_ZONE || tz > MAX_ZONE) Trap_Range(arg);
arg++;
}


if (!IS_END(arg)) return FALSE;

Normalize_Time(&secs, (REBINT*)&day);
Expand Down
17 changes: 17 additions & 0 deletions src/tests/units/make-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ Rebol [
--assert 31-Aug-2132/00:00:00 = to date! 99999.0
--assert 01-Sep-2132/00:00:00 = make date! 100000.0
--assert 01-Sep-2132/00:00:00 = to date! 100000.0
--test-- "make/to date! block!"
--assert 1-Feb-2000 == make date! [2000 2 1]
;@@ https://github.com/Oldes/Rebol-issues/issues/2619
--assert 1-Feb-2000/1:02:03 == make date! [2000 2 1 1 2 3]
--assert 1-Feb-2000/1:02:03.4 == make date! [2000 2 1 1 2 3.4]
--assert 1-Feb-2000/1:02:03 == make date! [1-Feb-2000 1 2 3]
--assert 1-Feb-2000/1:02:03.4 == make date! [1-Feb-2000 1 2 3.4]
;; time must be in correct range < 24:0:0
--assert error? try [make date! [2000 2 1 24 0 0]]
--assert error? try [make date! [2000 2 1 23 60 0]]
--assert error? try [make date! [2000 2 1 23 59 60]]
--assert 1-Feb-2000/23:59:59.99 == make date! [2000 2 1 23 59 59.99]
;; including timezone...
--assert 1-Feb-2000/1:02:03+2:00 == make date! [2000 2 1 1 2 3 2:00]
--assert 1-Feb-2000/1:02:03.4+2:00 == make date! [2000 2 1 1 2 3.4 2:00]
--assert 1-Feb-2000/1:02:03+2:00 == make date! [1-Feb-2000 1 2 3 2:00]
--assert 1-Feb-2000/1:02:03.4+2:00 == make date! [1-Feb-2000 1 2 3.4 2:00]
===end-group===

===start-group=== "make/to integer"
Expand Down

0 comments on commit 1c1374b

Please sign in to comment.