문제

Something I'm currently working on requires localised times for users around the world. All datetimes are stored as UTC, so converting them is easy enough and we have a known, safe, point of reference, etc, etc.

However, something about how the offset is expressed is making me scratch my head a moment.

>>> timezone.now()  # get the UTC-stamped server time as an example
datetime.datetime(2013, 5, 21, 16, 37, 54, 62598, tzinfo=<UTC>)

>>> eastern = pytz.timezone('US/Eastern')  # localise this to US/Eastern
>>> utc_dt.astimezone(eastern)
datetime.datetime(2013, 5, 21, 12, 37, 54, 62598,   
    tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>) 

(That's my line break in the datetime output, just to make it easier to spot the bit I'm on about.)

That expression of the offset seems, well, a bit over the top. Rather than simply saying it's an offset of -4h from UTC, it looks like it's saying it's minus one day plus 20:00 hours. Is that right?

도움이 되었습니까?

해결책

You're seeing the repr of the pytz timezone class, which includes implementation details that shouldn't matter when you use it in real life. If you print that same object you'll see something different:

>>> print utc_dt.astimezone(eastern)
2013-05-21 15:00:27.648000-04:00
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top