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.

7
  • 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? Commented Apr 8, 2016 at 19:23
  • If think I (just) have to define a timezone and ask pytz to localize it, allowing for DST. Commented Apr 8, 2016 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. Commented Apr 8, 2016 at 19:35
  • It's a cvi (NI) call GetFileTime(). Commented Apr 8, 2016 at 19:36
  • GetFileTime() is a win32 API I think, am I correct? See also timeanddate.com/time/dst/events.html. Commented Apr 8, 2016 at 19:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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).

4 Comments

I am trying to match the behavior of a c-based call.
@Jiminion: which c-based call?
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?
"Seconds since epoch" aka "Unix time" is in UTC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.