kumo_chrono_helper/
lib.rs

1pub use chrono;
2pub use chrono::{DateTime, TimeZone, Utc};
3
4// chrono has a slightly awkward API that returns Option<T> in
5// a few cases; for small constant values it makes the call site
6// more complex, especially because the internally panicking variants
7// of the constructor methods are now deprecated, forcing the call
8// site to translate to an error inline or otherwise expect()
9// the error away.
10// This little crate exposes a few constants for infallible cases
11// and wrapper functions that explain the error condition for the others.
12
13#[allow(deprecated)]
14pub const MINUTE: chrono::Duration = chrono::Duration::minutes(1);
15
16#[allow(deprecated)]
17pub const SECOND: chrono::Duration = chrono::Duration::seconds(1);
18
19#[allow(deprecated)]
20pub const HOUR: chrono::Duration = chrono::Duration::hours(1);
21
22pub fn seconds(seconds: i64) -> anyhow::Result<chrono::Duration> {
23    chrono::Duration::try_seconds(seconds).ok_or_else(|| {
24        anyhow::anyhow!("{seconds} is out of range for chrono::Duration::try_seconds")
25    })
26}
27
28pub fn minutes(minutes: i64) -> anyhow::Result<chrono::Duration> {
29    chrono::Duration::try_minutes(minutes).ok_or_else(|| {
30        anyhow::anyhow!("{minutes} is out of range for chrono::Duration::try_minutes")
31    })
32}
33
34pub fn days(days: i64) -> anyhow::Result<chrono::Duration> {
35    chrono::Duration::try_days(days)
36        .ok_or_else(|| anyhow::anyhow!("{days} is out of range for chrono::Duration::try_days"))
37}