HP Forums
Unix/Linux Epoch Time Converter.. - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: Unix/Linux Epoch Time Converter.. (/thread-9821.html)



Unix/Linux Epoch Time Converter.. - Bill Duncan - 12-31-2017 12:55 AM

Simple routine for unix epoch time conversions using hp-41 with time module (or 41CX et al). Useful if you're offline (on a train?) looking at log files..

Be sure to use a constant for reg 00 that is appropriate for your time zone and daylight savings time. (Seconds relative to GMT. Time zone given is Eastern Standard Time.)

Initialize constants by running start of program. Uses local labels in user mode.

"A" to get current epoch time.

Enter: Date [Enter] Time "B" to get epoch time for any given date/time.

The "C" routine will translate an epoch time to date/time values.

Test in user mode by pressing "A" then "C". This should get you the current date/time.

Cheers.

Code:

01 LBL "EPOCH"
02    -18000   STO 00
04    1.01197  STO 01
06    86400    STO 02
08    SF 27

09 LBL A
10   DATE 
11   TIME

12 LBL B
13   X<>Y
14   RCL 01
15   X<>Y
16   DDAYS
17   RCL 02
18   *
19   X<>Y
20   HR
21   3600
22   *
23   +
24   RCL 00
25   -
26 RTN

27 LBL C
28   RCL 00
29   +
30   ENTER
31   ENTER
32   RCL 02
33   /
34   INT
35   RCL 01
36   X<>Y
37   DATE+
38   CLA
39   ADATE
40   AVIEW
41   PSE
42   X<>Y
43   RCL 02
44   MOD
45   3600
46   /
47   HMS
48   CLA
49   ATIME
50   AVIEW
51 END



RE: Unix/Linux Epoch Time Converter.. - rprosperi - 12-31-2017 01:54 AM

For what machine(s)?

Appears to be for a 41, but could be others too...


RE: Unix/Linux Epoch Time Converter.. - Bill Duncan - 12-31-2017 02:00 AM

Sorry, yes, HP-41 with a time module (or 41cx etc.)

The Unix epoch time is the number of seconds past midnight, January 1st, 1970. Unlike real 32bit Unix/Linux (which will have problems in 2038 with signed int being 2^31 seconds), this should be good until the year 2286.. Wink

Note that the Time zone info for Register 00 is in seconds relative to GMT. The constant given is Eastern Standard time.


RE: Unix/Linux Epoch Time Converter.. - Thomas Okken - 12-31-2017 02:30 AM

Should work in Free42 and DM42 as well.

Nice post! I have something like this in Free42 at work; very handy when dealing with logs that show raw Unix time stamps, debugging Unix C code, etc. I use it a lot but never thought to post it.


RE: Unix/Linux Epoch Time Converter.. - Bill Duncan - 12-31-2017 04:17 PM

I've updated. Often all I want is the current epoch time, so I replaced the RTN after the setup into a "SF 27"; so now it falls through into the first routine. More useful I think.

Cheers, and Happy New Year.


RE: Unix/Linux Epoch Time Converter.. - salvomic - 12-31-2017 04:29 PM

(12-31-2017 02:30 AM)Thomas Okken Wrote:  Should work in Free42 and DM42 as well.
...

Any help to encode in raw for Free42 (or DM42)?
I'm using the SM encoder and I get this error "Invalid instruction ' -18000 STO 00' in line 01".
Thank you!

Salvo


RE: Unix/Linux Epoch Time Converter.. - Sylvain Cote - 12-31-2017 04:36 PM

(12-31-2017 04:29 PM)salvomic Wrote:  
(12-31-2017 02:30 AM)Thomas Okken Wrote:  Should work in Free42 and DM42 as well.
...

Any help to encode in raw for Free42 (or DM42)?
I'm using the SM encoder and I get this error "Invalid instruction ' -18000 STO 00' in line 01".
Thank you!

Salvo

Look at the line number on the left, he just optimized it for the post
Code:
01 LBL "EPOCH"
02  -18000
03  STO 00
04  1.01197
05  STO 01
06  86400
07  STO 02
08  SF 27
...

Thank you Bill for this nice routine and Happy New Year!

Sylvain


RE: Unix/Linux Epoch Time Converter.. - Bill Duncan - 12-31-2017 05:17 PM

Yes, sorry Salvo.. I often put several lines of code on one line if I feel that it helps the clarity of the program. Obviously in this case it didn't, or I should've warned everyone anyway..

Cheers.


RE: Unix/Linux Epoch Time Converter.. - salvomic - 12-31-2017 06:50 PM

(12-31-2017 04:36 PM)Sylvain Cote Wrote:  ...
Look at the line number on the left, he just optimized it for the post
...

Thank you Bill for this nice routine and Happy New Year!

Sylvain

(12-31-2017 05:17 PM)Bill Duncan Wrote:  Yes, sorry Salvo.. I often put several lines of code on one line if I feel that it helps the clarity of the program. Obviously in this case it didn't, or I should've warned everyone anyway..

Cheers.

Thank you Sylvain and Bill!
It worked deleting line numbers, as in this code:
Code:
LBL "EPOCH"
-18000   STO 00
1.01197  STO 01
86400    STO 02
SF 27
LBL A
DATE 
TIME
LBL B
X<>Y
RCL 01
X<>Y
DDAYS
RCL 02
*
X<>Y
HR
3600
*
+
RCL 00
-
RTN
LBL C
RCL 00
+
ENTER
ENTER
RCL 02
/
INT
RCL 01
X<>Y
DATE+
CLA
ADATE
AVIEW
PSE
X<>Y
RCL 02
MOD
3600
/
HMS
CLA
ATIME
AVIEW
END
I got the .raw for HP42/Free42 (DM42), that' this code:
Code:
00 { 94-Byte Prgm }
01▸LBL "EPOCH"
02 -18000
03 STO 00
04 1.01197
05 STO 01
06 86400
07 STO 02
08 SF 27
09▸LBL A
10 DATE
11 TIME
12▸LBL B
13 X<>Y
14 RCL 01
15 X<>Y
16 DDAYS
17 RCL 02
18 ×
19 X<>Y
20 →HR
21 3600
22 ×
23 +
24 RCL 00
25 -
26 RTN
27▸LBL C
28 RCL 00
29 +
30 ENTER
31 ENTER
32 RCL 02
33 ÷
34 IP
35 RCL 01
36 X<>Y
37 DATE+
38 CLA
39 ADATE
40 AVIEW
41 PSE
42 X<>Y
43 RCL 02
44 MOD
45 3600
46 ÷
47 →HMS
48 CLA
49 ATIME
50 AVIEW
51 END
To use the attached file, delete the .txt extension.

I wonder how to simulate in Free42 the "user mode" (HP41CX) and A, B, C.
I'm testing it only executing XEQ "EPOCH" for now...

The raw is not good in the emulator i41CX however. Is there a tool to get raw files from text for HP41CX, like the tool by Swiss Micros?

Thank you,
HNY!

Salvo


RE: Unix/Linux Epoch Time Converter.. - Bill Duncan - 12-31-2017 07:21 PM

I thought I'd try my hand in doing an HP-48GX RPL version. Haven't used RPL for awhile, so excuse me if it isn't optimal..

The nice things about the RPL version is that it can test the depth of the stack (so use the current date/time if you haven't entered anything) and, of course it can display the entire date/time string on a single line..

->EPOCH
Code:

<<
  DEPTH IF 2 < THEN
    DATE TIME
  END
  -> D T
  <<
    1.01197 D DDAYS
    86400 *
    T HMS->
    3600 * + 18000 + IP
  >>
>>

and..
EPOCH->
Code:

<<
  18000 - DUP
  86400 / IP 1.01197 SWAP DATE+
  SWAP 86400 MOD
  3600 / ->HMS
  DUP2 TSTR CLLCD
  7 DISP 7 FREEZE
>>

The last routine leaves the date and time in the stack as well as displaying the string.

As in the original version, 18000 is for EST (Eastern Standard Time) and is the number of seconds relative to GMT or UTC. I've put several instructions on a single line for clarity. You should be able to test one against the other by executing "->EPOCH" then translating back with "EPOCH->".

Hope these routines are useful to other unix/linux sysadmins.. Wink
Cheers.


RE: Unix/Linux Epoch Time Converter.. - grsbanks - 12-31-2017 07:28 PM

(12-31-2017 06:50 PM)salvomic Wrote:  I wonder how to simulate in Free42 the "user mode" (HP41CX) and A, B, C.

That was replaced by the CUSTOM menu.

You could alter "EPOCH" so that it not only sets up the constants used but also puts in place a custom menu.


RE: Unix/Linux Epoch Time Converter.. - salvomic - 12-31-2017 07:32 PM

(12-31-2017 07:28 PM)grsbanks Wrote:  That was replaced by the CUSTOM menu.

You could alter "EPOCH" so that it not only sets up the constants used but also puts in place a custom menu.

Well, thank you.
I'll try it asap.

Salvo


RE: Unix/Linux Epoch Time Converter.. - Bill Duncan - 01-01-2018 06:26 AM

2017 had a nice round number of 1,500,000,000 epoch seconds back on July 13th..

Happy 2018 everyone..


RE: Unix/Linux Epoch Time Converter.. - salvomic - 01-01-2018 08:24 AM

(01-01-2018 06:26 AM)Bill Duncan Wrote:  2017 had a nice round number of 1,500,000,000 epoch seconds back on July 13th..

Happy 2018 everyone..

Indeed...

Happy New Year, Buon anno, Bonne et heureuse année!


RE: Unix/Linux Epoch Time Converter.. - salvomic - 01-06-2018 06:10 PM

Bill,
I've put here the version for HP Prime.

Enjoy!

Salvo


RE: Unix/Linux Epoch Time Converter.. - salvomic - 02-09-2018 10:48 PM

hi,
I've modified the program for DM42 and Free42 (not HP 42s, as it miss the date), adding a custom Menu:
[Cur] → Current Epoch time [DT→E] → Date and Time to Epoch [E→DT] → Epoch to Date and Time
The program set 3600 as Time Zone variables (Europe, -1), you can set your time/zone (i.e. -18000 as in the original Bill's program).
Code:

00 { 139-Byte Prgm }
01▸LBL "EPOCH"
02 3600
03 STO 00
04 1.01197
05 STO 01
06 86400
07 STO 02
08 SF 27
09▸LBL H
10 "CUR"
11 KEY 1 XEQ A
12 "DT→E"
13 KEY 2 XEQ B
14 "E→DT"
15 KEY 3 XEQ C
16 KEY 9 GTO 99
17 MENU
18▸LBL 20
19 STOP
20 GTO 20
21▸LBL 99
22 CLMENU
23 EXITALL
24 RTN
25▸LBL A
26 DATE
27 TIME
28▸LBL B
29 X<>Y
30 RCL 01
31 X<>Y
32 DDAYS
33 RCL 02
34 ×
35 X<>Y
36 →HR
37 3600
38 ×
39 +
40 RCL 00
41 -
42 RTN
43▸LBL C
44 RCL 00
45 +
46 ENTER
47 ENTER
48 RCL 02
49 ÷
50 IP
51 RCL 01
52 X<>Y
53 DATE+
54 CLA
55 ADATE
56 AVIEW
57 PSE
58 X<>Y
59 RCL 02
60 MOD
61 3600
62 ÷
63 →HMS
64 CLA
65 ATIME
66 AVIEW
67 END

You can obtain the ".raw" version using the SwissMicros' tool.

Enjoy!

EDIT 09 Oct 2020: updated code and link to the tool for .raw conversion.