time.rs 864 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. extern crate time;
  2. use super::Sensor;
  3. pub struct TimeSensor {
  4. format: String,
  5. is_utc: bool,
  6. time: String
  7. }
  8. impl TimeSensor {
  9. pub fn new(format: &str, is_utc: bool) -> TimeSensor {
  10. TimeSensor {
  11. format: format.to_string(),
  12. is_utc: is_utc,
  13. time: "".to_string()
  14. }
  15. }
  16. }
  17. impl Sensor for TimeSensor {
  18. fn icon(&self) -> String {
  19. if self.is_utc {
  20. "UTC"
  21. }
  22. else {
  23. ""
  24. }.to_string()
  25. }
  26. fn status(&self) -> String {
  27. self.time.clone()
  28. }
  29. fn process(&mut self) {
  30. let now =
  31. if self.is_utc {
  32. time::now_utc()
  33. }
  34. else {
  35. time::now()
  36. };
  37. let time = time::strftime(&self.format, &now);
  38. self.time = time.unwrap()
  39. }
  40. }