问题
Is there a built-in library or does anyone have available a function to convert a string timezone such as "America/New_York" to a datetime.tzinfo object?
Thanks.
回答1:
Yes, you need the pytz library:
import datetime, pytz
zoneName = 'America/New_York'
now = datetime.datetime.now(pytz.timezone(zoneName))
returns:
datetime.datetime(2011, 3, 16, 1, 39, 33, 87375, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)
来源:https://stackoverflow.com/questions/5321466/how-to-convert-string-timezones-in-form-country-city-into-datetime-tzinfo