12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- extern crate time;
- use super::Sensor;
- pub struct TimeSensor {
- format: String,
- is_utc: bool,
- time: String
- }
- impl TimeSensor {
- pub fn new(format: &str, is_utc: bool) -> TimeSensor {
- TimeSensor {
- format: format.to_string(),
- is_utc: is_utc,
- time: "".to_string()
- }
- }
- }
- impl Sensor for TimeSensor {
- fn icon(&self) -> String {
- if self.is_utc {
- "UTC"
- }
- else {
- ""
- }.to_string()
- }
- fn status(&self) -> String {
- self.time.clone()
- }
- fn process(&mut self) {
- let now =
- if self.is_utc {
- time::now_utc()
- }
- else {
- time::now()
- };
- let time = time::strftime(&self.format, &now);
- self.time = time.unwrap()
- }
- }
|