What is the correct interpretation at the end of the day?

Over the years, I have come across various interpretations of the end of the day. But what is the correct way to present it when comparing dates and intervals?

I found that some people prefer 23:59:59 , while others use 00:00:00 .
Here at StackOverflow, I even found some examples of questions related to using and displaying 24:00:00 , however the scope of this question is more focused on determining how to divide the two days.

In other words, we are interested in the problem of determining where exactly the day ends and where the next begins. This is apparently a common problem in many applications that require even the simplest comparisons or time slot calculations.

To clarify the above interpretations, below are three examples representing January 1 st 2014 under three different interpretations:

  • Quasi Midnight : 2014-01-01 00:00:00 - 2014-01-01 23:59:59
  • Midnight sharp : 2014-01-01 00:00:00 - 2014-01-02 00:00:00
  • Military Midnight : 2014-01-01 00:00:00 - 2014-01-01 24:00:00

While I feel that the quasi-midnight interpretation is the most intuitive, it also seems to require enough templates to correctly indicate the time. In addition, the risk of encountering red cases when the last second part of the day is not properly handled, or the risk of affecting performance when scanning huge collections of intervals for testing around these gaps.

Similarly, the military midnight interpretation also seems to require some template to tweak this split point. Without thinking too much, I cannot come up with a lot of complications using this approach.

Finally, the midnight harsh interpretation seems to be a good candidate for the most consistent. Unlike midnight, it does not require a template for setting the time and, of course, behaves with the operators < , <= , > and >= .

It is worth noting that the PHP language already interprets 2014-01-01 24:00:00 as 2014-01-02 00:00:00 , which leads to the conversion of the date into an accurate, clear interpretation of the date.

Are there any remarkable precedents in the FLOSS reference libraries or standards that justify using one interpretation over another?

+7
date date-arithmetic
source share
5 answers

First, let's recall the open and closed intervals from mathematics:

  • An open interval does not include its endpoints and is indicated by parentheses. For example, (0,1) means more than 0 and less than 1.
  • A closed interval includes its end points and is indicated by square brackets. For example, [0,1] means greater than or equal to 0 and less than or equal to 1.

I do not think that someone will not agree that the day begins with the time 00:00:00.000000... This means that any second, millisecond, microsecond, etc. Up to this point falls on the previous day.

We express this concept using left and right intervals. So:

January 1, 2014 covers the time ["2014-01-01 00:00:00", "2014-01-02 00:00:00")

That would mean the correctness of midnight. However, this will require your machine to have infinite resolution in order to present all of these really, really small time units just before 00:00:00 the next day.

Since no digital system can have infinite resolution, we are forced to give way to quasi-midnight, which will be a closed interval. The exact value of the right endpoint depends on the resolution of your system.

As MikeW said:

January 1, 2014 covers time
["2014-01-01 00:00:00", "2014-01-02 00:00:00"-system_resolution]

If the environment / language / data structure provides only one second resolution, then the last moment of the day is 23:59:59 . For a resolution of 1 millisecond, it will be 23:59:59.999 .

Thus, in essence, both quasi-midnight and midnight clarity are correct, but your final point for midnight depends on your resolution.


In practice, I don’t think you need to worry about that! Just use DateTime objects / APIs that your language provides and the corresponding conditional statements (pseudo-code):

 if DateTime.Now() < DateTime("2000-01-01 00:00:00"): // Worry about Y2K bugs 
+6
source share

00:00:00 - the beginning of a new day.

23:59:59 is the last second of the old day, but there is still a second ahead, so 23:59:59.999 is the last millisecond of the old day. Its still not the end of the day, although there is another 1 millisecond.

It depends on what you need to do. If you want to check if an event happened before midnight (for example, you query the MySQL database), the < operator will do this at the beginning of a new day, because it satisfies any level of detail (nanoseconds, microseconds, whatever).

If you want to check if something happened on a new day, it should be >= beginning of a new day, i.e. 00:00:00

+6
source share

Well, "What is the right way to present it?" This is a subjective question, so it is impossible to answer. However, most “clock” applications and system clocks present it as “Quasi Midnight” above, so you should probably go for it if you need consistency in presenting time to users.

But if you don’t show the time directly to users and instead use it as part of some other calculation, it most likely depends on the application.

This page and this page provide quite a few examples of how time is problematic and how subjective “correctness” can get depending on your application.

+2
source share

IMO, it basically comes down to * what requirements do you really have for the problem and * temporary resolution in tools / languages ​​at your disposal.

After all, it is very easy to overestimate any problem in abstract terms.

Having said that, I personally would prefer strictly less (<) 00:00:00

+2
source share

The datetime data type has traditionally been represented as a floating point number. The whole number number is the number of days from a given era, and the mantissa or decimal part makes up the time offset. There are many different eras and many ways to store dates. Basically, I watched the beginning of the day as 01/01/2001 12:00:00 AM and the end of the day, viewed as 01/01/2001 11:59:59 PM.

A good way to think about this would be to subtract one second from midnight at 11:59:59 of the previous day, and then adding the second back again will be midnight again.

The value for the midnight of a certain day will be stored in the same way using the above era. Zulu's 12-hour 24-hour semantics are the just selected options for displaying Date Time values.

+1
source share

All Articles