4D method to handle unix timestamps
Method 'unixTimeStamp' starts here
`({theDate {;theTime}}) -> unixTimeStampLong
`-> unixTimestamp returns theunix timestamp based on date & time passed, or based on current data and time if nothing is passed
`Unix timestamp is number seconds since 1 January 1970, 00:00:00
`©developer-source.com Sun, Jan 14, 2007 balinderwalia
`<chain>`</chain>
`<param>
C_DATE($1;$pTheDate)
C_TIME($2;$pTheTime)
C_LONGINT($0;$pTimeStampLong)
`</param>
`<locals>
C_STRING(80;$gmtDateTimeStr)
C_DATE($gmtDate)
C_TIME($gmtTime)
C_DATE($unixTimeStampCutDate)
C_LONGINT($nDaysSinceCutDateLong)
C_LONGINT($localToGMTDifference)
C_LONGINT($nSecondsInDay)
`</locals>
`<process> `</process> `<interproc>
C_LONGINT(◊unixTsGmtLocalTimeDiff)
`</interproc>
`<code>
`Initialise the variables & parameters to the default values
`
If (Count parameters>0)
$pTheDate:=$1
Else
$pTheDate:=Current date(*)
End if
`
If (Count parameters>1)
$pTheTime:=$2
Else
$pTheTime:=Current time(*)
End if
$nSecondsInDay:=86400 `86400 seconds = 24*60*60 = 24 hrs = 1 day
$gmtDateTimeStr:=AP Timestamp to GMT ($pTheDate;$pTheTime;$gmtDate;$gmtTime)
If (($gmtDate>=!01/01/70!) & ($gmtDate<!01/01/2030!))
$unixTimeStampCutDate:=!01/01/70! `BSW15/11/02 Please don't change this cut date without asking the Balinder - Thankx
$nDaysSinceCutDateLong:=($gmtDate-$unixTimeStampCutDate)*$nSecondsInDay
If ($gmtDate>=$unixTimeStampCutDate)
$pTimeStampLong:=$nDaysSinceCutDateLong+$gmtTime
Else
$pTimeStampLong:=$nDaysSinceCutDateLong-$gmtTime
End if
$nDaysSinceCutDateLong:=($pTheDate-$unixTimeStampCutDate)*$nSecondsInDay
If ($pTheDate>=$unixTimeStampCutDate)
$localToGMTDifference:=$nDaysSinceCutDateLong+$pTheTime
Else
$localToGMTDifference:=$nDaysSinceCutDateLong-$pTheTime
End if
`update local and gmt time difference (seconds)
◊unixTsGmtLocalTimeDiff:=$localToGMTDifference-$pTimeStampLong
Else
`Error occurred so alert admin via email (This piece of code should be abstracted to a single method which can be called from anywhere, like BugReport(emailStr; reportStr;descriptionTxt....)
TRACE
C_STRING(80;$webSiteMailServerStr)
C_STRING(80;$senderStr)
C_STRING(80;$receiverStr)
C_TEXT($subjectTxt;$bodyTxt)
C_LONGINT($iError)
`
$webSiteMailServerStr:="mail.tenthmatrix.net"
$senderStr:="development@tenthmatrix.net"
$receiverStr:="developers@tenthmatrix.net"
$subjectTxt:="Timestamps might be broken urgent message for developers"
$bodyTxt:="In "+Structure file+" See method ("+Current method name+")"+Char(13)+Char(10)+" Rewrite Timestamp related functions and methods because it will break the"+" timestamps generated before 01/01/1932 and after 01/01/2068!"
$iError:=SMTP_QuickSend ($webSiteMailServerStr;$senderStr;$receiverStr;$subjectTxt;$bodyTxt)
End if
`</code>
`<return>
$0:=$pTimeStampLong
`</return>
Method 'unixTimeStamp' ends here
Method 'unixTimestampToDate' starts here
`(unixTimeStampLong) -> theDate
`-> UnixTimestampToDate returns date for the timestamp passed, if no timestamp passed it returns current date
`©developer-source.com Sun, Jan 14, 2007 balinderwalia
`<chain>`</chain>
`<param>
C_LONGINT($1;$pTimeStampLong)
C_DATE($0;$pTheDate)
`</param>
`<locals>
C_DATE($unixTimeStampCutDate)
C_LONGINT($nSecondsInDay)
C_LONGINT($currTimeStampLong)
`</locals>
`<process> `</process> `<interproc>
C_LONGINT(◊unixTsGmtLocalTimeDiff)
`</interproc>
`<code>
`Initialise the variables & parameters to the default values
$nSecondsInDay:=86400 `86400 seconds = 24*60*60 = 24 hrs = 1 day
$unixTimeStampCutDate:=!01/01/70!
`
If (Count parameters>0)
$pTimeStampLong:=$1
Else
$pTimeStampLong:=unixTimestamp (Current date(*);Current time(*))
End if
If (◊unixTsGmtLocalTimeDiff=0)
$currTimeStampLong:=unixTimestamp (Current date(*);Current time(*))
End if
$pTimeStampLong:=$pTimeStampLong+◊unixTsGmtLocalTimeDiff
If ($pTimeStampLong>0)
$pTheDate:=$unixTimeStampCutDate+($pTimeStampLong\86400)
Else
$pTheDate:=$unixTimeStampCutDate+($pTimeStampLong\86400)+1
End if
`</code>
`<return>
$0:=$pTheDate
`</return>
Method 'unixTimestampToDate' ends here
Method 'unixTimestampToTime' starts here
`(unixTimeStampLong) -> theTime
`-> UnixTimestampToTime returns time for the timestamp passed, if no timestamp passed it returns current time
`©developer-source.com Sun, Jan 14, 2007 balinderwalia
`<chain>`</chain>
`<param>
C_LONGINT($1;$pTimeStampLong)
C_TIME($0;$pTheTime)
`</param>
`<locals>
C_LONGINT($nSecondsInDay)
C_LONGINT($currTimeStampLong)
`</locals>
`<process> `</process> `<interproc>
C_LONGINT(◊unixTsGmtLocalTimeDiff)
`</interproc>
`<code>
`Initialise the variables & parameters to the default values
$nSecondsInDay:=86400 `86400 seconds = 24*60*60 = 24 hrs = 1 day
If (Count parameters>0)
$pTimeStampLong:=$1
Else
$pTimeStampLong:=unixTimestamp (Current date(*);Current time(*))
End if
If (◊unixTsGmtLocalTimeDiff=0)
$currTimeStampLong:=unixTimestamp (Current date(*);Current time(*))
End if
$pTimeStampLong:=$pTimeStampLong+◊unixTsGmtLocalTimeDiff
$pTheTime:=Time(Time string(($pTimeStampLong%$nSecondsInDay)))
`</code>
`<return>
$0:=$pTheTime
`</return>
Method 'unixTimestampToTime' ends here
Now in order to test above three methods run the code in a test method like this:
$timestamp:=unixTimestamp
If (True)
$timestamp:=$timestamp+3600
End if
$timeDate:=unixTimestampToDate ($timestamp)
$timeTime:=unixTimestampToTime ($timestamp)
