ariebovenberg / whenever

⏰ Sensible and typesafe datetimes for Python
https://whenever.rtfd.io
MIT License
362 stars 7 forks source link

can UTCDateTime be replaced with Instant? #64

Open bxparks opened 4 months ago

bxparks commented 4 months ago

(Spun off from #59)

Ah, so you are using UTCDateTime as a substitute for Instant

The whole discussion on whether to have Instant or not probably belongs in its own issue. The fact that most modern libraries have this, is a strong signal for its usefulness. Having both UTCDateTime and Instant would probably be unnecessary

I can see some value in UTCDateTime, because it captures an intent of the developer, and it's more ergonomic than OffsetDateTime(offset=0).

Can UTCDateTime be replaced with an Instant class? The Instant class is a wrapper around an "epoch seconds". It provides type-safety and encapsulation. There should be an unambiguous 1-to-1 conversion between an Instant and UTCDateTime.

Here are some examples from other libraries:

I don't know, I can see both Instant and UTCDateTime being useful, because they capture slightly different things, and the ergonomics for the end-users are slightly different.

bxparks commented 4 months ago

It occurs to me that the Instant class would replace the float type returned by the timestamp() function mentioned in #63 .

ariebovenberg commented 3 months ago

My thoughts after some time:

Instant and UTCDateTime are very similar. In the end, if you normalize to UTC, you might as well be dealing with a timestamp/integer: you're usually not interested in the nominal month, day, hour in UTC. You're more likely interested in comparison or adding/subtracting

what I'm tending towards: keep UTCDateTime, but optimize it for comparison and shifting. It would probably be a timestamp under the hood.

ariebovenberg commented 1 month ago

An update to add here: In the rust extension I'm working on, UTCDateTime is Instant in almost everything but name.

Note that I'm still hanging on to the UTCDateTime name for these reasons:


It occurs to me that the Instant class would replace the float type returned by the timestamp() function mentioned in https://github.com/ariebovenberg/whenever/issues/63 .

Not necessarily... Instant can be opaque while the timestamp must be a an integer with a specific value 🤔

ariebovenberg commented 3 days ago

I've come around to Instant now:

The next release will replace UTCDateTime with Instant