简体   繁体   中英

How do I handle a series of timestamps from different timezones in Python?

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 US 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 (ie 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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM