Skip to content

Commit

Permalink
FEAT: Way to combine DATE! and TIME! without a variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Nov 22, 2020
1 parent 432e8a3 commit 08bdedf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/core/t-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,15 @@

if (IS_DATE(arg)) {
*val = *arg;
if (IS_TIME(++arg)) {
// make date! [1-1-2000 100:0]
// we must get date parts here so can be used
// for time normalization later
day = VAL_DAY(val) - 1;
month = VAL_MONTH(val) - 1;
year = VAL_YEAR(val);
goto set_time;
}
return TRUE;
}

Expand Down Expand Up @@ -463,6 +472,7 @@
day--;
month--;

set_time:
if (IS_TIME(arg)) {
secs = VAL_TIME(arg);
arg++;
Expand Down Expand Up @@ -812,7 +822,7 @@
bp = Qualify_String(arg, 45, &len, FALSE); // can trap, ret diff str
if (Scan_Date(bp, len, D_RET)) return R_RET;
}
else if (ANY_BLOCK(arg) && VAL_BLK_LEN(arg) >= 3) {
else if (ANY_BLOCK(arg) && VAL_BLK_LEN(arg) >= 1) {
if (MT_Date(D_RET, VAL_BLK_DATA(arg), REB_DATE)) {
return R_RET;
}
Expand Down
16 changes: 16 additions & 0 deletions src/tests/units/date-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ Rebol [
===end-group===

===start-group=== "TO DATE!"
--test-- "make date!"
--assert 1-Feb-0003 = make date! [1 2 3]
--assert 1-Feb-0003/4:00 = make date! [1 2 3 4:0]
--assert 1-Feb-0003/4:00+5:00 = make date! [1 2 3 4:0 5:0]
;@@ https://github.com/Oldes/Rebol-wishes/issues/1
--assert 1-Jan-2000 = make date! [1-1-2000]
--assert 1-Jan-2000/10:00 = make date! [1-1-2000 10:0]
--assert 1-Jan-2000/10:00+2:00 = make date! [1-1-2000 10:0 2:0]
--assert 5-Jan-2000/4:00 = make date! [1-1-2000 100:0]

--test-- "invalid input"
;@@ https://github.com/Oldes/Rebol-issues/issues/878
--assert error? try [to date! "31-2-2009"]
Expand All @@ -56,6 +66,12 @@ Rebol [
--assert 1-Feb-0003 = #[date! 1 2 3]
--assert 1-Feb-0003/4:00 = #[date! 1 2 3 4:0]
--assert 1-Feb-0003/4:00+5:00 = #[date! 1 2 3 4:0 5:0]
;@@ https://github.com/Oldes/Rebol-wishes/issues/1
--assert 1-Jan-2000 = #[date! 1-1-2000]
--assert 1-Jan-2000/10:00 = #[date! 1-1-2000 10:0]
--assert 1-Jan-2000/10:00+2:00 = #[date! 1-1-2000 10:0 2:0]
--assert 5-Jan-2000/4:00 = #[date! 1-1-2000 100:0]

--test-- "#[date!] invalid"
--assert error? try [load {#[date!]}]
--assert error? try [load {#[date! 1]}]
Expand Down

0 comments on commit 08bdedf

Please sign in to comment.