KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to produce a UTC Date-Time Stamp
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: October 15, 2009

There are numerous organizations that want records, communications, appointments and more given a date-time stamp expressed in a UTC string. A UTC string is the time at UTC (or GMT or ZULU) expressed as "yyyymmddThhmmssZ".

Using AP Pack plugin call AP Timestamp to GMT we can easily produce this time stamp regardless of where you are in the world. The method below, UTIL_GetDTSTAMP_ZULU, can convert any time into its equivalent UTC/GMT/ZULU time and produce the appropriate UTC Date-Time stamp string.

NOTE: AP Timestamp to GMT does not account for daylight savings time. Since in those countries that observe "Daylight Savings" or "Summer" time, most of the year is spent in daylight time instead of standard time. Because of this the code defaults to daylight time.

  `// UTIL_GetDTSTAMP_ZULU
C_TEXT($0;$DTS_T)
C_BOOLEAN($IsDaylightTime;$1)
C_DATE($LocalDate_D;$2)
C_TIME($LocalTime_H;$3)
C_LONGINT($Ndx)
C_TEXT($GMT_T)
C_DATE($gmtDate_D)
C_TIME($gmtTime_H)

  `====================== Initialize and Setup ================================

$Ndx:=Count parameters
If ($Ndx>0)
    $IsDaylightTime:=$1
    If ($Ndx>1)
        $LocalDate_D:=$2
        If ($Ndx>2)
            $LocalTime_H:=$3
        Else
            $LocalTime_H:=Current time(*)
        End if
    Else
        $LocalDate_D:=Current date(*)
        $LocalTime_H:=Current time(*)
    End if
Else
    $IsDaylightTime:=True
    $LocalDate_D:=Current date(*)
    $LocalTime_H:=Current time(*)
End if

If($IsDaylightTime)
    $LocalTime_H:=$LocalTime_H+3600
    If($LocalTime_H>=†24:00:00†)
        $LocalDate_D:=Add to date($LocalDate_D;0;0;1)
    End if
End if

  `======================== Method Actions ==================================

$GMT_T:=AP Timestamp to GMT ($LocalDate_D;$LocalTime_H;$gmtDate_D;$gmtTime_H)

$DTS_T:=Replace string(Substring(String($gmtDate_D;ISO Date );1;11);"-";"")
$DTS_T:=$DTS_T+Replace string(Substring(String($gmtTime_H;ISO Time );12);":";"")+"Z"

  `======================== Clean up and Exit =================================

$0:=$DTS_T


Example #1 will return the UTC string for the current date and time:
$Buf_T:=UTIL_GetDTSTAMP_ZULU  `// 20090916T194108Z


Example #2 will return the UTC string for the current date and time:
$Buf_T:=UTIL_GetDTSTAMP_ZULU (False) `// 20090916T184708Z


Example #3 will return the UTC string for the current local time on 02/18/2009:
$Buf_T:=UTIL_GetDTSTAMP_ZULU (False;!02/18/2009!) `// 20090218T194914Z


Example #4 will return the UTC string for midnight local time on 02/18/2009:
$Buf_T:=UTIL_GetDTSTAMP_ZULU (False;!02/18/2009!;†00:00:00†) `// 20090218T050000Z