Timezone in Python

Just as a mark

If we want to set PST timezone for a given datetime, it should be usually UTC-8. But sometimes it could return UTC-7:53. How should we resolve? Use localize instead of replace timezone

>>> import datetime
>>> import pytz
>>> n = datetime.datetime.now()
>>> n
datetime.datetime(2018, 12, 7, 5, 26, 5, 546284)
>>> n.replace(tzinfo=pytz.timezone('US/Pacific'))
datetime.datetime(2018, 12, 7, 5, 26, 5, 546284, tzinfo=<DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD>)
>>> pytz.timezone('US/Pacific').localize(n)
datetime.datetime(2018, 12, 7, 5, 26, 5, 546284, tzinfo=<DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD>)

你可能感兴趣的:(编程语言)