KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility to return the time difference between two ISO DateTimeStamps
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: January 21, 2015

The GEN_TimestampDifference will return the time difference in hours minutes an seconds between to dates time stamp in ISO date format.

The code snippet below provides an example of how to use the utility. Since 4D v12 creating an ISO date time string "0000-00-00T00:00:00" is very simple using the String command.

$ISO_TimeNow_T:=String(Current date;ISO date;Current time)
$ISO_TimeThen_T:=String($EvtDate_D;ISO date;$EvtDate_D)

$Diff_H:=GEN_TimestampDifference ($ISO_TimeThen_T;$ISO_TimeNow_T)
If ($Diff_H >= ?00:30:00?) // 30 minutes must have expired
    // Do something
End if


If (True)
    If (False)
       Begin SQL
       /*
       Name: GEN_TimestampDifference ( $timestamp1;$timestamp2 )
       Path: GEN_TimestampDifference

       Purpose: Compare two ISO dateTimeStamps and return
       the time differebce between them

       $0 - C_TIME - Difference in time
       $1 - C_STRING - ISO dateTimeStamp #1
       $2 - C_STRING - ISO dateTimeStamp #2
       */
       End SQL
    End if
    C_TEXT($MethodName_T)
    $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
    C_TIME($0)
    C_STRING(19;$Timestamp1_A19;$1)
    C_STRING(19;$Timestamp2_A19;$2)

    //--------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------
    //local_variable_declarations
    C_LONGINT($Ndx;$SOA;$RIS;$Params_L;$Year_L;$Month_L;$Day_L)
    C_DATE($Date1_D;$Date2_D)
    C_TIME($Hours_H;$Time1_H;$Time2_H)

End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters
$0:=?00:00:00?

//======================== Method Actions ==================================

Case of
    : ($Params_L<2)

    : (Length($1)#19)

    : (Length($2)#19)

    : ($1=$2)

    Else
       If ($1>$2) //if 1st timestamp is the most recent
          $Timestamp1_A19:=$1
          $Timestamp2_A19:=$2

       Else
          $Timestamp1_A19:=$2
          $Timestamp2_A19:=$1

       End if

       $Year_L:=Num(Substring($Timestamp1_A19;1;4))
       $Month_L:=Num(Substring($Timestamp1_A19;6;2))
       $Day_L:=Num(Substring($Timestamp1_A19;9;2))
       $Date1_D:=Add to date(!00/00/0000!;$Year_L;$Month_L;$Day_L)

       $Year_L:=Num(Substring($Timestamp2_A19;1;4))
       $Month_L:=Num(Substring($Timestamp2_A19;6;2))
       $Day_L:=Num(Substring($Time>stamp2_A19;9;2))
       $Date2_D:=Add to date(!00/00/0000!;$Year_L;$Month_L;$Day_L)

       $Hours_H:=(24*?01:00:00?)*($Date1_D-$Date2_D)
       $Time1_H:=Time(Substring($Timestamp1_A19;12))
       $Time2_H:=Time(Substring($Timestamp2_A19;12))

       $0:=$Hours_H+($Time1_H-$Time2_H)

End case

See Also: