9

This is always a puzzle to me- and I realise that it is not strictly an issue in programming or software development, but it seems to be a reasonable common one in our field.

For example, if I were to set an expiry datetime as 2011-04-08 00:00:00 - and given my current local time is 10:45 on the 8th already - have I already expired? Or do I still have half a day or so left?

Is there are universal standard for which end of the day midnight 'belongs' to?

Or should I take a leaf from the British military, and say that the day ends at 23:59:59 and starts at 00:00:01 and that there is no midnight?

HorusKol
  • 4,131
  • 1
  • 20
  • 25

3 Answers3

17

I think in programming (oracle and php, not sure 'bout other languages), midnight is the beginning of a day. For example if you trunc(sysdate) you will get 2011-04-08 00:00:00 which is our past.

so, when the clock changed from 23:59:59 to 00:00:00, the date actually changed from 2011-04-08 to 2011-04-09

Sufendy
  • 595
  • 4
  • 12
  • 2
    Yes, the day rolls over at midnight, which is 00:00:00. – quickly_now Apr 08 '11 at 01:32
  • This is correct AFAIK. One confusion may be that in casual speech, "midnight today" probably means midnight at the end of today, because that's more likely to be before people go to bed, than after they wake up. – Jack V. May 11 '11 at 13:42
3

An addendum to @Phelios' answer, if you have a time like 00:00:00, this is not necessarily exactly midnight. Rather, it is an infinitely large set of points in time, that all have a maximum distance of <1 second to each other. For example, all of 00:00:00.345, 00:00:00.567 and 00:00:00.842 clearly lie on the new-day-side of midnight, but would be shortened to "00:00:00" for display purposes. Likewise, 23:59:59:899 lies on the old-day-side of midnight. So midnight itself is an infinitely short time span between the days that you will never hit in practice.

T-Bull
  • 251
  • 1
  • 3
  • This is one time it's important to call out the difference between _theory_ and 'alleged' _practice_: **In practice** a user typing a date/time could barely be bothered to type the seconds; let alone milliseconds. **In practice** a timing device that determines the time of an event might not support milliseconds. **In practice** even if a timing device does support milliseconds, there's no guarantee the storage format doesn't round/truncate a portion of the time for some reason. – Disillusioned Feb 27 '17 at 09:48
1

This is a similar problem to calculating differences between angles using circular geometry, where for example, 0 degrees and 360 degrees (and 720 degrees, etc) are all the same. So what is the difference between 350 degrees and 10 degrees?

Answer: it depends on which way around you want to measure it - it might be 20 degrees or 340 degrees. But doing the calculation for angular differences is actually quite messy. Time difference calcs are similarly messy unless you convert to an internal form that makes plain addition and subtraction operate conventionally. Or you use a modern class library that magically hides it all away from you (and thus never teaches anybody about the actual details).

quickly_now
  • 14,822
  • 1
  • 35
  • 48