• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 2023 Google, Inc.
3 //
4 //  Licensed under the Apache License, Version 2.0 (the "License");
5 //  you may not use this file except in compliance with the License.
6 //  You may obtain a copy of the License at:
7 //
8 //  http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //  Unless required by applicable law or agreed to in writing, software
11 //  distributed under the License is distributed on an "AS IS" BASIS,
12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //  See the License for the specific language governing permissions and
14 //  limitations under the License.
15 
16 //! # Time Display class
17 
18 use chrono::{DateTime, Datelike, NaiveDateTime, Timelike, Utc};
19 
20 /// A simple class that contains information required to display time
21 pub struct TimeDisplay {
22     /// seconds since std::time::UNIX_EPOCH
23     secs: i64,
24     /// nano sub seconds since std::time::UNIX_EPOCH
25     nsecs: u32,
26 }
27 
28 impl TimeDisplay {
29     /// Creates a new TimeDisplay with given secs and nsecs
30     ///
31     /// # Arguments
32     ///
33     /// * `secs` - seconds since std::time::UNIX_EPOCH
34     /// * `nsecs` - nano sub seconds since std::time::UNIX_EPOCH
new(secs: i64, nsecs: u32) -> TimeDisplay35     pub fn new(secs: i64, nsecs: u32) -> TimeDisplay {
36         TimeDisplay { secs, nsecs }
37     }
38 
39     /// Displayes time in UTC with a format YYYY-MM-DD-HH:MM:SS
40     ///
41     /// # Returns
42     ///
43     /// `Ok(String)` if the display was successful, `Error` otherwise.
utc_display(&self) -> String44     pub fn utc_display(&self) -> String {
45         if let Some(datetime) = NaiveDateTime::from_timestamp_opt(self.secs, self.nsecs) {
46             let current_datetime = DateTime::<Utc>::from_utc(datetime, Utc);
47             return format!(
48                 "{}-{:02}-{:02}-{:02}-{:02}-{:02}",
49                 current_datetime.year(),
50                 current_datetime.month(),
51                 current_datetime.day(),
52                 current_datetime.hour(),
53                 current_datetime.minute(),
54                 current_datetime.second()
55             );
56         }
57         "INVALID-TIMESTAMP".to_string()
58     }
59 }
60 
61 #[cfg(test)]
62 mod tests {
63 
64     use super::TimeDisplay;
65 
66     #[test]
test_utc_display_ok()67     fn test_utc_display_ok() {
68         let epoch_time = TimeDisplay::new(0, 0);
69         let utc_epoch = epoch_time.utc_display();
70         assert_eq!(utc_epoch, "1970-01-01-00-00-00");
71         let twok_time = TimeDisplay::new(946684900, 0);
72         let utc_twok = twok_time.utc_display();
73         assert_eq!(utc_twok, "2000-01-01-00-01-40");
74     }
75 
76     #[test]
test_utc_display_err()77     fn test_utc_display_err() {
78         let max_seconds = TimeDisplay::new(i64::MAX, 0);
79         assert_eq!("INVALID-TIMESTAMP", max_seconds.utc_display());
80         let max_nanos = TimeDisplay::new(0, 2_000_000_000);
81         assert_eq!("INVALID-TIMESTAMP", max_nanos.utc_display());
82     }
83 }
84