简体   繁体   中英

Python's Datetime: Conversion to time zones based on lat_long

I have been reading python's datetime documentation and it seems like there are few things which are not clearly mentioned there.

date = datetime.datetime(2014,10,1,11,45,30)

Which timezone will the above date be in? UTC?

If I have to make sure that the above date remains in EST what could be done. I am not clear on tzinfo object here?

If I have to convert these datetimes to some other time zones based on latitude and longitude what should I do?

Your code would create a "timezone naive" datetime object. That means - no timezone. It'll be interpreted as local time based on where it is used.

If you want to set a timezone, try using the pytz library.

import pytz  # 3rd party: $ pip install pytz

u = datetime.utcnow()
u = u.replace(tzinfo=pytz.utc) # NOTE: it works only with a fixed utc offset

# Then you can change timezones, e.g. http://www.timezoneconverter.com/cgi-bin/zoneinfo?tz=America/New_York
print u.astimezone(pytz.timezone("America/New_York"))

As for the lat/lon conversion to a timezone, this isn't a simple task. Here's a question that discusses possible solutions.

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