6

This does not work:

t = os.path.getmtime(filename)
dTime = datetime.datetime.fromtimestamp(t)
justTime = dTime.timetuple()
if justTime.tm_isdst == 0 :
    tDelta = datetime.timedelta(hours=0)
else:
    tDelta = datetime.timedelta(hours=1)

What happens instead is that the conditional always equals 1, despite some of the timestamps being within daytime savings time.

I am trying to make a python call match the behavior of a c-based call.

Jiminion
  • 5,080
  • 1
  • 31
  • 54
  • Filesystems store the date/time stamp as the number of seconds since epoch, which does not include any timezone or DST information. That is only added when you translate the epoch time to a displayable format. Which c-based call are you trying to match? – cdarke Apr 08 '16 at 19:23
  • If think I (just) have to define a timezone and ask pytz to localize it, allowing for DST. – Jiminion Apr 08 '16 at 19:30
  • 1
    You should be able to just used your current timezone and not to add one. DST is not applied on the same date or time across timezones. – cdarke Apr 08 '16 at 19:35
  • It's a cvi (NI) call GetFileTime(). – Jiminion Apr 08 '16 at 19:36
  • GetFileTime() is a win32 API I think, am I correct? See also http://www.timeanddate.com/time/dst/events.html. – cdarke Apr 08 '16 at 19:37
  • Haven't used GetFileTime() API in a while, but it uses epoch time, it is not adjusted to a timezone. See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx – cdarke Apr 08 '16 at 19:39
  • I note that FAT **does** use local time, I didn't realise that. I guess only memory sticks use FAT these days. https://msdn.microsoft.com/en-us/library/windows/desktop/ms724290(v=vs.85).aspx – cdarke Apr 08 '16 at 19:42

2 Answers2

6

To find out whether a given timestamp corresponds to daylight saving time (DST) in the local time zone:

import os
import time

timestamp = os.path.getmtime(filename)
isdst = time.localtime(timestamp).tm_isdst > 0

It may fail. To workaround the issues, you could get a portable access to the tz database using pytz module:

from datetime import datetime
import tzlocal  # $ pip install tzlocal

local_timezone = tzlocal.get_localzone() # get pytz tzinfo
isdst = bool(datetime.fromtimestamp(timestamp, local_timezone).dst())

Related: Use Python to find out if a timezone currently in daylight savings time.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

Why do you assume that your filesystem writes down timestamps with a timezone?

On a server, you stick to UTC which does not have DST. On a desktop, you should look up the latest moment of the DST change (on or off), and correct the time accordingly. I don't know if pytz has this information, but this information is definitely available on the web in a machine-readable form.

Note that for some moments during the transition from DST some values of local time occur twice, and it's impossible to tell if a particular timestamp (e.g. 2:30 am) occurred before or after the switch (within an hour).

9000
  • 39,899
  • 9
  • 66
  • 104
  • I am trying to match the behavior of a c-based call. – Jiminion Apr 08 '16 at 18:53
  • @Jiminion: which c-based call? – cdarke Apr 08 '16 at 19:27
  • `getmtime()` returns "seconds since epoch" that is not in any timezone (it is not in UTC, it is not in the local timezone). It is the same number around the world (it doesn't matter what timezone the server uses as long as its time is synchronized with a desired clock). See [Does Python's `time.time()` return the local or UTC timestamp?](http://stackoverflow.com/a/20035913/4279) – jfs Apr 11 '16 at 23:48
  • "Seconds since epoch" aka ["Unix time" is in UTC](https://en.wikipedia.org/wiki/Unix_time). – 9000 Apr 12 '16 at 01:44