- I tested SystemTimeToTzSpecificLocalTime. The problem is that in current my time zone, it will just say 0, so I would welcome anyone testing this script and the script in my original post.
- My original script is still better though. This script gets the UTC time, and time will elapse between getting that time and converting it to local time, during which time the time zone could change due to daylight-saving time, therefore, it would be better to retrieve the time multiple times (in this script also) as a check, as I do in my original script.
- People can say that certain edge cases are unlikely, but if I'm going to write a function, I'm going to at least make it as reliable as I can (based on the information I have available). I can understand a script not working correctly around the daylight-saving time changeover period (although potentially my original script can handle this), however, the dll function used here, actually warns about the beginning/end of the year period also, which breaks the principle of least astonishment.
- I tried to make my original script as reliable as possible, if anyone can spot any problems with it, do tell me. Thanks.
Code: Select all
q:: ;test SystemTimeToTzSpecificLocalTime
VarSetCapacity(SYSTEMTIME1, 16, 0)
VarSetCapacity(SYSTEMTIME2, 16, 0)
DllCall("kernel32\GetSystemTime", Ptr,&SYSTEMTIME1)
DllCall("kernel32\SystemTimeToTzSpecificLocalTime", Ptr,0, Ptr,&SYSTEMTIME1, Ptr,&SYSTEMTIME2)
vDateUTC := "", vDate := ""
Loop, 7
if !(A_Index = 3)
vDateUTC .= Format("{:02}", NumGet(&SYSTEMTIME1, A_Index*2-2, "UShort"))
, vDate .= Format("{:02}", NumGet(&SYSTEMTIME2, A_Index*2-2, "UShort"))
vSec := DateDiff(vDate, vDateUTC, "Seconds")
vSign := (vSec >= 0) ? "+" : "-"
vTime := FormatTime(DateAdd(19990101, Abs(vSec), "Seconds"), "HH:mm:ss")
MsgBox, % vSign vTime
return
;from:
;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689
DateAdd(DateTime, Time, TimeUnits)
{
EnvAdd DateTime, %Time%, %TimeUnits%
return DateTime
}
DateDiff(DateTime1, DateTime2, TimeUnits)
{
EnvSub DateTime1, %DateTime2%, %TimeUnits%
return DateTime1
}
FormatTime(YYYYMMDDHH24MISS:="", Format:="")
{
local OutputVar
FormatTime OutputVar, %YYYYMMDDHH24MISS%, %Format%
return OutputVar
}
==================================================
- @Masonjar13: Some time zones have offsets that are multiples of 15 minutes.
UTC offset - Wikipedia
https://en.wikipedia.org/wiki/UTC_offset
Every inhabited place in the world has a UTC offset that is a multiple of 15 minutes, and the majority of offsets (as well as all nautical time zones) are measured in whole hours.
- The problem with this line:
difference:=a_now,difference+=-a_nowUTC
Code: Select all
q:: ;the problem with applying standard subtraction to datestamps
vDate1 := 19990115000000
vDate2 := 19990114230000
MsgBox, % vDate1-vDate2 ;770000 ;77 hours
MsgBox, % vDate2-vDate1 ;-770000 ;-77 hours
;MsgBox, % DateDiff(vDate1, vDate2, "Hours") ;1
;MsgBox, % DateDiff(vDate2, vDate1, "Hours") ;-1
vDate1 := 19990115000000
vDate2 := 19990115010000
MsgBox, % vDate1-vDate2 ;-10000 ;-1 hour
MsgBox, % vDate2-vDate1 ;10000 ;1 hour
;MsgBox, % DateDiff(vDate1, vDate2, "Hours") ;-1
;MsgBox, % DateDiff(vDate2, vDate1, "Hours") ;1
return