ChronoFlare: A type safe time interval library

Following up from my previous proposal I have created a library for representing time intervals in a type-safe way, inspired by std::chrono::duration.

This allows you to write APIs that specify the resolution of time you are expecting

fn sleep_seconds(s: Seconds):
    ...
sleep_seconds(Seconds(10))
sleep_seconds(Minutes(10)) # fails to compile
sleep_seconds(Seconds(Minutes(10)) # Explicit cast

The resolution of the time interval is encoded into its type via a Ratio parameter, so users can also create custom time interval types like so by creating a alias of the underlying Time struct.

alias HalfDay = Time[Ratio[86400//2, suffix="HD"]()]

You can also override the underlying DType used to store the inner value, the default being DType.index.

alias FloatSeconds = Seconds[DType.float64]

# Will also determine datatype based on initializing value.
var fs = Seconds(1.0)

And of course you can do arithmetic with matching interval types.

var s = Seconds(10) + Seconds(30)

To be clear this is not a datetime library, just a way to represent simple time intervals in a type-safe manner.

1 Like