korken89 / fugit

`fugit` provides a comprehensive library of `Duration` and `Instant` for the handling of time in embedded systems, doing all it can at compile time.
Apache License 2.0
55 stars 16 forks source link

Add Duration::to_nanos, to_micros, to_millis, to_secs, to_minutes, to_hours methods #22

Closed adamgreig closed 2 years ago

adamgreig commented 2 years ago

I was worried this might lead to overflows when doing something like

Duration::<u32, 1, 1_000_000_000>::from_ticks(1_000_000_000).to_nanos()

because it's working out

(1_000_000_000u32 * 1_000_000_000u32)/1_000_000_000u32

but in practice it seems to do the right thing (and I cover this case with tests).

In any event the maths is exactly the inverse of the existing micros() etc methods, so I guess it should work or fail in the same ways.

korken89 commented 2 years ago

Yeah, I designed it so the helpers work in u64 and at compile time find the GCD of the conversion, so most often only a small division or multiplication is used.

Though if one tries to work with too large dynamic range it will eventually fail. Most of the code handles overflows, but for example the shorthands do not so far.

bors[bot] commented 2 years ago

Build succeeded: