👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
What I need to do
I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezone-aware datetime objects. I do not want to convert my entire application to timezone unaware for this one legacy case.
What I"ve Tried
First, to demonstrate the problem:
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> import pytz
>>> unaware = datetime.datetime(2011,8,15,8,15,12,0)
>>> unaware
datetime.datetime(2011, 8, 15, 8, 15, 12)
>>> aware = datetime.datetime(2011,8,15,8,15,12,0,pytz.UTC)
>>> aware
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=<UTC>)
>>> aware == unaware
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can"t compare offset-naive and offset-aware datetimes
First, I tried astimezone:
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: astimezone() cannot be applied to a naive datetime
>>>
It"s not terribly surprising this failed, since it"s actually trying to do a conversion. Replace seemed like a better choice (as per How do I get a value of datetime.today() in Python that is "timezone aware"?):
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=<UTC>)
>>> unaware == aware
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can"t compare offset-naive and offset-aware datetimes
>>>
But as you can see, replace seems to set the tzinfo, but not make the object aware. I"m getting ready to fall back to doctoring the input string to have a timezone before parsing it (I"m using dateutil for parsing, if that matters), but that seems incredibly kludgy.
Also, I"ve tried this in both Python 2.6 and Python 2.7, with the same results.
Context
I am writing a parser for some data files. There is an old format I need to support where the date string does not have a timezone indicator. I"ve already fixed the data source, but I still need to support the legacy data format. A one time conversion of the legacy data is not an option for various business BS reasons. While in general, I do not like the idea of hard-coding a default timezone, in this case it seems like the best option. I know with reasonable confidence that all the legacy data in question is in UTC, so I"m prepared to accept the risk of defaulting to that in this case.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from How to make a timezone aware datetime object in Python?, check other ast Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How to make a timezone aware datetime object in Python?
- Deutsch How to make a timezone aware datetime object in Python?
- Français How to make a timezone aware datetime object in Python?
- Español How to make a timezone aware datetime object in Python?
- Türk How to make a timezone aware datetime object in Python?
- Русский How to make a timezone aware datetime object in Python?
- Português How to make a timezone aware datetime object in Python?
- Polski How to make a timezone aware datetime object in Python?
- Nederlandse How to make a timezone aware datetime object in Python?
- 中文 How to make a timezone aware datetime object in Python?
- 한국어 How to make a timezone aware datetime object in Python?
- 日本語 How to make a timezone aware datetime object in Python?
- हिन्दी How to make a timezone aware datetime object in Python?
Moscow | 2023-03-21
Simply put and clear. Thank you for sharing. How to make a timezone aware datetime object in Python? and other issues with datetime Python module was always my weak point 😁. Checked yesterday, it works!
Moscow | 2023-03-21
Thanks for explaining! I was stuck with How to make a timezone aware datetime object in Python? for some hours, finally got it done 🤗. Checked yesterday, it works!
Prague | 2023-03-21
Simply put and clear. Thank you for sharing. How to make a timezone aware datetime object in Python? and other issues with copy Python module was always my weak point 😁. Will use it in my bachelor thesis