os.path.getmtime on winXP

Bengt Richter bokr at oz.net
Tue Nov 8 04:33:00 EST 2005


On Tue, 08 Nov 2005 07:57:44 +0100, =?ISO-8859-1?Q?Jorg_R=F8dsj=F8?= <jorg at neoplex.org> wrote:

>[sorry to those reading twice, but I just realised that I had posted 
>this after mucking about with the date on my machine to try to figure 
>this out -- so the message probably went into last months messages for 
>most people including me.]
>
>Hi
>
>I'm trying to use os.path.getmtime to check if a file has been modified. 
>  My OS is WinXP. The problem is, that when the os changes from/to 
>daylight savings time, the result is suddenly off by 3600 seconds ie. 
>one hour, even if the file remains the same.

Well, if the file hasn't been modified, the file time should be wrt
a constant epoch, so you must be observing a DST-corrected conversion
of that number, but you don't show what you are using.
E.g. you can convert with time.localtime or time.gmtime, and format the
result any way you want with time.strftime(...)

 >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime('wc.py')))
 '2003-09-10 14:38:57'
 >>> time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(os.path.getmtime('wc.py')))
 '2003-09-10 21:38:57'

Which comes from
 >>> os.path.getmtime('wc.py')
 1063229937

And default localtime formatting is
 >>> time.ctime(os.path.getmtime('wc.py'))
 'Wed Sep 10 14:38:57 2003'

which is
 >>> time.asctime(time.localtime(os.path.getmtime('wc.py')))
 'Wed Sep 10 14:38:57 2003'

the GMT version of which is
 >>> time.asctime(time.gmtime(os.path.getmtime('wc.py')))
 'Wed Sep 10 21:38:57 2003'

reflecting
 >>> os.system('dir wc.py')
  Volume in drive C is System
  Volume Serial Number is 14CF-C4B9

  Directory of c:\pywk\clp

 03-09-10  14:38                    595 wc.py


>
>I've tried using win32file.GetFileTime, and it reports a consistent 
>number, regardless of DST.
>
>What is happening here? My first thought is that getmtime should measure 
>'raw' time, and not be concerned with DST, and thus give me the same 
>result no matter the date on the machine I call it. Is the module broken 
>in some way, or am I just missing something here?
>
How did you format the number you got from os.path.getmtime?
You might want to try some of the above.

If you actually created/modified files just before and after the DST change
and saw an extra hour difference instead of the time between the two actions,
then maybe I'd look into whether the OS has some perverse option to use local DST
time to record in the file stat info, but that's hard to believe. More likely someone
is messing with with raw file time setting, like touch. Don't have it handy to see
what DST assumptions it makes if any.

Regards,
Bengt Richter



More information about the Python-list mailing list