Hi,
> I worked out an Date and Time to Epoch conversion recently. I’d be interested
> if anyone can find a case where it doesn’t work:
>
>
>
> C_DATE($1)
> C_TIME($2)
> C_LONGINT($0)
>
> C_DATE($vd_Current)
> C_TIME($vh_Current)
> C_TIME($vh_ISO_GMT)
> C_LONGINT($vl_Unix_Timestamp_Offset)
>
>
> $vd_Current:=Current date
> $vh_Current:=Current time
> $vh_ISO_GMT:=Time(Substring(String($vd_Current;ISO date
> GMT;$vh_Current);12;8))
> $vl_Unix_Timestamp_Offset:=($vh_ISO_GMT-$vh_Current)*1
>
> $0:=($1-Date("01/01/1970"))*(60*60*24)+($2*1)+$vl_Unix_Timestamp_Offset
Avoid using Date() to convert a string to a date because this will rely on the
date being formatted according to you current system setting...
I use this :
C_LONGINT($0;$vl_epoch)
C_DATE($1;$vd_date)
C_TIME($2;$vh_time)
ASSERT(Count parameters>1;"requires 2 parameters")
ASSERT($1#!00/00/0000!;"$1 cannot be a null date")
$vd_date:=$1
$vh_time:=$2
C_TEXT($vt_timestampUtc)
$vt_timestampUtc:=String($vd_date;ISO Date GMT;$vh_time)
// "2017-03-26T18:07:16Z" at 20:07:16 french time zone (just moved to summer
time on 26th march 2017)
// NOTE : in v12, v13, v14 there is a bug : on 26/03/2017 (day when the
daylight shift changes) it returns 19:07:16Z when it should return 18:07:16Z.
Ok for subsequent days...
// seems ok in v15 - tested on Mac OS 10.8
$vt_timestampUtc:=Substring($vt_timestampUtc;1;19) // remove the "Z"
// "2017-03-26T18:07:16Z" => "2017-03-26T18:07:16"
// XML DECODE will convert "2017-03-26T18:07:16" into !2017-03-26! date and
?18:07:16? time
// works ok in 4D v12+ : another great trick from Miyako to parse a timestamp
in "YYY-MM-DDTHH:Mi:SS" ;-)
C_DATE($vd_dateUtc)
C_TIME($vh_timeUtc)
XML DECODE($vt_timestampUtc;$vd_dateUtc)
XML DECODE($vt_timestampUtc;$vh_timeUtc)
// 86400 = 60*60*24
$vl_epoch:=(($vd_dateUtc-!01/01/1970! )*86400)+$vh_timeUtc
$0:=$vl_epoch
In OS X Terminal, you can use this to check / compare with system epoch
/bin/date +%s
So you could use a function like this :
// this is OS X only !!!
C_LONGINT($0;$vl_epoch)
C_TEXT($vt_cmd;$vt_in;$vt_out;$vt_err)
$vt_cmd:="/bin/date +%s"
LAUNCH EXTERNAL PROCESS($vt_cmd;$vt_in;$vt_out;$vt_err)
ASSERT(ok=1;"LAUNCH EXTERNAL PROCESS with command \""+$vt_cmd+"\" failed")
$vl_epoch:=Num(Replace string($vt_out;"\n";"";*))
$0:=$vl_epoch
HTH
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ: http://lists.4d.com/faqnug.html
Archive: http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:[email protected]
**********************************************************************