bento-kit 0.1.1

A bento box of common Rust utilities: id generation, timing, masking
Documentation
#![cfg(feature = "time")]

use bento_kit::time::{format_now_utc, now_millis, now_seconds, TimeUse};
use std::thread::sleep;
use std::time::Duration;

#[test]
fn timeuse_measures_real_elapsed_time() {
    // No upper bound: a sleep on a busy CI runner can stretch arbitrarily.
    let t = TimeUse::new();
    sleep(Duration::from_millis(50));
    let elapsed = t.elapsed(None).as_millis();
    assert!(elapsed >= 50, "elapsed was {elapsed}ms, expected >= 50");
}

#[test]
fn timeuse_multi_stage_profiling() {
    let mut t = TimeUse::new();
    t.start(Some("connect"));
    sleep(Duration::from_millis(20));
    let connect = t.stop(Some("connect"));

    t.start(Some("query"));
    sleep(Duration::from_millis(10));
    let query = t.stop(Some("query"));

    let total = t.stop(None);
    assert!(connect.as_millis() >= 20);
    assert!(query.as_millis() >= 10);
    assert!(total.as_millis() >= 30, "total = {}ms", total.as_millis());
}

#[test]
fn now_millis_and_seconds_agree() {
    let ms = now_millis();
    let s = now_seconds();
    let derived = ms / 1000;
    assert!((derived - s).abs() <= 1);
}

#[test]
fn iso_8601_format_round_trips_to_chrono() {
    let s = format_now_utc("%Y-%m-%dT%H:%M:%S%.3fZ");
    let parsed = chrono::DateTime::parse_from_rfc3339(&s.replace('Z', "+00:00"));
    assert!(parsed.is_ok(), "could not parse our own output: {s}");
}