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
01-06-2018, 09:39 PM (This post was last modified: 01-07-2018 09:35 AM by salvomic.)
Post: #2
RE: Epoch (Unix time)
In this version the user can simply input Date and/or Time and his/her timezone to get current date and time. User can write directly epoc(Date, Time, -5) or input those variables via Vars Catalog.
Otherwise he/she can input a date (format of the Prime: 2018.0106), a time (use the template in shift-E to input i.e. 12°40'32'') and the timezone (timezone is relative to UTC in hours)...

Code:

// Epoch v. 1.1 :: 
// 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: date (e.g. 2018.0523), time (e.g. 13°15'30'')
//     >> or input: Date for current date, and Time for current time (from Var catalog)
// input: timezone (relative to UTC in hours, 0 for GMT/UTC)
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 tim<>Time THEN 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
// Input: epoch in seconds, timezone (0 for GMT / UTC) 
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;

∫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 




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