# ChronoFlare: A type safe time interval library

**URL:** <https://forum.modular.com/t/chronoflare-a-type-safe-time-interval-library/1155>\
**Category:** Community Showcase\
**Created:** [March 28, 2025, 4:56am UTC](https://forum.modular.com/t/chronoflare-a-type-safe-time-interval-library/1155 "2025-03-28T04:56:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgreni](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bgreni](https://forum.modular.com/u/bgreni)\
**Post date:** [March 28, 2025, 4:56am UTC](https://forum.modular.com/t/chronoflare-a-type-safe-time-interval-library/1155/1 "2025-03-28T04:56:24Z")

</div>

Following up from my previous [proposal](https://github.com/modular/max/pull/4022) I have created a library for representing time intervals in a type-safe way, inspired by [std::chrono::duration](https://en.cppreference.com/w/cpp/chrono/duration).

> **[GitHub - bgreni/ChronoFlare: A time interval library written in mojo](https://github.com/bgreni/ChronoFlare)**
>
> A time interval library written in mojo

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

```mojo
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.

```mojo
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`.

```mojo
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.

```mojo
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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [September 24, 2025, 4:56am UTC](https://forum.modular.com/t/chronoflare-a-type-safe-time-interval-library/1155/2 "2025-09-24T04:56:36Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
