Author Topic: Function to convert UTC to local time?  (Read 4759 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Function to convert UTC to local time?
« on: July 19, 2020, 12:53:48 pm »
Does anyone happen to have a function handy for converting UTC to local time? I've found several things online for C/C++ but they always fail to compile. I'd like to be able to take a UTC timestamp and just put it into whatever time zone is passed.
Shuwatch!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Function to convert UTC to local time?
« Reply #1 on: July 19, 2020, 01:18:47 pm »
Is this something like you're looking for?
https://www.qb64.org/forum/index.php?topic=1638.30

- Dav


Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Function to convert UTC to local time?
« Reply #3 on: July 19, 2020, 01:23:27 pm »
Have you tested this?
https://docs.microsoft.com/es-es/windows/win32/api/timezoneapi/nf-timezoneapi-systemtimetotzspecificlocaltimeex
@moises1953
I've looked at it but I can't find good information on how to pass the timestamp I have to it.
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Function to convert UTC to local time?
« Reply #4 on: July 19, 2020, 01:25:12 pm »
Is this something like you're looking for?
https://www.qb64.org/forum/index.php?topic=1638.30
@Dav I saw that but it look like it just converts a timestamp to Epoch/UNIX time which I already have. I'm just wanting to take a string of a UTC date and time and make it local time. Using SystemTimeToTzSpecificLocalTime as moises suggested would probably be it but I can't figure out how to go about using it.
Shuwatch!

Marked as best answer by SpriggsySpriggs on July 19, 2020, 01:02:02 pm

Offline moises1953

  • Newbie
  • Posts: 55
    • View Profile
Re: Function to convert UTC to local time?
« Reply #5 on: July 19, 2020, 03:12:57 pm »
May be this code helps you

Code: QB64: [Select]
  1. TYPE SystemTime
  2.   wYear AS INTEGER
  3.   wMonth AS INTEGER
  4.   wDayOfWeek AS INTEGER
  5.   wDay AS INTEGER
  6.   wHour AS INTEGER
  7.   wMinute AS INTEGER
  8.   wSecond AS INTEGER
  9.   wMilliseconds AS INTEGER
  10.  
  11. TYPE TDZoneInfo 'TIME_DYNAMIC_ZONE_INFORMATION
  12.   Bias AS LONG
  13.   StandardName AS STRING * 64 'wchar32
  14.   StandardDate AS SystemTime
  15.   StandardBias AS LONG
  16.   DaylightName AS STRING * 64 'wchar32
  17.   DaylightDate AS SystemTime
  18.   DaylightBias AS LONG
  19.   TimeZoneKeyName AS STRING * 256 ' WCHAR128
  20.   DynamicDaylightTimeDisabled AS _BYTE 'BOOLEAN->BYTE
  21.  
  22.   FUNCTION SystemTimeToTzSpecificLocalTimeEx%% (BYVAL lpTimeZoneInformation AS _UNSIGNED _OFFSET,_
  23.     BYVAL lpUniversalTime AS _UNSIGNED _OFFSET, byval lpLocalTime AS _UNSIGNED _OFFSET)
  24.   FUNCTION GetDynamicTimeZoneInformation~& (BYVAL lpTimeZoneInformation AS _UNSIGNED _OFFSET)
  25.   SUB GetSystemTime (BYVAL lpSystemTime AS _UNSIGNED _OFFSET)
  26.  
  27. DIM UTCTime AS SystemTime, loctime AS SystemTime, ZoneInfo AS TDZoneInfo
  28.  
  29. CALL GetSystemTime(_OFFSET(UTCTime))
  30.  
  31. PRINT UTCTime.wYear; "/"; UTCTime.wMonth; "/"; UTCTime.wDay
  32. PRINT UTCTime.wHour; ":"; UTCTime.wMinute; ":"; UTCTime.wSecond
  33.  
  34. e~& = GetDynamicTimeZoneInformation(_OFFSET(ZoneInfo))
  35. PRINT ZoneInfo.StandardName
  36.  
  37. f%% = SystemTimeToTzSpecificLocalTimeEx(_OFFSET(ZoneInfo), _OFFSET(UTCTime), _OFFSET(loctime))
  38.  
  39. PRINT loctime.wYear; "/"; loctime.wMonth; "/"; loctime.wDay
  40. PRINT loctime.wHour; ":"; loctime.wMinute; ":"; loctime.wSecond
  41.  
  42.  

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Function to convert UTC to local time?
« Reply #6 on: July 19, 2020, 05:08:13 pm »
@moises1953 That is exactly what I need! Thank you so much!
In the meantime, I discovered how to implement Javascript in QB64 using NodeJS! I'll make a full post on how to do that soon. Here is what my solution was for getting the correct date:
Code: QB64: [Select]
  1.     DIM JS AS INTEGER
  2.     JS = FREEFILE
  3.     OPEN "uuiddate.js" FOR OUTPUT AS #JS
  4.     PRINT #JS, "const fs = require('fs');"
  5.     PRINT #JS, "let writeStream = fs.createWriteStream('uuiddate.d');"
  6.     PRINT #JS, "var d = new Date('" + uuiddate(timestamp) + " " + hour + ":" + minute + ":" + second + " UTC');"
  7.     PRINT #JS, "writeStream.write(d.toLocaleString().replace(',',''),'ascii');"
  8.     PRINT #JS, "writeStream.end();"
  9.     CLOSE #JS
  10.     SHELL _HIDE "node " + CHR$(34) + _STARTDIR$ + "\uuiddate.js" + CHR$(34)
  11.     KILL "uuiddate.js"
  12.     DIM d AS INTEGER
  13.     d = FREEFILE
  14.     DIM utctolocale AS STRING
  15.     OPEN "uuiddate.d" FOR BINARY AS #d
  16.     IF LOF(d) <> 0 THEN
  17.         LINE INPUT #d, utctolocale
  18.     END IF
  19.     CLOSE #d
  20.     KILL "uuiddate.d"
  21.     ExtractTimestamp = utctolocale
« Last Edit: July 19, 2020, 06:57:25 pm by SpriggsySpriggs »
Shuwatch!

Offline moises1953

  • Newbie
  • Posts: 55
    • View Profile
Re: Function to convert UTC to local time?
« Reply #7 on: July 23, 2020, 01:01:40 pm »
This is a more compact form

Code: QB64: [Select]
  1. TYPE SystemTime
  2.   wYear AS INTEGER
  3.   wMonth AS INTEGER
  4.   wDayOfWeek AS INTEGER
  5.   wDay AS INTEGER
  6.   wHour AS INTEGER
  7.   wMinute AS INTEGER
  8.   wSecond AS INTEGER
  9.   wMilliseconds AS INTEGER
  10.  
  11. TYPE TDZoneInfo 'TIME_DYNAMIC_ZONE_INFORMATION
  12.   Bias AS LONG
  13.   StandardName AS STRING * 64 'wchar32
  14.   StandardDate AS SystemTime
  15.   StandardBias AS LONG
  16.   DaylightName AS STRING * 64 'wchar32
  17.   DaylightDate AS SystemTime
  18.   DaylightBias AS LONG
  19.   TimeZoneKeyName AS STRING * 256 ' WCHAR128
  20.   DynamicDaylightTimeDisabled AS _BYTE 'BOOLEAN->BYTE
  21.  
  22.   FUNCTION SystemTimeToTzSpecificLocalTimeEx%% (lpTimeZoneInformation AS tdzoneinfo,_
  23.     lpUniversalTime AS systemTime, lpLocalTime AS systemTime)
  24.   FUNCTION GetDynamicTimeZoneInformation~& (lpTimeZoneInformation AS TDZoneInfo)
  25.   SUB GetSystemTime (lpSystemTime AS SystemTime)
  26.  
  27. DIM UTCTime AS SystemTime, loctime AS SystemTime, ZoneInfo AS TDZoneInfo
  28.  
  29. CALL GetSystemTime(UTCTime)
  30.  
  31. PRINT UTCTime.wYear; "/"; UTCTime.wMonth; "/"; UTCTime.wDay
  32. PRINT UTCTime.wHour; ":"; UTCTime.wMinute; ":"; UTCTime.wSecond
  33.  
  34. e~& = GetDynamicTimeZoneInformation(ZoneInfo)
  35. PRINT ZoneInfo.StandardName
  36.  
  37. f%% = SystemTimeToTzSpecificLocalTimeEx(ZoneInfo, UTCTime, loctime)
  38.  
  39. PRINT loctime.wYear; "/"; loctime.wMonth; "/"; loctime.wDay
  40. PRINT loctime.wHour; ":"; loctime.wMinute; ":"; loctime.wSecond
  41.  
  42.