KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to get the timezone offset from GMT
PRODUCT: 4D | VERSION: 12.1 | PLATFORM: Mac & Win
Published On: December 21, 2010

4D v12.1 introduces new functionality to the String command; one of these new features is the ability to format time as ISO DATE GMT (that is in ISO DATE format with GMT offset applied).

Using this new functionality, you can easily calculate the offset using the following code:

// Description:
// Returns the current timezone offset as text like:
// "-08:00:00" if in Pacific Standard Time
// "-06:00:00" if in Central America
// "+04:30:00" if in Kabul
// "+12:00:00" if in Fiji


C_TEXT($0;$local_DATE;$gmt_DATE;$gmt_TIME)
$local_DATE:=Substring(String(Current date;ISO Date);1;10)
$gmt_DATE:=Substring(String(Current date;ISO Date GMT);1;10)
$gmt_TIME:=Substring(String(Current date;ISO Date GMT);12;8)
If ($gmt_DATE=$local_DATE)
// - time
$0:="-"+$gmt_TIME
Else
// + time
$0:="+"+String(?24:00:00?-Time($gmt_TIME))
End if


If the above code is saved as a method named UTIL_GetTimezoneOffset it can be used with the following syntax:


C_TEXT($timeOffset)
$timeOffset:=UTIL_GetTimezoneOffset
// $timeOffset = "-08:00:00" when the computer is using California's timezone