date.datetime() method to convert from date to datetime

There is a datetime.date() method for converting from a datetime to a date, but no stdlib method to do the opposite conversion. Could a date.datetime() method be added that returns a datetime with the time component set to zero? Alternatively, if the object is already a datetime, it could simply return itself. This isn't exactly hard to do in user code, but the obvious way of doing it is a bit too verbose for such a simple operation. Less verbose ways are not obvious or are error-prone, as this StackOverflow question shows: http://stackoverflow.com/questions/1937622/convert-date-to-datetime-in-pytho... -- Evan Martin

Yes, zero means the midnight of the date. No timezone - it's a naive datetime, same as the original date. The method would return exactly the same result as datetime.combine(date, time()) -- Evan Martin

Evan Martin <python-dev@realityexists.net> writes:
What is “zero” for a time-of-day? Do you mean “midnight on that day”? In what timezone? -- \ “Faith, n. Belief without evidence in what is told by one who | `\ speaks without knowledge, of things without parallel.” —Ambrose | _o__) Bierce, _The Devil's Dictionary_, 1906 | Ben Finney

On Fri, Jun 3, 2011 at 12:07 AM, Evan Martin <python-dev@realityexists.net> wrote:
My preferred alternative to this idea would be to allow datetime constructor to take date (or datetime). This would make date/datetime behave similar to int/float. If this is done, I would also like the single-argument constructor to accept str in ISO format.

Alexander Belopolsky <alexander.belopolsky@gmail.com> writes:
+1. They are conceptually very similar types, so it makes sense for the constructor to accept each of them.
If this is done, I would also like the single-argument constructor to accept str in ISO format.
−1, please don't overload a type's default constructor to the point of accepting all sorts of unrelated types. I think ‘datetime.fromstring’, if implemented, should be a separate alternative constructor (by whatever spelling). -- \ “I knew it was a shocking thing to say, but … no-one has the | `\ right to spend their life without being offended.” —Philip | _o__) Pullman, 2010-03-28 | Ben Finney

On 4/06/2011 10:08 AM, Ben Finney wrote:
I think that would work well if we had overloaded constructors, but without them it might complicate things too much. What would the signature look like? Also, that would be inconsistent with how a datetime is converted to a date - as a user I would then expect the date constructor to take a datetime, too. If datetime.date() converts a datetime to a date then I naturally think to call date.datetime() to go the other way. -- Evan Martin

Yes, zero means the midnight of the date. No timezone - it's a naive datetime, same as the original date. The method would return exactly the same result as datetime.combine(date, time()) -- Evan Martin

Evan Martin <python-dev@realityexists.net> writes:
What is “zero” for a time-of-day? Do you mean “midnight on that day”? In what timezone? -- \ “Faith, n. Belief without evidence in what is told by one who | `\ speaks without knowledge, of things without parallel.” —Ambrose | _o__) Bierce, _The Devil's Dictionary_, 1906 | Ben Finney

On Fri, Jun 3, 2011 at 12:07 AM, Evan Martin <python-dev@realityexists.net> wrote:
My preferred alternative to this idea would be to allow datetime constructor to take date (or datetime). This would make date/datetime behave similar to int/float. If this is done, I would also like the single-argument constructor to accept str in ISO format.

Alexander Belopolsky <alexander.belopolsky@gmail.com> writes:
+1. They are conceptually very similar types, so it makes sense for the constructor to accept each of them.
If this is done, I would also like the single-argument constructor to accept str in ISO format.
−1, please don't overload a type's default constructor to the point of accepting all sorts of unrelated types. I think ‘datetime.fromstring’, if implemented, should be a separate alternative constructor (by whatever spelling). -- \ “I knew it was a shocking thing to say, but … no-one has the | `\ right to spend their life without being offended.” —Philip | _o__) Pullman, 2010-03-28 | Ben Finney

On 4/06/2011 10:08 AM, Ben Finney wrote:
I think that would work well if we had overloaded constructors, but without them it might complicate things too much. What would the signature look like? Also, that would be inconsistent with how a datetime is converted to a date - as a user I would then expect the date constructor to take a datetime, too. If datetime.date() converts a datetime to a date then I naturally think to call date.datetime() to go the other way. -- Evan Martin
participants (4)
-
Alexander Belopolsky
-
Ben Finney
-
Evan Martin
-
Nick Coghlan