There's always something new to learn about managing time in software. I've been working a lot with timestamps recently, and wrote up some surprising-to-me things from that work.

#Python

mostlypython.com/time-deltas-a

@ehmatthes FYI there is a much more elegant way to find out how many of a given time unit there is in a timedelta, which is to divide it by another timedelta, like so:

```
>>> timedelta(days=3) / timedelta(hours=1)
72
```

In fact all the talk about normalization feels a bit misleading. The root of your original bug isn't really normalization so much as the fact that you are accessing a component when what you wanted was a view onto the entire duration.

Follow

@ehmatthes It would be nice if this ends up implemented in ruff, since the majority of the time accessing one component of a timedelta is not something you want to do anyway: github.com/astral-sh/ruff/issu

@pganssle That was an interesting read! It's nice to know how not alone I was in misusing `.seconds`!

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.