Consultor Eletrônico



Kbase P125725: 4GL/ABL: How to convert UNIX UCT time to ABL DATETIME?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   05/09/2007
Status: Unverified

GOAL:

How to convert UNIX time (the decimal number of milliseconds elapsed since 00:00:00, January 1, 1970 to ABL DATETIME?

GOAL:

How to convert a decimal field given as the number of milliseconds elapsed since since 00:00:00, January 1, 1970 to ABL DATETIME?

FIX:

The following procedure converts the values of a field stored in UCT time, that is as the number of milliseconds elapsed since Midnight January 1, 1970 into DATETIME values and output these values into a text file:
DEFINE VARIABLE dTimestamp AS DECIMAL NO-UNDO.
DEFINE VARIABLE dtDate AS DATE NO-UNDO.
DEFINE VARIABLE iTime AS DECIMAL NO-UNDO.
OUTPUT TO Conversion.txt.
PUT UNFORMATTED
"DECIMAL Format" AT 1
"DATETIME Format" AT 20 SKIP.
FOR EACH SomeTableName NO-LOCK:
RUN convertUnixTimestamp(INPUT TimeInUCTField, OUTPUT dtDate, OUTPUT iTime).
PUT UNFORMATTED
TimeInUCTField AT 1
DATETIME(dtDate, integer(iTime)) AT 20 SKIP.
END.
OUTPUT CLOSE.

PROCEDURE convertUnixTimestamp:
DEFINE INPUT PARAMETER ipdTimestamp AS DECIMAL NO-UNDO.
DEFINE OUTPUT PARAMETER opdtDate AS DATE NO-UNDO.
DEFINE OUTPUT PARAMETER opdTime AS DECIMAL NO-UNDO.

DEFINE VARIABLE iMilliSecondsPerDay AS INTEGER NO-UNDO.
DEFINE VARIABLE iMilliSecondsPerYear AS DECIMAL NO-UNDO.
DEFINE VARIABLE iYear AS INTEGER NO-UNDO.
DEFINE VARIABLE iDay AS INTEGER NO-UNDO.
DEFINE VARIABLE dSecsLeft AS DECIMAL NO-UNDO.
DEFINE VARIABLE iLeapDays AS INTEGER NO-UNDO.

ASSIGN
iMilliSecondsPerDay = 60 * 60 * 24 * 1000
iMilliSecondsPerYear = iMilliSecondsPerDay * 365
iYear = TRUNCATE(ipdTimestamp / iMilliSecondsPerYear, 0)
dSecsLeft = ipdTimestamp - (iYear * iMilliSecondsPerYear).

IF iYear > 2 THEN DO:
ASSIGN
iLeapDays = TRUNCATE((iYear - 3) / 4, 0) + 1
dSecsLeft = dSecsLeft - (iLeapDays * iMilliSecondsPerDay).
END.

ASSIGN
iDay = TRUNCATE(dSecsLeft / iMilliSecondsPerDay, 0)
opdtDate = DATE(1, 1, iYear + 1970) + iDay
opdTime = dSecsLeft - (iDay * iMilliSecondsPerDay).
END PROCEDURE.