一天总是86,400个纪元秒吗? [英] Is a day always 86,400 epoch seconds long?

查看:99
本文介绍了一天总是86,400个纪元秒吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回顾我过去的答案时,我注意到我建议使用诸如

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 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)

当重新阅读这段代码时,我不禁感到小马托尼在我的脊椎上,轻轻地在我的耳朵和其他如此可怕,可怕的事物上轻敲 le秒。

When rereading this piece of code, I couldn't help but feel the ghastly touch of Tony the Pony on my spine, gently murmuring "leap seconds" to my ears and other such terrible, terrible things.

对于 second的时代定义,一天的长度为86,400秒的假设何时打破? (我假设使用类似Python的 time.mktime 已经返回DST调整后的值,因此上面的代码段也应该在DST转换日起作用……)

When does the "a day is 86,400 seconds long" assumption break, for epoch definitions of 'second', if ever? (I assume functions such as Python's time.mktime already return DST-adjusted values, so the above snippet should also work on DST switching days... I hope?)

推荐答案

无论何时进行日历计算,总是最好使用平台提供的任何API,例如Python的日期时间日历模块或成熟的高质量库,而不是自己编写更简单的代码。日期和日历API丑陋而复杂,但这是因为现实世界中的日历有很多奇怪的行为。

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.

例如,如果它是 10:00:00现在是 AM,然后到明天10:00:00 AM的秒数可能有所不同,具体取决于您使用的时区,DST是今晚开始还是结束,等等。

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.

每当常量86400出现在代码中时,很有可能您正在做的事情不太正确。

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.

这篇关于一天总是86,400个纪元秒吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆