view src/metrics/time.rs @ 38:0227aba85c56

Use time crate for clock metric It's more reliable for the local time.
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 23 Mar 2016 21:20:58 +0100
parents fc3487a89d63
children f4652b525ec7
line wrap: on
line source

//! A simple clock.

use framework::*;

extern crate time;
//use self::time;

struct TimeMetric {
    fmt: String,
}

const DEFAULT_FMT: &'static str = "%a %b %d %H:%M:%S %Y (%Z)";

impl Metric for TimeMetric {
    fn init(&mut self, _: &mut MetricState, arg: Option<String>) {
        self.fmt = arg.unwrap_or(String::from(DEFAULT_FMT));
    }
    fn render(&mut self, _: &mut MetricState) -> RenderResult {
        let t = time::now();
        let tstr = format!("{}", t.strftime(&self.fmt).unwrap());

        RenderResult::new(tstr, Color::Default)
    }
}

pub fn clock_metric() -> Box<Metric> {
    Box::new(TimeMetric { fmt: String::new() })
}