Open Source on IBM i

Creating a Unix Timestamp

As most people know the Unix timestamp is an integer value which represents the seconds from 1970-01-01.

There is currently no built-in function for it but you can easily calculate it by yourself.

**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;
Tags : RPG