PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Converting UTC to local time  (Read 1328 times)

Jeff Wilder

  • Silver Member
  • ***
  • Posts: 42
    • View Profile
Converting UTC to local time
« on: December 22, 2020, 11:54:22 AM »
Hello,
I have a situation where I need to convert a supplied UTC date and time to the user's local date and time for display.  These may be dates in the past or future.  I am unsure of the best method to do this because of the potential for the user's local time zone to use daylight saving time. I am aware of TCB(44) and TCB(45), but those only apply to the current date. To properly convert the time, I need to know if the user's local time zone will observe (or had observed) DST on a specific date (or if their time zone even observes DST).

Does anyone have any tips, tricks, or routines that can properly handle this?

Thanks,
Jeff

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Converting UTC to local time
« Reply #1 on: December 22, 2020, 12:21:58 PM »
Running on Linux or Unix you can use the system 'date' routine to perform all the time zone and daylight savings adjustments for you.  First off convert your date/time into the following format: 

YYYYMMDD hh:mm:ss UTC

Then pass this to the 'date' system command as follows:

sue:~ # date --date "20201222 12:00:00 UTC"
Tue Dec 22 07:00:00 EST 2020


The system will provide you the local date/time adjusting for Daylight savings,

Now if you want to find the date/time for other time zones you can simply set the environment variable TZ to the target time zone before running the date command.  The easiest way to accomplish this would be a shell script:

sue:~ # cat date.sh
#!/bin/bash
export TZ=$1
date --date="$2"


This would allow you to do the following:

sue:~ # ./date.sh America/Toronto "20201222 12:00:00 UTC"
Tue Dec 22 07:00:00 EST 2020

sue:~ # ./date.sh America/Chicago "20201222 12:00:00 UTC"
Tue Dec 22 06:00:00 CST 2020

sue:~ # ./date.sh America/Vancouver "20201222 12:00:00 UTC"
Tue Dec 22 04:00:00 PST 2020

sue:~ # ./date.sh Europe/Paris "20201222 12:00:00 UTC"
Tue Dec 22 12:00:00 Asis 2020

sue:~ # ./date.sh Asia/Tokyo "20201222 12:00:00 UTC"
Tue Dec 22 21:00:00 JST 2020

sue:~ # ./date.sh Europe/Moscow "20201222 12:00:00 UTC"
Tue Dec 22 16:00:00 MSK 2020


As for Daylight savings -- If I provide a date that occurs during daylight savings the routine automatically determine the proper offset and time zone.  Take 2 months ago in Toronto and we get:

sue:~ # ./date.sh America/Toronto "20201022 12:00:00 UTC"
Thu Oct 22 08:00:00
EDT 2020





Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Allen Miglore

  • Silver Member
  • ***
  • Posts: 38
    • View Profile
    • UnForm
Re: Converting UTC to local time
« Reply #2 on: December 22, 2020, 12:37:52 PM »
I've written a few functions over the years.  Maybe they'll help.

These two convert between utc seconds and julian (where julian includes a fractional part).

def fn%utcsectojul(local secs)=jul(1970,1,1)+(secs-tcb(44))/86400
def fn%jultoutcsec(local dtm)=(dtm-jul(1970,1,1))*86400+tcb(44)

These two produce text versions of a given date/time julian in universal or local time.

def fn%utctime$(local dtm)
 let dtm+=tcb(44)/86400
 return dte(int(dtm),24*fpt(dtm):"%Ds, %D %Ms %Yl %Hz:%mz:%sz +0000")
end def
def fn%localtime$(local dtm)
 local time
 return dte(int(dtm),24*fpt(dtm):"%Ds, %D %Ms %Yl %Hz:%mz:%sz")+" "+str(-tcb(44)/3600:"+00")+str(60*fpt(-tcb(44)/3600):"00")
end def

Jeff Wilder

  • Silver Member
  • ***
  • Posts: 42
    • View Profile
Re: Converting UTC to local time
« Reply #3 on: December 22, 2020, 01:32:50 PM »
Thank you again Mike. That will work for my needs.

I will also try something similar in Powershell for Windows installations.

Code: [Select]
0010 BEGIN
0020 LET D$="2020-06-15T15:00:00"
0030 LET C$="$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName;$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone);$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc('"+D$+"', $TZ);$LocalTime.GetDateTimeFormats('s')"
0040 OPEN (1)"|powershell.exe -Command ""&{"+C$+"}"""
0050 READ (1,ERR=*NEXT)A$
0060 PRINT A$
0070 END

Thank you Allen, but the TCB(44) function returns the current offset. I need to know what the offset will be on any given date.

Regards,
Jeff