Page 1 of 1

FileGetTime Results 1 Hour Off except during Daylight Savings Time

Posted: 21 Mar 2024, 06:54
by jwinfl
FileGetTime Results 1 Hour Off except during Daylight Savings Time

Code: Select all

#NoEnv  ; best ahk compatibility
SendMode Input  ; sendinput
#SingleInstance force  ; only 1 instance of script running
SetTitleMatchMode, 3 ; 1=starts with, 2=anywhere, 3=exact match
filespath1 := "D:\_ JLW\Phones\. Virtual PBX\RECORDINGS - TIME STAMPED\240310.133039 rec_921.wav"
FileGetTime Time1,%FilesPath1%,m
filespath2 := "D:\_ JLW\Phones\. Virtual PBX\RECORDINGS - TIME STAMPED\240309.094413 rec_919.wav"
FileGetTime Time2,%FilesPath2%,m
msgbox % time1 "`r" filespath2 "`r`r" time2 "`r" filespath2
exitapp
When I run the above code the date modified hour is correct during daylight savings time and plus 1 hour when it is not daylight savings time.
This is a screen shot of windows explorer and the msgbox...
image.png
image.png (415.37 KiB) Viewed 233 times
image.png
image.png (415.37 KiB) Viewed 233 times
Any help on getting the filegettime, m to match the explorer date modified time will be appreciated.

Re: FileGetTime Results 1 Hour Off except during Daylight Savings Time

Posted: 21 Mar 2024, 07:05
by mikeyww
Hello,

The easiest way might be to set your server to adjust its clock for Daylight Saving Time (DST). An alternative is using the script to adjust a time that you want to use within the script. You might then want to determine whether your own location is currently using DST. The use of DST depends on government policy. Some countries or even regions do not use DST, and some have different rules for when DST is used. The Web has APIs that can get the information along with time zones. Some other posts on the forum have examples.

Many backup and synchronization programs include options that simply allow a one-hour tolerance to accommodate this issue.

Working with a timestamp, EnvAdd can be used to add or subtract a unit of time.

Re: FileGetTime Results 1 Hour Off except during Daylight Savings Time

Posted: 21 Mar 2024, 07:39
by jwinfl
mikeyww wrote:
21 Mar 2024, 07:05
Hello,

The easiest way might be to set your server to adjust its clock for Daylight Saving Time (DST). An alternative is using the script to adjust a time that you want to use within the script. You might then want to determine whether your own location is currently using DST. The use of DST depends on government policy. Some countries or even regions do not use DST, and some have different rules for when DST is used. The Web has APIs that can get the information along with time zones. Some other posts on the forum have examples.

Many backup and synchronization programs include options that simply allow a one-hour tolerance to accommodate this issue.

Working with a timestamp, EnvAdd can be used to add or subtract a unit of time.
My problem is with AHK's filegettime's, modified date, (hour), it is not the same as what is shown in windows explorer when it is NOT daylight savings time, and it is the same when it is daylight savings time.
I know that the modified date of the recordings shown in windows explorer is the correct hour shown for both daylight and non-daylight savings times, so why is AHK showing a different time (the wrong time) than windows explorer when it is regular time (not daylight savings time)?

Re: FileGetTime Results 1 Hour Off except during Daylight Savings Time

Posted: 21 Mar 2024, 07:46
by mikeyww

Re: FileGetTime Results 1 Hour Off except during Daylight Savings Time

Posted: 21 Mar 2024, 07:51
by RussF
Found this link on Microsoft that may help. I just skimmed it, but it may shed some light on the issue. I'm guessing that AHK's GileGetTime uses the built-in GetFileTime function.

Russ

Re: FileGetTime Results 1 Hour Off except during Daylight Savings Time

Posted: 25 Mar 2024, 06:38
by jwinfl
Thank you for your answers and comments.
Unfortunately they all basically end up at the same place...
Anyone using AHK's FileGetTime reasonably assumes that it retrives the correct hour, where daylight savings time is used it does NOT always give the correct hour, it requires additional coding for the correct hour, which in my opinion, should be clearly stated in AHK's reference materials (I didn't find any) or better yet, the function should be fixed to account for daylight savings time.
Online I didn't find any simple daylight savings time functions so I wrote two, just put in the year in and it returns either the start or end of daylight savings time for that year, hopefully they may help someone else. I am sure there are numerous ways to improve the following code, but they appear to work for me and with them I have adjusted the FileGetTime so that I get the correct hour when the file was last modified.

Code: Select all

DST_Start(YYYY) { ; DST_Start = 2nd sunday in march
	mar0 := % YYYY "0300"
	loop 14
	{
	mar0++
	FormatTime, d_wk, %mar0%,dddd
	if (d_wk = "Sunday")
		sunday++
	if (d_wk = "Sunday" and Sunday = 2)
		break
	}
	return mar0
}
DST_End(YYYY) { ; DST_End = 1st sunday in nov
	mar0 := % YYYY "1100"
	loop 7
	{
	mar0++
	FormatTime, d_wk, %mar0%,dddd
	if (d_wk = "Sunday")
			break
	}
	return mar0
}
I consider this thread closed.

Re: FileGetTime Results 1 Hour Off except during Daylight Savings Time

Posted: 25 Mar 2024, 07:31
by mikeyww
A forum thread does not typically close.

The script is OK if it works. The main caveat for others is that some other regions use different rules for the timing of DST.
...should be clearly stated in AHK's reference materials
Suggestions on documentation improvements

Re: FileGetTime Results 1 Hour Off except during Daylight Savings Time

Posted: 25 Mar 2024, 07:40
by garry
;- DST = example calculate CEST Central-European- SUMMER Time
;- https://autohotkey.com/boards/viewtopic.php?f=28&t=46997
;-
;- https://www.timeanddate.com/time/dst/
;- https://www.timeanddate.com/worldclock/
;- https://www.science.co.il/language/Locale-codes.php
; ( UTC + 1 / CET - Central European Time - begin last sunday in octobre / e.g sun 2018-10-28 03:00>02:00 UTC 01:00)
; ( UTC + 2 / CEST - Central European SUMMER Time - begin last sunday in mars / e.g. sun 2018-03-25 02:00>03:00 UTC 01:00)
;- https://en.wikipedia.org/wiki/Summer_Time_in_Europe
;- On 8 February 2018, the European Parliament voted to ask the European Commission to re-evaluate DST in Europe.
;- On 31 August 2018, the European Commission announced that they, after a web query giving high support of not switching clock twice annually, intend to go ahead with a new directive.
;-
A formula which cn be used to calculate the beginning of European Summer Time is:
Sunday (31 − ((((5 × y) ÷ 4) + 4) mod 7)) March at 01:00 UTC
The corresponding formula for the end of European Summer Time is:
Sunday (31 − ((((5 × y) ÷ 4) + 1) mod 7)) October at 01:00 UTC
where y is the year, and a mod b is b times the fractional part of a/b. These formulae are valid until 2099
;-
;- Function last sunday in mars and october for DST daylight saving time ( example EUROPE )
;- https://autohotkey.com/board/topic/97664-find-specific-wday/ ---

Code: Select all

;- Function last sunday in mars and october for DST daylight saving time
;- https://autohotkey.com/board/topic/97664-find-specific-wday/ ---
LastSunday(Date)
    {
    Date += 31, D
    Date := SubStr(Date, 1, 6)
    Date += -1, Day
    FormatTime, WD , %Date%, WDay
    Date += -(WD - 1), D
    return, SubStr(Date, 1, 8)
   }
a script from @tmplinshi , get UTC and add 1 or 2 hours for CET or CEST ( EUROPE - SET TIME )
or use original script from tmplinshi to set China Time , UTC + 8 hours
;-

Code: Select all

;- https://autohotkey.com/boards/viewtopic.php?f=28&t=46997 ---
;- https://www.timeanddate.com/time/dst/
;- https://www.timeanddate.com/worldclock/
;- https://www.science.co.il/language/Locale-codes.php
;- 歐洲 CET / CEST Central European Summer-Time
;-----------------------------------------------------
#warn
setworkingdir,%a_scriptdir%
; 以管理员身份运行
if !A_IsAdmin {
Try
Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
ExitApp
}
global dst
if SynTime.DoIt()
msgbox, 262208,SUCCESS ,Succes synchronized %dst%,
else
msgbox, 262208,NO SUCCESS ,	NO Succes synchronized CET,
return
/*
if SynTime.DoIt()
MsgBox, 64, 成功 , 同步北京时间成功!
else
MsgBox, 48, 失败 , 同步失败!
; 同步北京时间。用法: SynTime.DoIt()
return
*/
Class SynTime
{
DoIt() { ; 成功返回 1
return this.SetLocalTime( this.BeijingTime() )
}
SetLocalTime(YYYYMMDDHHMISS) {
FormatTime, t, %YYYYMMDDHHMISS%, yyyy/M/1/d/H/m/s/0
VarSetCapacity(SystemTime, 16, 0)
Loop, Parse, t, /
NumPut(A_LoopField, SystemTime, (A_Index-1)*2, "UShort")
return DllCall("SetLocalTime", "Ptr", &SystemTime)
}
BeijingTime() { ; 返回 YYYYMMDDHHMISS
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("HEAD", "http://baidu.com", true)
whr.Send()
whr.WaitForResponse()
_date := whr.GetResponseHeader("Date") ; 示例数据: Mon, 21 Apr 2014 14:58:23 GMT
arr := StrSplit( _date, [A_Space, ":"] )
oMonth := {Jan:"01",Feb:"02",Mar:"03",Apr:"04",May:"05",Jun:"06",Jul:"07",Aug:"08",Sep:"09",Oct:"10",Nov:"11",Dec:"12"}
Mon    := oMonth[ arr.3 ] ; 月份英文缩写转换成数字
timestamp := arr.4 Mon arr.2 arr.5 arr.6 arr.7 ; YYYYMMDDHHMISS 格式的日期
p2:=1                             ;- ( UTC + 1 / CET - Central European Time - begin last sunday in octobre / e.g  sun 2018-10-28 03:00>02:00 )
stringmid,q,timestamp,1,8
stringmid,Y,timestamp,1,4
q:=(q)
lsm := LastSunday(Y . "03")
lso := LastSunday(Y . "10")
if (q >= lsm) and (q < lso)
{
DST:="CEST summertime UTC+2"
p2:=(P2+1)                     ;- ( UTC + 2 / CEST - Central European SUMMER Time - DST begin last sunday in mars / e.g. sun 2018-03-25 02:00>03:00 )
timestamp += +P2,hours
}
else
{
DST:="CET normaltime UTC+1"
timestamp += +P2,hours
}
;timestamp += 8, Hours ; 加 8 小时就是北京时间
return timestamp
}
}
;-----------------------------------------------------------------------
;- Function last sunday in mars and october for DST daylight saving time in Europe
;- https://autohotkey.com/board/topic/97664-find-specific-wday/ ---
LastSunday(Date)
{
Date += 31, D
Date := SubStr(Date, 1, 6)
Date += -1, Day
FormatTime, WD , %Date%, WDay
Date += -(WD - 1), D
return, SubStr(Date, 1, 8)
}
;============================================================================