epoch Prime

+- HP Forums (http://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: epoch Prime (/thread-9859.html)



epoch Prime - salvomic - 01-05-2018 11:00 AM

hi,
following an idea by Bill Duncan (see this MoHPC thread), is there anyone so kind to help me to make a program (or a set of commands) to get:
• current date and time -> epoch (Unix)
• epoch -> current date and time
• any date_and_time -> epoch and reverse
giving to the user the option to set Time Zone also?
The program should also avoid the "2038 bit overflow"...
The goal is to use when possible built-in commands, for precision purpose.

It a simple but useful idea to control epoch offline (when one is controlling logs or other things or for funny). I'm using the Bill's program in HP-41CX and also in Free42s. Now I thought to make something for the Prime, but not only by myself Smile

Thank you
Salvo

RE: epoch Prime - salvomic - 01-06-2018 09:45 AM

(01-06-2018 09:28 AM)pier4r Wrote:  Little nitpick.

When one use constants, like 86400, at first it is ok to plaster them all over to get things done, but then (as you realized) a little typo may produce problems.

...

Edit: of course, I assume you are typing your programs from a computer with a real keyboard. If you type them on the prime or on a touchscreen, I understand completely the short variables and the limited comments.

yes, this last paragraph of your post is the real explanation of why most of us here use short vars and few comments...
I understand your thoughts and appreciate, however just now the program is still not finished, so the most important thing is if it works and how, then we can refactor variables and put in some other comments...
As I wrote in the first post, our goal is to make a program the more concise (and, if possible, as you wrote, the more user-friendly).

First of all we must solve some problems:
• to use less parameters (I mean reuse the parameters for date and time without "magic numbers" for the case in which the user wants to use the current date and time or he/she wants to input another date)
• to decide if it is important (or not so important) to give also "local time" epoch, or only (and always) GMT/UTC epoch (as it is now in the program).
• to decide if there is a reason to "complicate" the program, with calculation of leap seconds or other important considerations or not...

Salvo

EDIT:
as pier4r suggested, the version without "magic constants" it:
Code:

// 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(da,ti, zone)
BEGIN
LOCAL sec_in_day:=86400;
LOCAL sec_in_hour:=3600;
LOCAL unix_epoch_start_time:=1970.0101;
IF da==-1 THEN da:=Date; END;
IF ti==-1 THEN ti:=Time; ELSE ti:=HMS→(ti); END;
ti:=ti-zone;
RETURN sec_in_day*(DDAYS(unix_epoch_start_time,da))+sec_in_hour*HMS→(ti);
END;

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