44

過去の回答を確認しているときに、次のようなコードを提案したことに気付きまし

import time

def dates_between(start, end):
  # muck around between the 9k+ time representation systems in Python
  # now start and end are seconds since epoch

  # return [start, start + 86400, start + 86400*2, ...]
  return range(start, end + 1, 86400)

このコードを読み直すと、背骨にトニー・ザ・ポニーがひどく触れ、耳に「うるう秒」などのひどいものがそっとつぶやくのを感じずにはいられませんでした。

「秒」のエポック定義について、「1日は86,400秒の長さ」の仮定が破られるのはいつですか。(Pythonなどの関数はtime.mktimeすでにDSTで調整された値を返すと想定しているので、上記のスニペットはDSTの切り替え日でも機能するはずです...私は願っていますか?)

4

4 に答える 4

21

Whenever doing calendrical calculations, it is almost always better to use whatever API the platform provides, such as Python's datetime and calendar modules, or a mature high-quality library, than it is to write "simpler" code yourself. Date and calendar APIs are ugly and complicated, but that's because real-world calendars have a lot of weird behavior.

For example, if it is "10:00:00 AM" right now, then the number of seconds to "10:00:00 AM tomorrow" could be a few different things, depending on what timezone(s) you are using, whether DST is starting or ending tonight, and so on.

Any time the constant 86400 appears in your code, there is a good chance you're doing something that's not quite right.

And things get even more complicated when you need to determine the number of seconds in a week, a month, a year, a quarter, and so on. Learn to use those calendar libraries.

于 2013-07-08T11:31:38.657 に答える
14

According to Wikipedia,

UTC days are almost always 86 400 s long, but due to "leap seconds" are occasionally 86 401 s and could be 86 399 s long (though the latter option has never been used as of December 2010); this keeps the days synchronized with the rotation of the Earth (or Universal Time).

I expect that a double leap second could in fact make the day 86402s long, if that were to ever be used.

EDIT again: second guessed myself due to confusing python documentation. time.mktime always returns UTC epoch seconds. There done. : )

于 2011-09-26T08:00:29.273 に答える
12

Number of seconds in a day depends on time system that you use e.g., in POSIX, a day is exactly 86400 seconds by definition:

As represented in seconds since the Epoch, each and every day shall be accounted for by exactly 86400 seconds.

In UTC, there could be a leap second included i.e., a day can be 86401 SI seconds (and theoretically 86399 SI seconds). As of Jun 30 2015, it has happened 26 times.

If we measure days by apparent motion of the Sun then the length of a (solar) day varies through the year by ~16 minutes from the mean.

In turn it is different from UT1 that is also based on rotation of the Earth (mean solar time). An apparent solar day can be 20 seconds shorter or 30 seconds longer than a mean solar day. UTC is kept within 0.9 seconds of UT1 by the introduction of occasional intercalary leap seconds.

If you define a day by local clock then it may be very chaotic due to bizarre political timezone changes. It is not correct to assume that a day may change only by an hour due to DST.

于 2013-12-02T18:35:40.053 に答える
7

In all time zones that "support" daylight savings time, you'll get two days a year that don't have 24h. They'll have 25h or 23h respectively. And don't even think of hardcoding those dates. They change every year, and between time zones.

Oh, and here's a list of 34 other reasons that you hadn't thought about, and why you shouldn't do what you're doing.

于 2013-11-20T13:39:53.197 に答える