|
|
|
|
|
by jessedhillon
5256 days ago
|
|
IMO dateutil provides this in a more transparent way by providing tzinfo subclasses: py> from dateutil.tz import gettz
py> from datetime import datetime
py> new_york = gettz('America/New York')
py> los_angeles = gettz('America/Los Angeles')
py> minsk = gettz('Europe/Minsk')
py> fmt = "%Y-%m-%d %H:%M%z"
py> datetime.now(new_york).strftime(fmt)
'2012-02-03 01:51-0500'
py> datetime.now(los_angeles).strftime(fmt)
'2012-02-02 22:51-0800'
py> datetime.now(los_angeles).astimezone(minsk).strftime(fmt)
'2012-02-03 09:51+0300'
For me, the most annoying thing about Python is that out of the box, AFAICT, you cannot get a TZ-aware datetime. In fact, while tzinfo is part of the standard library, I don't know of any included solution for making TZ-aware datetime, short of writing your own subclasses for each timezone.EDIT: datetime.utcnow() is TZ-aware, I meant any other timezone. |
|