Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a datetime ± infinity?

For floats we have special objects like -inf (and +inf), and which are guaranteed to compare less than (and greater than) other numbers.

I need something similar for datetimes, is there any such thing? In-db ordering must work correctly with django queryset filters, and ideally it should be db-agnostic (but at the very least it must work with mysql and sqlite) and timezone-agnostic.

At the moment I'm using null/None, but it is creating very messy queries because None is doing the job of both -inf and +inf and I have to explicitly account for all those cases in the queries.

like image 556
wim Avatar asked Mar 12 '14 15:03

wim


3 Answers

Try this:

>>> import datetime
>>> datetime.datetime.max
datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)

You can get min/max for datetime, date, and time.

like image 88
Ben Avatar answered Nov 12 '22 06:11

Ben


There isn't; the best you have is the datetime.datetime.min and datetime.datetime.max values.

These are guaranteed to be the smallest and largest datetime values, but datetime.datetime.min == datetime.datetime.min is still True; everything else is larger. The inverse is true for the datatime.datetime.max value.

There are also min and max values for datetime.date and datetime.time.

like image 36
Martijn Pieters Avatar answered Nov 12 '22 07:11

Martijn Pieters


In case someone is using dates in Pandas dataframe:

>>> import pandas as pd
>>> pd.Timestamp.min
Timestamp('1677-09-21 00:12:43.145225')
>>> pd.Timestamp.max
Timestamp('2262-04-11 23:47:16.854775807')
like image 2
Shovalt Avatar answered Nov 12 '22 06:11

Shovalt