views:

552

answers:

8

How to represent the start and end times for one day?

Using October 23, 2008 as an example, is it start 2008-10-23 12:00:00 AM and end 2008-10-23 11:59:59 PM?

+15  A: 

I would like to cite a site, http://www.cl.cam.ac.uk/~mgk25/iso-time.html

The international standard notation for the time of day is

hh:mm:ss

where hh is the number of complete hours that have passed since midnight (00-24), mm is the number of complete minutes that have passed since the start of the hour (00-59), and ss is the number of complete seconds since the start of the minute (00-60). If the hour value is 24, then the minute and second values must be zero.

Note: The value 60 for ss might sometimes be needed during an inserted leap second in an atomic time scale like Coordinated Universal Time (UTC). A single leap second 23:59:60 is inserted into the UTC time scale every few years as announced by the International Earth Rotation Service in Paris, to keep UTC from wandering away more than 0.9 s from the less constant astronomical time scale UT1, which is defined by the actual rotation of the earth. In practice you are not very likely to see a clock showing 23:59:60. Most synchronized clocks resynchronize again to UTC some time after a leap second has happened, or they temporarily slow down near the time of a leap seconds, to avoid any disruption that an out-of-range timestamp might otherwise cause.

An example time is

23:59:59

which represents the time one second before midnight.

As with the date notation, the separating colons can also be omitted as in

235959

and the precision can be reduced by omitting the seconds or both the seconds and minutes as in

23:59, 2359, or 23

It is also possible to add fractions of a second after a decimal dot or comma, for instance the time 5.8 ms before midnight can be written as

23:59:59.9942 or 235959.9942

As every day both starts and ends with midnight, the two notations 00:00 and 24:00 are available to distinguish the two midnights that can be associated with one date. This means that the following two notations refer to exactly the same point in time:

1995-02-04 24:00 = 1995-02-05 00:00

In case an unambiguous representation of time is required, 00:00 is usually the preferred notation for midnight and not 24:00. Digital clocks display 00:00 and not 24:00.

ISO 8601 does not specify, whether its notations specify a point in time or a time period. This means for example that ISO 8601 does not define whether 09:00 refers to the exact end of the ninth hour of the day or the period from 09:00 to 09:01 or anything else. The users of the standard must somehow agree on the exact interpretation of the time notation if this should be of any concern.

If a date and a time are displayed on the same line, then always write the date in front of the time. If a date and a time value are stored together in a single data field, then ISO 8601 suggests that they should be separated by a latin capital letter T, as in 19951231T235959.

A remark for readers from the U.S.:

The 24h time notation specified here has already been the de-facto standard all over the world in written language for decades. The only exception are a few English speaking countries, where still notations with hours between 1 and 12 and additions like “a.m.” and “p.m.” are in wide use. The common 24h international standard notation is widely used now even in England (e.g. at airports, cinemas, bus/train timetables, etc.). Most other languages do not even have abbreviations like “a.m.” and “p.m.” and the 12h notation is certainly hardly ever used on Continental Europe to write or display a time. Even in the U.S., the military and computer programmers have been using the 24h notation for a long time.

The old English 12h notation has many disadvantages like:

  • It is longer than the normal 24h notation.
  • It takes somewhat more time for humans to compare two times in 12h notation.
  • It is not clear, how 00:00, 12:00 and 24:00 are represented. Even encyclopedias and style manuals contain contradicting descriptions and a common quick fix seems to be to avoid “12:00 a.m./p.m.” altogether and write “noon”, “midnight”, or “12:01 a.m./p.m.” instead, although the word “midnight” still does not distinguish between 00:00 and 24:00 (midnight at the start or end of a given day).
  • It makes people often believe that the next day starts at the overflow from “12:59 a.m.” to “1:00 a.m.”, which is a common problem not only when people try to program the timer of VCRs shortly after midnight.
  • It is not easily comparable with a string compare operation.
  • It is not immediately clear for the unaware, whether the time between “12:00 a.m./p.m.” and “1:00 a.m./p.m.” starts at 00:00 or at 12:00, i.e. the English 12h notation is more difficult to understand.

Please consider the 12h time to be a relic from the dark ages when Roman numerals were used, the number zero had not yet been invented and analog clocks were the only known form of displaying a time. Please avoid using it today, especially in technical applications! Even in the U.S., the widely respected Chicago Manual of Style now recommends using the international standard time notation in publications.

Balint Pato
Although I do tend to agree that 24h time notation is better, the guy did ask when does the day start and end, not what format should US readers use.
Franci Penov
The poster's point is that the question, as you state it, can be best (possibly even "only") answered in 24 hour format, since, for example, "12:00am" is ambiguous.
Bobby Jack
Balint Pato
A: 

Your notation is strange. I'd suggest the standard way of specifying a full day range is:

2008-10-23 00:00:00 and finish 2008-10-23 23:59:59

David Arno
This is wrong; namely it excludes times between 23:59:59.000 and 23:59:59.999...?
Simon Johnson
@Simon, you are make a valid point that the range only works for situations where the comparison will not involve fractions of a second. However if you wish to be pedantic, I did not put 2008-10-23 23:59:59.0, so I am actually right for the whole second scenario I cited.
David Arno
Better to use a half open range, like [begin, end), where the value of 'begin' is included in the range, but the value of 'end' is not; it defines the value immediately beyond the range. The range of a day is then: [2008-10-23 00:00:00, 2008-10-24 00:00:00). No confusion.
janm
And a half open range handles leap seconds.
janm
+1  A: 

The definition of the day you mention in the question is any time that is >= 2008-10-23 00:00:00 and < 2008-10-24 00:00:00 .

Whilst you are correct, you make it unnecessarily complex as the next date must be calculated. >= 2008-10-23 00:00:00 and <= 2008-10-23 23:59:59 will work in most cases just as well and is simpler. Care must be taken with fractions of a second though,
David Arno
No, it makes it simpler, it handles leap seconds, and it is easy to see whether two intervals are contiguous.
janm
@janm, If a function exists within a particular framework that can provide a date-time structure for 00:00:00 for day+1, then Ben's case is both simpler and more reliable. day+1 is a complex function to write though compared with testing <= 23:59:59
David Arno
+2  A: 

Oct. 23 starts at 2008-10-23 12AM and finishes at 2008-10-24 12AM—a day ends at the exact same point the next one begins. The very last second begins at 11:59:59 PM but you still have a whole second before the day is over.

Mark Cidade
yes, the end is 11:59:59.999 if you have millisecond precision
Vinko Vrsalovic
.NET includes 100ns precision, which is splitting hairs. But the point stands that marxidad is correct in most cases. It is simpler when doing date range comparisons to use an end date that specifies the end boundary, not the end point. After all, the start point is also the start boundary.
spoulson
+5  A: 

Surely, if you just want to represent 1 day, you don't need to include the time at all - especially as this raises such a level of discussion about when a day starts or ends. In my experience, date handling is usually complicated enough, without introducing any extra complexity.

Today is Oct 20th 2008 - no more information is necessary.

Or am I missing something?

belugabob
This is - quite possibly - the best answer to this question; at least, pending question clarification, it is. +1
Bobby Jack
A: 

Will your date/times be from different time zones? Even if they are all from the same time zone then will the daylight saving changes cause you any problems?

Andrew Newcomb
+1  A: 

If you were to use mathematical interval notation, you would write it as

[2008-10-23 00:00:00 , 2008-10-24 00:00:00)

The [ means inclusion, and ) means up to but not including.

In this way you make it easier for users and for yourself.

Robert Paulson
A: 

It depends what you mean by a day. Date handling is, sadly, always more complex than it looks.

To convert your example into 24h ISO date format, you'd say:

2008-10-23 00:00:00 - 2008-10-23 23:59:59

However depending on whether you interpret a range as inclusive or exclusive that could omit the last second of the day*. Programmers normally prefer to keep the upper bound exclusive, so you'd go for:

2008-10-23 00:00:00 - 2008-10-24 00:00:00

Or, as a slightly nicer way of stating the same, if supported (or for human readability):

2008-10-23 00:00:00 - 2008-10-23 24:00:00

*: actually possibly more than one. If the span represents a day in the UTC timezone (or another timezone aligned to it, which is quite likely), there can occasionally be an extra 'leap second' at the end of the day, 23:59:60, in a pointless attempt to keep UTC in line with sidereal time. The next leap second is planned for the end of this year. However, many systems ignore leap seconds because they're silly and annoying.

If you want to represent a 'calendar day' independently of timezones your best bet is the Julian day number. Today is 2454760.

bobince