Still learning RPG/as400. I need to get the current date and time in UTC in UNIX epoch format. I havent had much luck searching on how to covert a standard dd/mm/yyyy hh:mm:ss into unix format either. Is it possible to pull UNIX time stamps from the as400?
-
Isn't Unix epoch an integer representing the number of seconds since 01 Jan 1970? – jtaylor___ Apr 14 '20 at 18:51
2 Answers
There is a always a need for things like this when you communicate with the outside world, f.e. JWT (check if token is expired).
And you can also do this without C functions:
**FREE
ctl-opt dftactgrp(*no) actgrp(*caller);
dcl-pr sys_getUtcOffset extproc('CEEUTCO');
offsetHours int(10);
offsetMinutes int(10);
offsetSeconds float(8);
feedback char(12) options(*omit);
end-pr;
dcl-c UNIX_EPOCH_START z'1970-01-01-00.00.00.000000';
main();
*inlr = *on;
dcl-proc main;
dcl-s uxts int(10);
dcl-s now timestamp;
dcl-s offsetHours int(10);
dcl-s offsetMinutes int(10);
dcl-s offsetSeconds float(8);
now = z'2020-04-15-00.00.00.000';
sys_getUtcOffset(offsetHours : offsetMinutes : offsetSeconds : *omit);
uxts = %diff(now : UNIX_EPOCH_START : *SECONDS) - offsetSeconds;
// Output : 1586908800
dsply %char(uxts);
end-proc;

- 255
- 1
- 5
I really question the need for this...
But from ILE RPG, you can call C functions...this one seems to be the one you'd want...
mktime() — Convert Local Time
If you're not familiar with calling C functions from ILE RPG, here's a good article and even covers mktime(). Supplementing RPG's Native Date/Time Support
Note that the required prototypes and data structure definitions can be found in the "System Includes" library, QSYSINC, in the ILERPE source file (QRPGLESRC) member "time"...just
/include QSYSINC/QRPGSRC,TIME
dcl-s myUnixTime like(time_t)
dcl-ds myTimeDs likeds(struct_tm)
//note: fill in myTimeDS here
myTimeDs.tm_min = 0;
// and so on...
myUnixTime = mktime(myTimeDs);
If the QSYSINC library isn't on your system, ask your admin to install option 13 - System Openness Includes
of the OS (LICPGM 5770-ss1)
Last resort, you can use the below extracts..
D time_t 10I 0 template
D struct_tm...
D DS QUALIFIED ALIGN
D TEMPLATE
// int tm_sec; /* seconds after the minute (0-61) */
// int tm_min; /* minutes after the hour (0-59) */
// int tm_hour; /* hours since midnight (0-23) */
// int tm_mday; /* day of the month (1-31) */
// int tm_mon; /* months since January (0-11) */
// int tm_year; /* years since 1900 */
// int tm_wday; /* days since Sunday (0-6) */
// int tm_yday; /* days since January 1 (0-365) */
// int tm_isdst; /* Daylight Saving Time flag */
D tm_sec 10I 0
D tm_min 10I 0
D tm_hour 10I 0
D tm_mday 10I 0
D tm_mon 10I 0
D tm_year 10I 0
D tm_wday 10I 0
D tm_yday 10I 0
D tm_isdst 10I 0
D mktime PR LIKE(time_t)
D EXTPROC('mktime')
D time LIKEDS(struct_tm)

- 21,637
- 1
- 20
- 44
-
Thank you. I need this to pull location history of our assets. The date ranges must be in unix format and coded into the request headers. – Dominick Apr 15 '20 at 15:32