Skip to main content

torrust_clock/conv/
mod.rs

1use std::str::FromStr;
2
3use chrono::{DateTime, Utc};
4
5use crate::DurationSinceUnixEpoch;
6
7/// It converts a string in ISO 8601 format to a timestamp.
8///
9/// For example, the string `1970-01-01T00:00:00.000Z` which is the Unix Epoch
10/// will be converted to a timestamp of 0: `DurationSinceUnixEpoch::ZERO`.
11///
12/// # Panics
13///
14/// Will panic if the input time cannot be converted to `DateTime::<Utc>`, internally using the `i64` type.
15/// (this will naturally happen in 292.5 billion years)
16#[must_use]
17pub fn convert_from_iso_8601_to_timestamp(iso_8601: &str) -> DurationSinceUnixEpoch {
18    convert_from_datetime_utc_to_timestamp(&DateTime::<Utc>::from_str(iso_8601).unwrap())
19}
20
21/// It converts a `DateTime::<Utc>` to a timestamp.
22/// For example, the `DateTime::<Utc>` of the Unix Epoch will be converted to a
23/// timestamp of 0: `DurationSinceUnixEpoch::ZERO`.
24///
25/// # Panics
26///
27/// Will panic if the input time overflows the `u64` type.
28/// (this will naturally happen in 584.9 billion years)
29#[must_use]
30pub fn convert_from_datetime_utc_to_timestamp(datetime_utc: &DateTime<Utc>) -> DurationSinceUnixEpoch {
31    DurationSinceUnixEpoch::from_secs(u64::try_from(datetime_utc.timestamp()).expect("Overflow of u64 seconds, very future!"))
32}
33
34/// It converts a timestamp to a `DateTime::<Utc>`.
35/// For example, the timestamp of 0: `DurationSinceUnixEpoch::ZERO` will be
36/// converted to the `DateTime::<Utc>` of the Unix Epoch.
37///
38/// # Panics
39///
40/// Will panic if the input time overflows the `u64` seconds overflows the `i64` type.
41/// (this will naturally happen in 292.5 billion years)
42#[must_use]
43pub fn convert_from_timestamp_to_datetime_utc(duration: DurationSinceUnixEpoch) -> DateTime<Utc> {
44    DateTime::from_timestamp(
45        i64::try_from(duration.as_secs()).expect("Overflow of i64 seconds, very future!"),
46        duration.subsec_nanos(),
47    )
48    .unwrap()
49}
50
51#[cfg(test)]
52mod tests {
53    use chrono::DateTime;
54
55    use crate::DurationSinceUnixEpoch;
56    use crate::conv::{
57        convert_from_datetime_utc_to_timestamp, convert_from_iso_8601_to_timestamp, convert_from_timestamp_to_datetime_utc,
58    };
59
60    #[test]
61    fn should_be_converted_to_datetime_utc() {
62        let timestamp = DurationSinceUnixEpoch::ZERO;
63        assert_eq!(
64            convert_from_timestamp_to_datetime_utc(timestamp),
65            DateTime::from_timestamp(0, 0).unwrap()
66        );
67    }
68
69    #[test]
70    fn should_be_converted_from_datetime_utc() {
71        let datetime = DateTime::from_timestamp(0, 0).unwrap();
72        assert_eq!(
73            convert_from_datetime_utc_to_timestamp(&datetime),
74            DurationSinceUnixEpoch::ZERO
75        );
76    }
77
78    #[test]
79    fn should_be_converted_from_datetime_utc_in_iso_8601() {
80        let iso_8601 = "1970-01-01T00:00:00.000Z".to_string();
81        assert_eq!(convert_from_iso_8601_to_timestamp(&iso_8601), DurationSinceUnixEpoch::ZERO);
82    }
83}