Python 3 Timedelta OverflowError

2024/10/5 5:23:57

I have a large database that I am loading into an in-memory cache. I have a process that does this iterating through the data day by day.

Recently this process has started throwing the following error:

OverflowError: date value out of range for the line

start_day = start_day - datetime.timedelta(days = 1)

This is running in Python 3.4.3 on Ubuntu 14.04.5

Answer

You have reached datetime.date.min, or the first of January of the year 1:

>>> from datetime import date, timedelta
>>> date.min
datetime.date(1, 1, 1)
>>> date.min - timedelta(days=1)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
OverflowError: date value out of range

If you started at datetime.date.today(), then it took your code a little over 736k steps to get there:

>>> date.today().toordinal()
736766

Your code probably has a bug somewhere that is subtracting too often.

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

Related Q&A

Constructing hierarchy from dictionary/JSON

Im looking for a way to create hierarchy in form of child parent relationship between two or more instances of same class.How would one go about creating such objects from nested dictionary like in exa…

PyPy file append mode

I have code like this:f1 = open(file1, a) f2 = open(file1, a)f1.write(Test line 1\n) f2.write(Test line 2\n) f1.write(Test line 3\n) f2.write(Test line 4\n)When this code is run with standard Python 2.…

Clean up ugly WYSIWYG HTML code? Python or *nix utility

Im finally upgrading (rewriting ;) ) my first Django app, but I am migrating all the content. I foolishly gave users a full WYSIWYG editor for certain tasks, the HTML code produced is of course terribl…

Using config files written in Python [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Model not defined when using foreign key to second model

Im trying to create several relationships between some models such as User and Country. When I try to syncdb, my console outputs "Name Country is not defined". Here is the code: class User(mo…

Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request

Im trying to send a trigger to a Zapier webhook in the form of a POST request containing JSON. It works fine if I just send the POST request through a local python script.What I want to do is create a …

django deploy to Heroku : Server Error(500)

I am trying to deploy my app to heroku. Deploy was done correctly but I got Server Error(500). When I turned DEBUG true, Sever Error doesnt occurred. So I think there is something wrong with loading st…

Should I preallocate a numpy array?

I have a class and its method. The method repeats many times during execution. This method uses a numpy array as a temporary buffer. I dont need to store values inside the buffer between method calls. …

dup, dup2, tmpfile and stdout in python

This is a follow up question from here.Where I want do go I would like to be able to temporarily redirect the stdout into a temp file, while python still is able to print to stdout. This would involve …

How do I write a Class-Based Django Validator?

Im using Django 1.8.The documentation on writing validators has an example of a function-based validator. It also says the following on using a class:You can also use a class with a __call__() method f…