Post Reply 
Epoch (Unix time)
01-06-2018, 06:07 PM (This post was last modified: 01-06-2018 08:47 PM by salvomic.)
Post: #1
Epoch (Unix time)
hi,
here it is a possible implementation of epoch (unix time, second's number from 1970 Jan 1 at midnight) for the Prime.
See here for the definition.

epoch(date, time, timezone) returns epoch number given date (i.e. 2018.0106, -1 for current Date), time (i.e. 15°22'30'', -1 for current Time), time zone (0 for UTC/GMT, +1, -1...)
epoch2date(epoch, timezone) returns a list with date and time (format: {2018.0106, 15°22'30''}) given epoch number of seconds and timezone.

Every values converted in GMT/UTC time

Code:

// v. 1.0 :: Epoch Unix time from 1970 Jan 1. Inspired by a Bill Duncan's program for HP-41CX
// Thanks to Dieter, Didier Lachieze, Stephen G1CMZ, pier4r...

// Epoch seconds in UTC time
// Input: da = date (i.e. 2018.0523), ti = time (i.e. 13°15'30''), zone UTC (0 GMT)
// -1 as current date and current time
EXPORT epoch(dat,tim, zone)
BEGIN
LOCAL sec_in_day:=24*60*60; //86400
LOCAL sec_in_hour:=60*60; //3600
LOCAL unix_epoch_start_time:=1970.0101;
IF dat==-1 THEN dat:=Date; END;
IF tim==-1 THEN tim:=Time; ELSE tim:=HMS→(tim); END;
tim:=tim-zone;
RETURN sec_in_day*(DDAYS(unix_epoch_start_time,dat))+sec_in_hour*HMS→(tim);
END;

// Given an epoch and a time zone, the program returns date and time
EXPORT epoch2date(epoc, zone)
BEGIN
LOCAL sec_in_day:=24*60*60; //86400
LOCAL sec_in_hour:=60*60; //3600
LOCAL unix_epoch_start_time:=1970.0101;
LOCAL dat, tim;
epoc:=epoc+sec_in_hour*zone;
dat:=DATEADD(unix_epoch_start_time, IP(epoc/sec_in_day));
tim:=(epoc MOD sec_in_day)/sec_in_hour;
RETURN({dat, →HMS(tim)});
END;

Enjoy!

Salvo

***
Original idea: epoch for HP-41CX

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Epoch (Unix time) - salvomic - 01-06-2018 06:07 PM
RE: Epoch (Unix time) - salvomic - 01-06-2018, 09:39 PM



User(s) browsing this thread: 1 Guest(s)