Page 1 of 1
GetFileDate & time zone
Posted: Mon Aug 13, 2012 3:32 am
by Tenaja
I am using GetFileDate to see if an open file is modified outside of my program, so I can warn the user it has been changed. Having traveled a few times recently, I changed the timezone of my pc while my program was open. Immediately, I was warned that the open files were open.
That means that GetFileDate does NOT actually return the last modified time, but rather, some other calculated value based on the time zone. This little loop demonstrates it--just run it, and change the timezone of your pc while it is running, and you will see the numbers change.
Code: Select all
test1:
Debug GetFileDate("TestFile.txt", #PB_Date_Modified)
Delay(150)
Goto test1
Is there a (cross-platform) way to get the "real" file modified date, so you can "safely" use this command?
Re: GetFileDate & time zone
Posted: Mon Aug 13, 2012 7:11 am
by jassing
Add or subtract the current system's timezone offset....
Re: GetFileDate & time zone
Posted: Mon Aug 13, 2012 5:02 pm
by Tenaja
jassing wrote:Add or subtract the current system's timezone offset....
Yes, of course that is what I was planning...
But what are the cross-platform commands?
Re: GetFileDate & time zone
Posted: Mon Aug 13, 2012 6:27 pm
by jassing
Here's code I use:
(but it is not cross platform...)
Code: Select all
EnableExplicit
Procedure.s GetTimeZone()
Protected cTimeZone.s, iTimeZone.Time_zone_information
With iTimeZone
Select GetTimeZoneInformation_(iTimeZone)
Case #TIME_ZONE_ID_STANDARD, #TIME_ZONE_ID_DAYLIGHT
cTimeZone = PeekS(@\StandardName,-1,#PB_Unicode)
EndSelect
EndWith
ProcedureReturn cTimeZone
EndProcedure
Procedure.f GetTimeZoneOffset(nHow.i = #PB_Date_Minute)
; Pass #pb_date_hour to get standard hour offset.
Protected nOffset.f, iTimeZone.Time_zone_information
With iTimeZone
Select GetTimeZoneInformation_(iTimeZone)
Case #TIME_ZONE_ID_STANDARD : nOffset = (\Bias+\StandardBias)
Case #TIME_ZONE_ID_DAYLIGHT : nOffset = (\Bias+\DaylightBias)
;Case #TIME_ZONE_ID_INVALID
;Case #TIME_ZONE_ID_UNKNOWN
Default : nOffset = 0 ; Invalid results...
EndSelect
EndWith
If nHow = #PB_Date_Hour : nOffset / 60 : EndIf
; nOffset is a number to GET to UTC; Generally speaking we use
; a value which is FROM UTC so if you want that, multiply the offset by -1
ProcedureReturn nOffset
EndProcedure
Macro DateToUTC( nDate )
AddDate(nDate, #PB_Date_Minute, GetTimeZoneOffset())
EndMacro
Debug "Our TimeZone is "+#DQUOTE$+GetTimeZone()+#DQUOTE$
Debug "Our TimeZone UTC "+StrF(GetTimeZoneOffset(#PB_Date_Hour)*-1,1)
Debug "UTC: "+FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss",DateToUTC(Date()))
Re: GetFileDate & time zone
Posted: Tue Aug 14, 2012 1:29 am
by RichAlgeni
Aren't all dates associated to files supposed to be in universal time format now (UTC), then converted to local time when displayed?
I'm wondering if instead of the date and time, you might want to do an SHA1 fingerprint hash of the file? When the hashes match, you'll know that no changes have been made. For instance, someone could have read in the file, then wrote back out without making a change to it. In that case you could get a false positive. With a hash, you should never get a false positive.
Code: Select all
Result$ = SHA1FileFingerprint(Filename$)
Re: GetFileDate & time zone
Posted: Tue Aug 14, 2012 4:51 am
by jassing
RichAlgeni wrote:Aren't all dates associated to files supposed to be in universal time format now (UTC), then converted to local time when displayed?
I'm not aware of any common-to-all-OS's "requirement" to do that.
Think about this:
You create a file, then you want to see it's date --- you want that date to not be when you created it in local time, but you want that time to be in utc time? I'd say that's not the way most would want to see it. It's a trivial enough task to adjust for UTC...
Re: GetFileDate & time zone
Posted: Tue Aug 14, 2012 4:24 pm
by RichAlgeni
I should have a bit more clear Jassing, I meant that all dates are kept internally in UTC, then when displayed, they are converted to local time? I know there was a controversy years ago where date for files in different time zones didn't match up correctly.
Regardless of that, using a hash is just another way to accomplish your task.
Re: GetFileDate & time zone
Posted: Tue Aug 14, 2012 4:41 pm
by RichAlgeni
Maybe this will help those I may have inadvertently confused..., like myself!
http://stackoverflow.com/questions/2532 ... -practices
Re: GetFileDate & time zone
Posted: Wed Aug 15, 2012 8:28 am
by ultralazor
store utc/gmt offsets and be done with it.
use constants preloaded on win/nt, use shell output parsing on others with date commands; i dont think there is a universal api for this on bsd or linux..