In Python, let\'s say I have a date of 25 December 2016. How can I create a timezone-aware datetime of noon on that date?
Bonus points if it\'s compatible with Django\'
Erik's answer to this question explains why you should use datetime.combine()
followed by timezone.localize()
. Setting the timezone directly on the datetime.time instance will not take the date into account, which could leave you with an incorrect result (due to daylight savings or even historic transitions). datetime.combine()
just isn't that smart!
>>> import pytz
>>> import datetime
>>> d = datetime.date(2016, 12, 25)
>>> t = datetime.time(12)
>>> tz = pytz.timezone('US/Pacific')
>>> tz.localize(datetime.datetime.combine(d,t))
datetime.datetime(2016, 12, 25, 12, 0, tzinfo=)