I've got a datetime
which has no timezone information. I'm now getting the timezone info and would like to add the timezone into the existed datetime instance, how can I do?
d = datetime.datetime.now() tz = pytz.timezone('Asia/Taipei')
How to add the timezone info tz
into datetime a
Timezone aware object using datetime now(). time() function of datetime module. Then we will replace the value of the timezone in the tzinfo class of the object using the replace() function. After that convert the date value into ISO 8601 format using the isoformat() method.
To make them timezone-aware, you must attach a tzinfo object, which provides the UTC offset and timezone abbreviation as a function of date and time. For zones with daylight savings time, python standard libraries do not provide a standard class, so it is necessary to use a third party library.
Use tz.localize(d)
to localize the instance. From the documentation:
The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information):
>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0)) >>> print(loc_dt.strftime(fmt)) 2002-10-27 06:00:00 EST-0500
If you don't use tz.localize()
, but use datetime.replace()
, chances are that a historical offset is used instead; tz.localize()
will pick the right offset in effect for the given date. The US Eastern timezone DST start and end dates have changed over time, for example.
When you try to localize a datetime value that is ambiguous because it straddles the transition period from summer to winter time or vice-versa, the timezone will be consulted to see if the resulting datetime object should have .dst()
return True or False. You can override the default for the timezone with the is_dst
keyword argument for .localize()
:
dt = tz.localize(naive, is_dst=True)
or even switch off the choice altogether by setting is_dst=None
. In that case, or in the rare cases there is no default set for a timezone, an ambiguous datetime value would lead to a AmbiguousTimeError
exception being raised. The is_dst
flag is only consulted for datetime values that are ambiguous and is ignored otherwise.
To go back the other way, turn a timezone-aware object back to a naive object, use .replace(tzinfo=None)
:
naivedt = awaredt.replace(tzinfo=None)
If you know that your original datetime was "measured" in the time zone you are trying to add to it, you could (but probably shouldn't) use replace
rather than localize
.
# d = datetime.datetime.now() # tz = pytz.timezone('Asia/Taipei') d = d.replace(tzinfo=tz)
I can imagine 2 times when this might make sense (the second one happened to me):
datetime
instance by making it aware of this incorrect timezone (and presumably later localizing it to the "correct" time zone so the values of now() match up to other times you are comparing it to (your watch, perhaps)time
instance (NOT a datetime
) with a time zone (tzinfo) attribute so that attribute can be used later to form a full datetime
instance.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With