Is a day always 86,400 epoch seconds long?

2024/11/19 17:21:37

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

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

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.

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

Answer

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.

https://en.xdnf.cn/q/26410.html

Related Q&A

Find the first instance of a nonzero number in a list in Python [duplicate]

This question already has answers here:Return the index of the first element of a list which makes a passed function true(7 answers)Closed last year.I have a list like this: myList = [0.0, 0.0, 0.0, 2.…

How to debug a Python module in Visual Studio Codes launch.json

My question may seem simple but, I have a module that I launch in a terminal like this: python -m my_module.my_fileHow do I debug this in Visual Studio Code? I have this in my launch.json (documentati…

Cleanest Fastest server setup for Django [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Quicker to os.walk or glob?

Im messing around with file lookups in python on a large hard disk. Ive been looking at os.walk and glob. I usually use os.walk as I find it much neater and seems to be quicker (for usual size direct…

Getting PyCharm to recognize python on the windows linux subsystem (bash on windows)

While running Linux versions of python, pip etc. "natively" on windows is amazing, Id like to do so using a proper IDE. Since SSHD compatibility has not been implemented yet, Im trying get Py…

Whats the difference between nan, NaN and NAN

In numpy there are nan, NaN and NAN. Whats the sense of having all three, do they differ or any of these can be used interchangeably?

Python requests: URL base in Session

When using a Session, it seems you need to provide the full URL each time, e.g.session = requests.Session() session.get(http://myserver/getstuff) session.get(http://myserver/getstuff2)This gets a littl…

size of NumPy array

Is there an equivalent to the MATLAB size() command in Numpy? In MATLAB, >>> a = zeros(2,5)0 0 0 0 00 0 0 0 0 >>> size(a)2 5In Python, >>> a = zeros((2,5)) >>> a ar…

Feature Importance Chart in neural network using Keras in Python

I am using python(3.6) anaconda (64 bit) spyder (3.1.2). I already set a neural network model using keras (2.0.6) for a regression problem(one response, 10 variables). I was wondering how can I generat…

numpy.max or max ? Which one is faster?

In python, which one is faster ? numpy.max(), numpy.min()ormax(), min()My list/array length varies from 2 to 600. Which one should I use to save some run time ?