postgreSQL supports infinity for datetime:
http://www.postgresql.org/docs/current/static/datatype-datetime.html#AEN6027
{{{
infinity date, timestamp later than all other time stamps
-infinity date, timestamp earlier than all other time stamps
}}}
Mapping this to python is not possible at the moment.
See:
http://initd.org/psycopg/docs/usage.html#infinite-dates-handling
{{{
PostgreSQL can store the representation of an “infinite” date, timestamp, or interval. Infinite dates are not available
to Python, so these objects are mapped to date.max, datetime.max, interval.max. Unfortunately the mapping cannot be
bidirectional so these dates will be stored back into the database with their values, such as 9999-12-31.
}}}
I don't know the internals of the datetime module. I guess it is not possible to support infinity.
What do you think?
Thomas Güttler
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
Unless someone has a real-world use for the values of date.max,
datetime.max, interval.max, I find it rather counter-productive to not
store them back as infinities.
Adding infinities to the datetime module would probably be possible but
someone has to figure out the arithmetic rules. Do we need "not a time"
when adding infinity to -infinity?
Regards
Antoine.
> > PostgreSQL can store the representation of an “infinite” date, timestamp, or interval. Infinite dates are not available
> > to Python, so these objects are mapped to date.max, datetime.max, interval.max. Unfortunately the mapping cannot be
> > bidirectional so these dates will be stored back into the database with their values, such as 9999-12-31.
> Unless someone has a real-world use for the values of date.max,
> datetime.max, interval.max, I find it rather counter-productive to not
> store them back as infinities.
That would make them de-facto infinities that weirdly don't look like
infinities:
py> datetime.date.max
datetime.date(9999, 12, 31)
If I'm reading this page correctly, PostgreSQL supports a lot of dates
that Python doesn't, up to 5874897AD:
http://www.postgresql.org/docs/9.4/static/datatype-datetime.html
so it might not matter if 9999-12-31 gets turned into infinity rather
than treated as a normal date.
> Adding infinities to the datetime module would probably be possible but
> someone has to figure out the arithmetic rules. Do we need "not a time"
> when adding infinity to -infinity?
I shouldn't think so. The purpose of NANs in IEEE-754 maths is to allow
the programmer to delay dealing with the failed operation until the end
of the calculation. I don't think that date calculations tend to be
anywhere as complicated as mathematical ones, so it would be acceptable
to just raise an exception.
Besides, Postgresql doesn't have a "NotATime" value, so if we're trying
to match what they do, we don't need one either.
--
Steven
That would only make them infinities in PostgreSQL. That sounds like a
reasonable compromise for a probably little-used feature, and uncommon
values.
> > Adding infinities to the datetime module would probably be possible but
> > someone has to figure out the arithmetic rules. Do we need "not a time"
> > when adding infinity to -infinity?
>
> I shouldn't think so. The purpose of NANs in IEEE-754 maths is to allow
> the programmer to delay dealing with the failed operation until the end
> of the calculation. I don't think that date calculations tend to be
> anywhere as complicated as mathematical ones, so it would be acceptable
> to just raise an exception.
Note Numpy datetimes and timedeltas do have a concept of "NotATime".
But, yes, it's an unexpected feature.
Regards
Antoine.
Useful enough that I wrote them for my code base. And early the
postgress folks think it's a useful construct.
So +1 on adding this -- and not just hacked into the postgress adapter.
My use case is thus:
We have a model that does a computation through time. Various objects
can be active for a given time period. We use datetimes to store the
active_start and active_stop. For objects that are always active up to
a particular stop time, or become active at a particular time, and
then stay active forever, we needed a way to express -infTime and
infTime.
It probably would have mostly worked to use the min and max datetime
values, but then we lose information to pass back to the user. We also
convert to-from numpy datetimes, which have a different range.
I have a sample implementation if anyone wants to look amateur it,
though I don't think it does reflected comparisons correctly. it's
really only just good enough for our use case.
-Chris
On Mon, Jan 26, 2015, at 06:44, Steven D'Aprano wrote:
> If I'm reading this page correctly, PostgreSQL supports a lot of dates
> that Python doesn't, up to 5874897AD:
Why doesn't Python support these?
> Unless someone has a real-world use for the values of date.max,
> datetime.max, interval.max, I find it rather counter-productive to not
> store them back as infinities.
The values are useful to know exactly how far in the future one can
store date values in Python. It is important that this be discoverable
programmatically because the maximum values might change in future
versions of the ‘datetime’ library.
Because it remains true (and presumably will continue to be true) that
the maximum value is *not* infinity, it is important that the following
real-world cases are distinct:
* Query the ‘datetime’ library for the largest (furthest-in-the-future)
value that it can handle.
* Store a value explicitly meaning “infinitely far in the future” or
“infinitely far in the past” compared to any actual date or datetime
value.
I have needed both, sometimes in the same program.
An example is to be able to represent the timestamp field of a journal
entry that is still being composed and has not yet gained a timestamp.
‘None’ is not sufficient, because it must be “in the future” compared to
any actual point in time, even greater than ‘datetime.date.max’.
To use the same value attempting to represent both “maximum” and
“infinite” is a fragile, and needlessly confusing, hack. Having distinct
“infinitely far in the future” and “infinitely far in the past” values,
that are *not* themselves particular points on the timeline, would be a
valuable addition for this reason.
> Adding infinities to the datetime module would probably be possible but
> someone has to figure out the arithmetic rules. Do we need "not a time"
> when adding infinity to -infinity?
Adding dates (or datetimes) is not a valid operation today, so I don't
see why we would need to change behaviour there.
--
\ “This world in arms is not spending money alone. It is spending |
`\ the sweat of its laborers, the genius of its scientists, the |
_o__) hopes of its children.” —Dwight Eisenhower, 1953-04-16 |
Ben Finney
So what? Again, the values wouldn't change in Python. It would only be
the PostgreSQL mapping that would change.
> > Adding infinities to the datetime module would probably be possible but
> > someone has to figure out the arithmetic rules. Do we need "not a time"
> > when adding infinity to -infinity?
>
> Adding dates (or datetimes) is not a valid operation today, so I don't
> see why we would need to change behaviour there.
We're talking about both datetimes and timestamps here. Please follow.
Regards
Antoine.
> Adding dates (or datetimes) is not a valid operation today, so I don't
> see why we would need to change behaviour there.
We're talking about both datetimes and timestamps here. Please follow.
> On Wed, 28 Jan 2015 09:42:16 +1100
> Ben Finney <ben+p...@benfinney.id.au> wrote:
> > The [minimum and maximum] values are useful to know exactly how far
> > in the future [and past] one can store date values in Python. It is
> > important that this be discoverable programmatically because the
> > maximum [or minimum] values might change in future versions of the
> > ‘datetime’ library.
>
> So what? Again, the values wouldn't change in Python. It would only be
> the PostgreSQL mapping that would change.
That's the point, though. We should at least allow for “datetime.date.max”
and “datetime.date.infinitely_far_in_the_future” to be distinct, and
allow for the former to change value in a backward-compatible way.
So the maxima and minima should map to their specific points in time in
both directions between PostgreSQL and Python.
“Maximum” does not mean “infinite”, and conflating them robs us not only
of the backward-compatible ability to change the maximum in future, but
also of the ability to later add a distinct value to represent infinity.
--
\ “You don't need a book of any description to help you have some |
`\ kind of moral awareness.” —Dr. Francesca Stavrakoloulou, bible |
_o__) scholar, 2011-05-08 |
Ben Finney
On Wed, 28 Jan 2015 09:42:16 +1100
Ben Finney <ben+p...@benfinney.id.au> wrote:
So what? Again, the values wouldn't change in Python. It would only be
the PostgreSQL mapping that would change.
> > Adding infinities to the datetime module would probably be possible but
> > someone has to figure out the arithmetic rules. Do we need "not a time"
> > when adding infinity to -infinity?
On 2015-01-26 11:31, Chris Barker - NOAA Federal wrote:
> I have a sample implementation if anyone wants to look at it,
> though I don't think it does reflected comparisons correctly. it's
> really only just good enough for our use case.
I'd like to take a look. Can you send it to me, or post it somewhere?
Cool, the comparison operator logic looks very similar to mine:
https://groups.google.com/d/msg/python-ideas/G3jeWoa6h14/ELpDLFu28QcJ
I believe the ideal design is the following type hierarchy:
datetime_base
datetime_neg_inf
datetime_pos_inf
datetime
But in the interests of pragmatism, I think this one can be made to
work:
datetime
datetime_neg_inf
datetime_pos_inf
The
advantage to this is that we don't have to switch everything over to
using a new derived type--just use the normal datetime for the majority
of cases where we don't need inifinity dates.
Python 2 raises exception from __lt__,
>> Python 2 raises exception from __lt__,
>
> yeah, that's what I got -- we're on 2 for now. You'd think there wold
> be a way to do it -- but I don't see a "reflected" versions of the
> comparison operators.
Raising an exception was a bug. The comparison operators are their own reflection.
So does that mean it could be fixed in 2.* ? (or already has been?)
My test implementation works for me on Python 2.7, see http://repl.it/9Wz
> How'd you do that? Does python always know to use the derived class's
> operators when working with a base class and a derived class?
I can't find the reference, but yes.
So the broken datetime behavior would only be noticed by a non-datetime derived class.
--
~Ethan~
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/