14

Did anybody notice that the interval of second in Python datetime is [00,61] see the table in the bottom of this page. http://docs.python.org/library/datetime.html#strftime-strptime-behavior

Why?

Eric O Lebigot
  • 81,422
  • 40
  • 198
  • 249
storm
  • 327
  • 1
  • 4
  • 10
  • Day of year also goes from [1, 366] in order to accomodate leap days. – plaes Feb 16 '12 at 06:56
  • -1 because +6 for a question answered in the link it contains is ridiculous. – Wooble Feb 16 '12 at 11:25
  • 1
    +1 because a question that points out an interesting fact that I did not know about is useful. – Eric O Lebigot Feb 17 '12 at 09:03
  • @storm: Please don't forget to mark one answer as accepted, if it indeed answered your question. This will even earn you reputation. :) – Eric O Lebigot Feb 17 '12 at 09:03
  • 4
    @storm, you've been here recently enough, you should *definitely* mark some questions as accepted when they answer your question. 0% acceptance is just silly. – Wayne Werner May 01 '12 at 20:34
  • Is the link in the description wrong or has this information been moved around in the Python docs? I can only find a reference to 62-second minutes here: https://docs.python.org/3/library/time.html#time.strftime – Feuermurmel Jun 21 '17 at 15:31
  • "_-1 because +6 for a question answered in the link it contains is ridiculous_" The question is not actually answered by the link. Leap seconds are noted, but that only explains _half_ of the question. – Thanatos Feb 20 '19 at 19:03

5 Answers5

20

The answer is a little further down in the page:

The range really is 0 to 61; according to the Posix standard this accounts for leap seconds and the (very rare) double leap seconds. The time module may produce and does accept leap seconds since it is based on the Posix standard, but the datetime module does not accept leap seconds in strptime() input nor will it produce them in strftime() output.

This is an interesting behavior, indeed.

Eric O Lebigot
  • 81,422
  • 40
  • 198
  • 249
  • hmm,I too exiting to ignore the explanation. – storm Feb 16 '12 at 06:07
  • 3
    I just realized that Python doesn't have a database of leap seconds (akin to tzinfo for timezones) and so doesn't have a way to validate if a certain leap second is legit... this means that conversion to Unix time is lossy: `t1=time.strptime("30 Jun 1997 23:59:60", "%d %b %Y %H:%M:%S"); t2=time.strptime("01 Jul 1997 00:00:00", "%d %b %Y %H:%M:%S")`, then `t1 == t2` yields `False` as expected, but `calendar.timegm(t1) == calendar.timegm(t2)` yields `True` – berdario Jan 20 '14 at 01:07
  • @berdario: your example is (accidently?) correct. There is [a leap second on 30 Jun 1997](http://tf.nist.gov/pubs/bulletin/leapsecond.htm). And there is always 86400 seconds in a POSIX day therefore `calendar.timegm()` returns correct results ([the same POSIX timestamp occurs twice in this case](http://en.wikipedia.org/wiki/Unix_time#Encoding_time_as_a_number)). – jfs Feb 23 '14 at 07:51
  • 3
    The tz database (available via `pytz` Python package) has a list of leap seconds. [You could use it, to check whether given time is a leap second.](http://stackoverflow.com/q/19332902/4279) – jfs Sep 06 '14 at 00:26
  • Note for further readers: there has never been a double leap second, and never will be. Apparently Posix standardized this incorrectly, and we're left with this abomination. – Max Jul 10 '18 at 21:41
4

There is no such thing as a double leap second. There cannot be 62 seconds in a minute. 59, yes. 60, yes. 61, yes. 62, no.

http://www.monkey.org/openbsd/archive2/tech/199905/msg00031.html

Greg Hennessy
  • 349
  • 2
  • 4
  • 14
  • Well, can you give a single case of "inverse leap second", e.g. when was the minute, having 59 seconds instead of 60? – toriningen May 22 '14 at 19:19
  • 2
    There have been no examples of a 59 second minute, however in theory there could be. There cannot be a "double leap second" though. – Greg Hennessy Jul 08 '14 at 19:22
  • Double leap seconds are permitted, there just haven't been any to date. So a minute could have 62 seconds -- it's just that such has yet to occur. – Chris Charabaruk May 30 '15 at 15:43
  • 2
    Chris, the mythical "double leap seconds" cannot happen due to the definitions of the ITU-R. The specifications are hard to get, and someone misread them. Under the current systems there will never be a double leap second, and the odds of there ever being a 59 second minute is very small. – Greg Hennessy May 31 '15 at 16:25
  • 1
    The link in this answer is dead, but there is [this](https://www.ucolick.org/~sla/leapsecs/onlinebib.html), which seems to have good coverage of the relevant minutiae. – John Y Oct 18 '16 at 20:08
  • FYI I believe this is a mirror of the original (dead) link: https://marc.info/?l=openbsd-tech&m=92682843416159&w=2 – Conrad Meyer Jun 01 '18 at 21:02
2

Probably to account for leap seconds.

cHao
  • 78,897
  • 19
  • 136
  • 168
0

When you have to add leap second it will be helpful to calculate that. You can search on net for leap second. Due to that second range in python is 0-61.

Nilesh
  • 17,950
  • 11
  • 74
  • 119
-7

Leap seconds.

It has been the case that there have been 62 seconds in a minute in the past.

It adjusts for the world spinning slower.

Part of this is down to tides. The energy for the tides comes from the rotation of the earth and moon. The result is that the world slows down.

If global warming takes place the oceans get hotter and expand. That is like a skater throwing their arms out, and the spin slows down. That hasn't taken place. The measurement of ocean levels doesn't agree with the rotation measurements. It's likely to be down to problems with the earth's surface moving, which is far larger than the sea level rise.

Nickle
  • 367
  • 3
  • 4