Skip to content

Commit

Permalink
FEAT: support to date! integer! and to integer! date! using unixt…
Browse files Browse the repository at this point in the history
…ime as an integer value

resolves: Oldes/Rebol-issues#2456
  • Loading branch information
Oldes committed Sep 20, 2021
1 parent 85de1db commit d797a93
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
36 changes: 36 additions & 0 deletions src/core/t-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "sys-core.h"

static const REBI64 DAYS_OF_JAN_1ST_1970 = 719468; // number of days for 1st January 1970


/***********************************************************************
**
Expand Down Expand Up @@ -309,6 +311,36 @@
}


/***********************************************************************
**
*/ REBCNT Date_To_Timestamp(REBVAL *date)
/*
** Return the unix time stamp for a specific date value.
**
***********************************************************************/
{
REBDAT d = VAL_DATE(date);
REBI64 epoch = (Days_Of_Date(d.date.day, d.date.month, d.date.year) - DAYS_OF_JAN_1ST_1970) * SECS_IN_DAY;
return epoch + ((VAL_TIME(date) + 500000000) / SEC_SEC);
}

/***********************************************************************
**
*/ void Timestamp_To_Date(REBVAL *date, REBI64 epoch)
/*
** Set Rebol date from the unix time stamp epoch.
**
***********************************************************************/
{
REBI64 days = (epoch / SECS_IN_DAY) + DAYS_OF_JAN_1ST_1970;

VAL_SET(date, REB_DATE);
Date_Of_Days(days, &VAL_DATE(date));
VAL_TIME(date) = TIME_SEC((epoch % 86400));
VAL_ZONE(date) = 0;
}


/***********************************************************************
**
*/ void Normalize_Time(REBI64 *sp, REBINT *dp)
Expand Down Expand Up @@ -883,6 +915,10 @@
return R_RET;
}
}
else if (IS_INTEGER(arg)) {
Timestamp_To_Date(D_RET, VAL_INT64(arg));
return R_RET;
}
// else if (IS_NONE(arg)) {
// secs = nsec = day = month = year = tz = 0;
// goto fixTime;
Expand Down
1 change: 1 addition & 0 deletions src/core/t-integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
else if (IS_CHAR(val))
num = VAL_CHAR(val);
// else if (IS_NONE(val)) num = 0;
else if (IS_DATE(val)) num = Date_To_Timestamp(val);
else if (IS_TIME (val)) num = SECS_IN(VAL_TIME(val));
else goto is_bad;
break;
Expand Down
14 changes: 2 additions & 12 deletions src/mezz/codec-unixtime.reb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ register-codec [
][
if string? epoch [ epoch: debase epoch 16 ]
if binary? epoch [ epoch: to integer! epoch ]
days: to integer! tmp: epoch / 86400
hours: to integer! time: (tmp - days) * 24
minutes: to integer! tmp: (time - hours) * 60
seconds: to integer! 0.5 + ((tmp - minutes) * 60)
time: to time! ((((hours * 60) + minutes) * 60) + seconds)
result: 1-Jan-1970 + days + time
result: to date! epoch
unless utc [
result: result + now/zone
result/zone: now/zone
Expand All @@ -44,12 +39,7 @@ register-codec [
date [date!]
/as type [word! datatype!] {one of: [string! binary! integer!]}
][
time: any [date/time 0:0:0]
unix: ((date - 1-1-1970) * 86400)
+ (time/hour * 3600)
+ (time/minute * 60)
+ time/second
- to integer! (any [date/zone 0])
unix: to integer! date
if as [
type: to word! type
binary/write bin: #{} [ui32 :unix]
Expand Down

0 comments on commit d797a93

Please # to comment.