Found this useful for making date and time calculations, as you can add or subtract any number of seconds to or from a given date and time.
Base for this ist the Unix Timestamp or Epoch: basically just the seconds since midnight 1970-01-01.
Below you will find 2 methods to convert a date and time into Epoch (a longint) and to convert Epoch back into an ISO date-time string that can easily be used with the Date(string) and Time(string) 4D commands.
It takes care of your time zone, so the Epoch generated is valid outside the context of your code, also you can use it to convert any epoch you might get from somewhere else, taking into account local time zones.
What it does not yet do is correctly handle dates before 1970.
Well, Date_ToEpoch works with dates prior to 1970, the other way round is a bit off, something screws up the GMT adjustment, will need to check that another time.
Of course you are welcome to repair that, if you do, please share it.
// ----------------------------------------------------
// Method: Date_ToEpoch
// ----------------------------------------------------
// Call: Epoch:=Date_ToEpoch(Date;Time)
// ----------------------------------------------------
// UserName (OS): Alexander Heintz
// Date and Time: 24.03.17, 12:49:26
// ----------------------------------------------------
// Does:
// converts a given date and time into UNIX Epoch
// thats seconds since midnight 1970 01 01
// ----------------------------------------------------
// Parameters:
// -> $1 date the date
// -> $2 time the time
// <- $0 real unix epoch
// ----------------------------------------------------
// Parameter Definition
C_DATE($1)
C_TIME($2)
C_REAL($0)
// ----------------------------------------------------
// Local Variable Definition
C_DATE($d_Date)
C_TIME($h_Time)
C_LONGINT($l_Days)
C_LONGINT($l_Hours)
C_LONGINT($l_Minutes)
C_LONGINT($l_Time)
C_REAL($r_Epoch)
// ----------------------------------------------------
// Parameter Assignment
$d_Date:=$1
$h_Time:=$2
// ----------------------------------------------------
//GMT Correction
$t_Timestamp:=String($d_Date;ISO date GMT;$h_Time)
//2017-03-24T12:04:55Z
//just strip the Z at the end
$t_Timestamp:=Substring($t_Timestamp;1;19)
//2017-03-24T12:01:17
//now get the GMT date and time for calculations
$d_Date:=Date($t_Timestamp)
$h_Time:=Time($t_Timestamp)
//get number of days
$l_Days:=$d_Date-!1970-01-01!
//start calculating the unix time
//add the days
$r_Epoch:=$l_Days*86400
$l_Time:=$h_Time*1 //make it a longint
//add the hours
$l_Hours:=Trunc($l_Time/3600;0)
$r_Epoch:=$r_Epoch+($l_Hours*3600)
$l_Time:=Mod($l_Time;3600)
//add the minutes
$l_Minutes:=Trunc($l_Time/60;0)
$r_Epoch:=$r_Epoch+($l_Minutes*60)
$l_Time:=Mod($l_Time;60)
//add the seconds
$r_Epoch:=$r_Epoch+$l_Time
$0:=$r_Epoch
And back again:
// ----------------------------------------------------
// Method: Date_FromEpoch
// ----------------------------------------------------
// Call: Timestring:=Date_FromEpoch(Epoch)
// ----------------------------------------------------
// [ ] theadsafe
// ----------------------------------------------------
// UserName (OS): Alexander Heintz
// Date and Time: 24.03.17, 13:10:11
// ----------------------------------------------------
// Does:
// returns a timestring (ISO Date) for the Epoch given
// the timestring returned is in local time zone
// ----------------------------------------------------
// Parameters:
// -> $1 real the Epoch to convert
// <- $0 text the ISO timestring
// ----------------------------------------------------
// Parameter Definition
C_REAL($1)
C_TEXT($0)
// ----------------------------------------------------
// Local Variable Definition
C_DATE($d_Date)
C_TIME($h_Time)
C_LONGINT($l_Days)
C_LONGINT($l_Seconds)
C_REAL($r_Epoch)
C_TEXT($t_TimeStamp)
// ----------------------------------------------------
// Parameter Assignment
$r_Epoch:=$1
// ----------------------------------------------------
$l_Days:=Trunc($r_Epoch/86400;0)
$l_Seconds:=Mod($r_Epoch;86400)
$d_Date:=!1970-01-01!+$l_Days
$h_Time:=Time(Time string($l_Seconds))
$t_TimeStamp:=String($d_Date;ISO date;$h_Time)+"Z"
$t_TimeStamp:=String(Date($t_TimeStamp);ISO date;Time($t_TimeStamp))
$0:=$t_TimeStamp
Hope its useful for someone.