kumo_chrono_helper/
lib.rs

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