timezone aware vs. timezone naive in python

2024/10/6 12:27:13

I am working with datetime objects in python. I have a function that takes a time and finds the different between that time and now.

def function(past_time):now = datetime.now()diff = now - past_time

When I initialized past_time before passing it to this function I initialized it as datetime naive. And now is also a datetime naive object. However when I try to call this function I get the error: can't subtract offset-naive and offset-aware datetimes. How come this is the case if they are both theoretically datetime naive objects?

Any help would be appreciated. Thanks!

Answer

datetime doesn't do any cross time zone calculations, because it's a complex and involved subject.

I suggest converting dates to UTC universally and performing maths on those.

I recently completed a project using timezones in a large python/Django project and after investigation went with converting everything internally to UTC and converting only on display to the user.

You should look into pytz to do the conversions to/from UTC, and store Olson codes for the timezones you want in your app - perhaps associated with each user, or appropriate to your program.

https://en.xdnf.cn/q/70369.html

Related Q&A

How to return a value from Python script as a Bash variable?

This is a summary of my code:# import whateverdef createFolder():#someCodevar1=Gdrive.createFolder(name)return var1 def main():#someCodevar2=createFolder()return var2if __name__ == "__main__"…

How to align text to the right in ttk Treeview widget?

I am using a ttk.Treeview widget to display a list of Arabic books. Arabic is a right-to-left language, so the text should be aligned to the right. The justify option that is available for Label and o…

ImportError: cannot import name RemovedInDjango19Warning

Im on Django 1.8.7 and Ive just installed Django-Allauth by cloning the repo and running pip install in the apps directory in my webapp on the terminal. Now when I run manage.py migrate, I get this err…

How does Yahoo Finance calculate Adjusted Close stock prices?

Heres how Yahoo Finance apparently calculates Adjusted Close stock prices:https://help.yahoo.com/kb/adjusted-close-sln28256.htmlFrom this, I understand that a constant factor is applied to the unadjust…

Celery design help: how to prevent concurrently executing tasks

Im fairly new to Celery/AMQP and am trying to come up with a task/queue/worker design to meet the following requirements.I have multiple types of "per-user" tasks: e.g., TaskA, TaskB, TaskC. …

Google App Engine - Using Search API Python with list fields

Im using ndb.Model. The Search API has the following field classes:TextField : plain textHtmlField : HTML formatted textAtomField : a string which is treated as a single tokenNumberField : a numeric v…

Handling PyMySql exceptions - Best Practices

My question regards exception best practices. Ill present my question on a specific case with PyMySQL but it regards errors handling in general. I am using PyMySQL and out of the many possible exceptio…

Beautifulsoup find element by text using `find_all` no matter if there are elements in it

For examplebs = BeautifulSoup("<html><a>sometext</a></html>") print bs.find_all("a",text=re.compile(r"some"))returns [<a>sometext</a>] …

Python: how to get values from a dictionary from pandas series

I am very new to python and trying to get value from dictionary where keys are defined in a dataframe column (pandas). I searched quite a bit and the closest thing is a question in the link below, but…

Django No Module Named URLs error

There are many similar questions posted already, but Ive already tried those solutions to no avail. Im working through a basic Django tutorial, and here is my code:urls.pyfrom django.conf.urls import …