I am using calls such as this in Python 3.4:
x = datetime.fromtimestamp(os.path.getctime(somefilename))
y = datetime.fromtimestamp(os.path.getmtime(somefilename))
Will x and y be timezone-aware, as per the definition of that term in the datetime documentation? Does this vary between platforms? I assume that in theory the ctime and mtime of a file are measured against the seconds since the epoch in UTC, so the answer should be yes?
If so, is that true across all/most POSIX platforms? Specifically, is it true on modern Linux/OS X?
If not, is there a better way to handle this? How can I get timezone-aware ctime and mtime data? What timezone do platforms use for expressing/storing ctime and mtime?
解决方案
On OSX, at least, os.path.getctime returns a TZ-naive datetime in the system's timezone.
$ date
Mon Jun 8 15:08:40 PDT 2015
$ touch new_file
$ python
>>> from datetime import datetime
>>> import os
>>> datetime.fromtimestamp(os.path.getctime('new_file'))
datetime.datetime(2015, 6, 8, 15, 8, 42)
>>> print datetime.fromtimestamp(os.path.getctime('new_file')).tzinfo
None
time.timezone will give you the local timezone offset in seconds, not accounting for DST. The pytz library will probably be very useful to you.