GetFileDate & time zone

Just starting out? Need help? Post your questions and find answers here.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

GetFileDate & time zone

Post 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?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: GetFileDate & time zone

Post by jassing »

Add or subtract the current system's timezone offset....
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: GetFileDate & time zone

Post 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?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: GetFileDate & time zone

Post 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()))
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: GetFileDate & time zone

Post 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$)
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: GetFileDate & time zone

Post 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...
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: GetFileDate & time zone

Post 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.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: GetFileDate & time zone

Post by RichAlgeni »

Maybe this will help those I may have inadvertently confused..., like myself!

http://stackoverflow.com/questions/2532 ... -practices
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: GetFileDate & time zone

Post 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..
so many ideas so little time..
Post Reply