1

I have a large dataset with timestamps from various timezones stretching back exactly one year. Each entry includes a timestamp and the seconds by which the timestamp's timezone is removed from UTC at present. So, for example, a timestamp from the Eastern U.S. timezone would have 14400 attached to it. This might be sort of easily handled if not for the fact that timestamps from non-DST times (i.e. the beginning of the year) also have 14400s attached to them, signifying the Eastern time zone.

Is there any particularly efficient way for me to convert this pile of data into pure-UTC timestamps in Python, taking DST into account?

1 Answer 1

0

You can use dateutil module and pytz module like that:

import dateutil.parse
import pytz

parsed_as_utc = dateutil.parser.parse(timestamp).astimezone(pytz.utc)
2
  • How can different timezones be taken into account? This works well for timestamps in my timezone (Eastern), but not so much when the system time isn't the same as the intended time of the timestamp.
    – Hevrioch
    Jul 22, 2012 at 2:01
  • @Hevrioch: My example is assuming, that you are storing ISO-8601 timestamps as text. These timestamps contain information about time zone, so parsed datetime contains it also. By invoking .astimezone(pytz.utc) you convert timestamp in different timezone into UTC time. This is exactly what you requested ("convert this pile of data into pure-UTC timestamps").
    – Tadeck
    Jul 22, 2012 at 6:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.