2

Given a local timestamp and a UTC timestamp, is it possible to determine the timezone and whether DST is in effect?

2 Answers 2

3

If your "UTC timestamp" is a float (int would suffice) of "seconds from the epoch", and your "local timestamp" is some weird version of it shifted into your local time coordinates (there is no "epoch" except the UTC one), module time in the standard Python library suffices.

As these docs show, given "seconds from the epoch", gmtime translates that into a 9-items tuple in UTC, localtime into a 9-items tuple in local time; to go the other way around, calendar.timegm (UTC 9-items tuple to UTC timestamp) and mktime (local time 9-items tuple to UTC timestamp). You'll note there's no such thing as a "local timestamp" (only local time 9-items tuples aka struct_time instances), simply because no such concept exists.

But, if I understand correctly what you mean by "local timestamp", then translating it to a struct time "as if" it was a real timestamp, and then back into local time, will give the offset you seek (in seconds). As for DST, a bit telling whether it's on is the 9th item of the tuples we've been mentioning.

For example, using the "local timestamp" only:

>>> loct
1282453595
>>> time.mktime(time.gmtime(loct)) - loct
28800.0
>>> _ / 3600
8.0
>>> 

This says I'm 8 hours west of UTC. And:

>>> time.localtime(loct)[-1]
1

this says that it is with DST in effect.

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

1 Comment

Here was the problem: I have a UTC timestamp which represents 00:00:00 on an arbitrary date at an arbitrary timezone. I know what that arbitrary date is, but I do not know the timezone. If I convert the timestamp into a datetime object, I can use the hour value to determine the timezone, but the problem is DST. If DST is in effect, the timezone would be shifted by one. However, I'm sure that the timezone will be one in the US. Anyhow, I found a solution to the problem (albeit not a clean one, it'll suffice.) Thank you!
0

Try taking a look at http://docs.python.org/library/datetime.html#datetime.datetime for built-in support for dates, times, and timezones.

If you need a more powerful library, peek at the suggestions here: Full-featured date and time library

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.