ctime is a good way to get the date narratives day name and month name in short form eg Sun Mar 27 15:39:23 2022. This UNIX api returns the names abbreviated to the first 3 characters eg Sun, Mon Tue and Jan, Feb, Mar etc.

If the current time is required then using the RPG BIF %timestamp() will return the local system time for the timezone specified in system value QTIMZON which will then wrongly be adjusted again by the ctime API. Therefore we need to populate the seconds since epoch (z’1970-01-01-′) parameter relative to Universal Time Cooridnate not the local timezone. To do this easily in RPG we can use some SQL to retrieve UTC time.


Below is an SQL RPG program that returns the ctime current local time date narrative.

// Get date narrative for current local system time                                      
ctl-opt dftactgrp(*NO);                                                                  
dcl-s epoch timestamp inz(z'1970-01-01-00.00.00');                                       
dcl-s datetext char(24);                                                                 
dcl-s seconds int(10);                                                                   
dcl-s timeptr pointer;                                                                   
dcl-s timeptrchr char(26) based(timeptr);                                                
dcl-s timestamp_t timestamp;                                                             
dcl-pr ctime pointer extproc('ctime'); // Unix API                                       
 time int(10);                                                                           
exec sql SET :timeStamp_t=CURRENT_TIMESTAMP-CURRENT_TIMEZONE; // Get UTC time            
seconds=%diff(timestamp_t:epoch:*S); // Seconds since epoch relative to UTC              
timeptr=ctime(seconds); // Get date narrative adjusted by ctime for local system timezone
dsply datetext;