Daylight savings and getmtime

Nick Coghlan ncoghlan at iinet.net.au
Fri Jan 28 23:45:47 EST 2005


wittempj at hotmail.com wrote:
> on my windows xp box os.path.getmtime gives back local time (I just
> saved the file):
> 
>>>>os.path.getmtime('c:\\temp\\testset.py')
> 
> 1106955016
> 
>>>>print time.mktime(time.localtime())
> 
> 1106955034.0

No. mktime is converting the local time to UTC. Try this instead:

Py> time.gmtime(os.path.getmtime("c:/devel/mtime_test.txt"))
(2005, 1, 29, 3, 35, 37, 5, 29, 0)

Py> time.localtime(os.path.getmtime("c:/devel/mtime_test.txt"))
(2005, 1, 29, 15, 35, 37, 5, 29, 1)

(This is with my machine set to Sydney time, so that I'm at UTC + 10, with 
daylight savings currently active)

> You can try to figure out if DST is on by comparing time.localtime()
> versus time.gmtime(). In the Western European Timezone there's one hour
> difference in winter and two hours  in summer.

To figure out if DST is currently active, you can use:

Py> import time
Py> time.localtime()
(2005, 1, 29, 14, 29, 26, 5, 29, 0)
Py> time.localtime().tm_isdst
0

Switching to Sydney time (which actually uses DST, unlike Brisbane) gives:

Py> import time
Py> time.localtime().tm_isdst
1

The same trick will work on historical dates, too. For instance, consider a file 
created last winter, when Sydney was NOT on daylight savings:

Py> time.localtime(os.path.getmtime("c:/log.txt"))
(2004, 7, 20, 19, 46, 35, 1, 202, 0)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list