Return to the RPG Tips
Obtaining the System Time in Milliseconds
There are times when it can be handy to obtain the system time with milliseconds - for example, when you want to create a unique identifier for data transfers. Although you can retrieve this data using the QWCRSVAL (Retrieve System Value) API, the QWCCVTDT (Convert Date and Time Format) API is easier to use and understand. QWCCVTDT is documented in OS/400 Miscellaneous APIs (SC41-4880) at http://publib.boulder.ibm.com/pubs/html/as400/v4r5/ic2924/info/apis/misc1.htm.
Passing *CURRENT to QWCCVTDT as the input format value returns the current system date and time. As shown in the sample program below, you can control the returned date format by changing the OutFmt (output format) value. By default, the system returns dates in a year-month-day (*YYMD) format; OutFmt lets you select another format from the additional choices of day-month-year (*DMYY), month-day-year (*MDYY), or long Julian (*LONGJUL). Note that only three significant digits are returned for the milliseconds rather than the full six digits that make up the milliseconds.
*======================================================== * To compile: * * CRTBNDRPG PGM(XXX/TIMER) SRCFILE(XXX/QRPGLESRC) *======================================================== * Convert date / time (QWCCVTDT) API D InpFmt S 10 Inz( '*CURRENT' ) D InpTim S 16 Inz( *ZEROS ) D OutFmt S 10 Inz( '*YYMD' ) * Current date and time D CurDatTim DS D CurDat 8S 0 D CurTim 9S 0 * Error code data D ErrData DS D BytesProv 1 4B 0 Inz( 272 ) D BytesAval 5 8B 0 D ExcpId 9 15A D Reserved 16 16A D ExcpData 17 272A C *Entry Plist C Parm CurDatTim C Call 'QWCCVTDT' C Parm InpFmt C Parm InpTim C Parm OutFmt C Parm CurDatTim C Parm ErrData C ReturnThe following CL program returns the 16-byte date and time value, including milliseconds:
PGM DCL &TIME *CHAR 16 DCL &ERR *CHAR 4 VALUE(X'00000000') CALL QWCCVTDT PARM('*CURRENT ' XX '*YMD ' &TIME &ERR) SNDPGMMSG &TIME ENDPGM The date and time are returned in the following format: 1 : Century, where 0=years 19xx and 1=years 20xx 2-7 : Date 8-13 : Time, in HHMMSS (hours, minutes, seconds) format 14-16 : MillisecondsIBM Knowledgebase item 8134953
[report a broken link by clicking here]