Can I just completely stop using datetime.date?

In Python, I got the impression that datetime.date is basically just a subset of datetime.datetime , with fewer functions and slightly less overhead. I would never use datetime.date again for the following reasons:

  • No more conversions between the two types!

  • datetime.date objects clockwise are always unclear, for obvious reasons. This makes it difficult to generalize timezone processing throughout the application if you sometimes use datetime.date and sometimes use datetime.datetime

  • Several times I came across headaches, accidentally comparing datetime.date and datetime.datetime objects. Using only one type simplifies the comparison.

  • Formatting the differences between datetime.date and datetime.datetime should only be a formatting issue. Conducting a difference in the lower class leads to unnecessary complexity of the language.

  • If I am on the right track about this, then datetime.date will eventually be deprecated in future releases of Python, much like unicode and long . The adoption of this agreement now puts me ahead of the curve.

It seems to me that the only convincing argument in favor of using datetime.date is the small amount of extra memory and memory involved in datetime.datetime . Let me say that I do not care about these additional overheads. Are there any other compelling reasons to continue using this class? I do not want to switch to this agreement, and then regret it later, because I missed something important.

+7
python date datetime
source share
2 answers

They represent different things.

A datetime is a specific point in time.

A date - time interval. It's not 00:00:00 that day, it's all day.

That is why you cannot directly convert between them. And why can't you use datetime as a replacement for date .

(The fact that the same type of timedelta used for both is probably a flaw in the module, which has been discussed several times, but I doubt it will ever be fixed.)

+12
source share

This is the exact ratio:

 >>> import datetime >>> issubclass(datetime.datetime, datetime.date) True 

If you do not want to use date , just do not do this. There are many applications in which “a small amount of extra memory and memory” for datetime is a significant burden, especially when the massive amount of date needs to be stored in the database as brines (for example, in ZODB in the Zope installation - and it is no coincidence that Zope Corp paid the then-PythonLabs team the development and implementation of the datetime module to get started ;-)).

EDIT - what date means

I want to push it a bit on @abamert "A date is a time interval. It's not 00:00:00 on that day, it's all day." Of course, you can think of it this way, but there were no such intentions. I wrote almost the entire original datetime module (both Python and C versions), working with the main module developers: Guido van Rossum and Jim Fulton.

Guido was very interested in the concepts of "naive" dates and times, the everyday "common sense" concepts of dates and times that simply ignore all the tedious subtleties that geeks like to possess ;-) (for example, a historical time zone - and even calendar adjustments and "jumping seconds "which cannot be used by anyone other than astronomers). For him, the best way to find out what the date meant is to ask a 12-year-old. And their answer does not matter. If they think that date(2014, 1, 9) means "all day," fine. If they like to think of it as black midnight at the beginning of the day, that’s fine too. Exactly midnight at the end of the day or at noon, or during sleep at 3 o’clock, or at different times on different days, or no time at all ... these differences are all in your head when it comes to date . date does not encourage or discourage any of them.

Now the geek may object, "but if I think of date(2014, 1, 9) as noon, and you think of date(2014, 1, 10) as 3pm, subtracting them returns the wrong answer!". Yon. Ask a 12-year-old: it’s obvious that for naive dates the difference is exactly 1 (naive) day.

There is nothing to indicate that any particular person or application "should" find this useful. Many applications find this useful. If yours is not one of them, do not use it.

All in all, the only serious regret I have about datetime is that tzinfo objects tzinfo not have a reliable way to distinguish between "standard" and "daytime" times during ambiguous transition hours. Guido's thrift was offended (in my opinion) that C struct tm "wasted" the whole int ( tm_isdst ) for this 1 bit of information, which is necessary only to eliminate the ambiguity of the transition time. It is no coincidence that most geographic areas that switch between “daylight” and “standard” time make these transitions when most people are asleep, so for almost all practical purposes the ambiguities are invisible (“what do you mean, 2:10 am? "that the first time the clock reads 2:10 am, or the second time after we set the clock back from 3 am to 2 am?"). But for those few who really care, living without this bit remains royal pain: - (

+9
source share

All Articles