Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

How do I get a file's modified time in UTC

  • Please log in to reply
7 replies to this topic
  • Members
  • 9 posts
  • Last active: Jan 06 2017 02:00 PM
  • Joined: 29 Oct 2012

Our company has offices in a few different time zones that regularly share and update files across our servers.  I am trying to compare the modified time of some text documents to the modified time of some pdf documents to ensure that the pdf is newer than the text document.  I know how to create the script to do all of the comparisons, but "FileGetTime" pulls the local time and this isn't accurate enough.  I was wondering if there is a fairly easy way to call the UTC modified time of the files for the comparison.  -Thanks.

  • Members
  • 383 posts
  • Last active: Apr 20 2018 01:45 PM
  • Joined: 21 Oct 2011

http://www.autohotke...s/Variables.htm, you'll like what you'll find from there.

  • Members
  • 9 posts
  • Last active: Jan 06 2017 02:00 PM
  • Joined: 29 Oct 2012

Thank you for the quick response.  I may be missing something, but I didn't see anything in the variables section that really benefits me.  The time variables in this help document are for the current time, system time, idle time, script times, etc...  What I am looking for is the time that is stored for a file when the file was modified.  The files I would be comparing are files that are not created by ahk or even on computers that have ahk on them. 


In a liitle bit of research I found a win32 function "GetFileTime" at http://msdn.microsof...0(v=vs.85).aspx, but I don't know how to use this function in ahk, if this is the function I need or even if this is possible in ahk.  Any more help would be greatly appreciated.  -Thanks

  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005

not sure , this used local time

here a part from a script with listview / loop


a script to get UTC time from your computer

;- http://www.autohotkey.com/board/topic/89516-iso-8601formatting-datetime/
;- from user just me
SetBatchLines, -1

UTCTimestamp := GetTimestampUTC()
UTCFormatStr := "yyyy-MM-dd'T'HH:mm'Z'"
FormatTime, TimeStr, %UTCTimestamp%, %UTCFormatStr%
MsgBox, 0, UTC,%UTCTimestamp%`n%TimeStr%

GetTimestampUTC() { ; http://msdn.microsoft.com/en-us/library/ms724390
   VarSetCapacity(ST, 16, 0) ; SYSTEMTIME structure
   DllCall("Kernel32.dll\GetSystemTime", "Ptr", &ST)
   Return NumGet(ST, 0, "UShort")                        ; year   : 4 digits until 10000
        . SubStr("0" . NumGet(ST,  2, "UShort"), -1)     ; month  : 2 digits forced
        . SubStr("0" . NumGet(ST,  6, "UShort"), -1)     ; day    : 2 digits forced
        . SubStr("0" . NumGet(ST,  8, "UShort"), -1)     ; hour   : 2 digits forced
        . SubStr("0" . NumGet(ST, 10, "UShort"), -1)     ; minute : 2 digits forced
        . SubStr("0" . NumGet(ST, 12, "UShort"), -1)     ; second : 2 digits forced


Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011

Try this:

FileGetTime, localTime, file.txt
UtcTime := localTime + A_NowUTC - A_Now
FormatTime, timestr, %UtcTime%
Msgbox % timestr

Join us at the new forum - http://www.ahkscript.org/


  • Members
  • 9 posts
  • Last active: Jan 06 2017 02:00 PM
  • Joined: 29 Oct 2012


I think what I am looking for is going to be similar to the DllCall that you posted.  Something like DllCall("Kernel32.dll\GetFileTime"...  The problem is I don't think the modified time for a text file or a pdf file is going to be stored in a dll file.  I'm assuming that the information has to be stored with the file (if the UTC modified time is even saved at all).


Linear Spoon:

At first glance I thought you had something with that equation.  The problem is the equation calculates the file's UTC time based on my current location and not the location where the file was modified.  If I modify a file in Atlanta, GA at 5pm and someone modifies a file at the same time in Houston, TX (4pm) and I compare the two files on my local machine, the equation would tell me the Houston file was created at 9pm UTC and the Atlanta file at 10pm UTC, when they were actually both modified at 10pm UTC.


I think the key is going to be pulling either the UTC time directly from the file (text, pdf, etc...) or being able to pull the location where the file was modified.


Thank you for the help.  Any other thoughts or suggestions?

  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005

have no solution, just thanks to user "Linear Spoon" for the example, didn't realized the existing ahk-variable A_NowUTC

Howard from Toronto
  • New members
  • 1 posts
  • Last active: Aug 03 2015 10:20 PM
  • Joined: 03 Aug 2015

Unfortunately, Linear Spoon's suggested solution will not work reliably. It uses ordinary arithmetic, rather than the date-string sensitive arithmetic of the EnvAdd and EnvSub commands. I'm currently 4 hours behind UTC. The arithmetic difference between A_Now and A_NowUTC is 40000, but the true difference is only 14400 seconds. If I'm adjusting time within the same day, it doesn't matter whether I use ordinary or date-string arithmetic, but it does make a difference if I cross the midnight boundary. The following source code does the arithmetic both ways and displays the difference:

; UTC correction test

LastModified := 20150802222500 ; The local time is 22:25, less than 4 hours before midnight
TimeUTC := A_NowUTC
TimeLocal := A_Now
; Compute the difference using ordinary arithmetic, treating timestamps as very long integers
Delta1 := TimeUTC - TimeLocal
; Compute the difference using functions that parse timestamps correctly
Delta2 := TimeUTC
EnvSub Delta2, %TimeLocal%, Seconds
; Adjust the local time using ordinary arithmetic
LastModifiedUTC1 := LastModified + Delta1
; Adjust the local time using functions that parse timestamps correctly
LastModifiedUTC2 := LastModified
EnvAdd LastModifiedUTC2, %Delta2%, Seconds
; Display the values of all variables
Message := "LastModified = " . LastModified . "`n"
         . "TimeUTC = " . TimeUTC . "`n" 
         . "TimeLocal = " . TimeLocal . "`n" 
         . "Delta1 = " . Delta1 . "`n" 
         . "Delta2 = " . Delta2 . "`n" 
         . "LastModifiedUTC1 = " . LastModifiedUTC1 . "`n" 
         . "LastModifiedUTC2 = " . LastModifiedUTC2 . "`n" 
MsgBox %Message%