Is a day always 86,400 epoch seconds long?

前端 未结 4 749
一整个雨季
一整个雨季 2021-01-01 08:35

While reviewing my past answers, I noticed I\'d proposed code such as this:

import time

def dates_between(start, end):
  # muck around between the 9k+ time          


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 09:15

    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.

提交回复
热议问题