Metric Clock

Published 400d ago

 ayebear

Prints the current time in a metric time format (100,000 seconds per day)

use time::OffsetDateTime;

fn main() {
    let (y, d, h, m, s) = metric_time();
    println!("{}-{} {:01}:{:02}:{:06.03}", y, d, h, m, s);
}

fn metric_time() -> (i32, i32, f64, f64, f64) {
    // need "local-offset" feature enabled for "time" crate
    //let local = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
    let local = OffsetDateTime::now_utc();
    let (h, m, s, micro) = local.to_hms_micro();
    let (h, m, s) = (h as f64, m as f64, s as f64 + micro as f64 / 1_000_000.);
    let total_metric_s = ((h * 3600. + m * 60. + s) / 86400.) * 100_000.;
    (
        local.year(),
        local.ordinal().into(),
        (total_metric_s / 10_000.).floor(),
        (total_metric_s % 10_000. / 100.).floor(),
        total_metric_s % 100.,
    )
}

3

Please login or sign up to comment and collaborate