| 1 | // Copyright 2020 The Matrix.org Foundation C.I.C. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use std::num::NonZeroU8; |
| 16 | |
| 17 | use ruma::MilliSecondsSinceUnixEpoch; |
| 18 | use time::{ |
| 19 | OffsetDateTime, |
| 20 | format_description::well_known::{Iso8601, iso8601}, |
| 21 | }; |
| 22 | |
| 23 | #[cfg(test)] |
| 24 | pub(crate) fn json_convert<T, U>(value: &T) -> serde_json::Result<U> |
| 25 | where |
| 26 | T: serde::Serialize, |
| 27 | U: serde::de::DeserializeOwned, |
| 28 | { |
| 29 | let json = serde_json::to_string(value)?; |
| 30 | serde_json::from_str(&json) |
| 31 | } |
| 32 | |
| 33 | const ISO8601_WITH_MILLIS: iso8601::EncodedConfig = iso8601::Config::DEFAULT |
| 34 | .set_time_precision(iso8601::TimePrecision::Second { decimal_digits: NonZeroU8::new(3) }) |
| 35 | .encode(); |
| 36 | |
| 37 | /// Format the given timestamp into a human-readable timestamp. |
| 38 | /// |
| 39 | /// # Returns |
| 40 | /// |
| 41 | /// Provided the timestamp fits within an `OffsetDateTime` (ie, it is on or |
| 42 | /// before year 9999), a string that looks like `1970-01-01T00:00:00.000Z`. |
| 43 | /// Otherwise, `None`. |
| 44 | pub fn timestamp_to_iso8601(ts: MilliSecondsSinceUnixEpoch) -> Option<String> { |
| 45 | let nanos_since_epoch = i128::from(ts.get()) * 1_000_000; |
| 46 | |
| 47 | // OffsetDateTime has a max year of 9999, whereas MilliSecondsSinceUnixEpoch has |
| 48 | // a max year of 285427, so `from_unix_timestamp_nanos` can overflow for very |
| 49 | // large timestamps. (The Y10K problem!) |
| 50 | let dt = OffsetDateTime::from_unix_timestamp_nanos(nanos_since_epoch).ok()?; |
| 51 | |
| 52 | // SAFETY: `format` can fail if: |
| 53 | // * The input lacks information on a component we have asked it to format |
| 54 | // (eg, it is given a `Time` and we ask it for a date), or |
| 55 | // * The input contains an invalid component (eg 30th February), or |
| 56 | // * An `io::Error` is raised internally. |
| 57 | // |
| 58 | // The first two cannot occur because we know we are giving it a valid |
| 59 | // OffsetDateTime that has all the components we are asking it to print. |
| 60 | // |
| 61 | // The third should not occur because we are formatting a short string to an |
| 62 | // in-memory buffer. |
| 63 | |
| 64 | Some(dt.format(&Iso8601::<ISO8601_WITH_MILLIS>).unwrap()) |
| 65 | } |
| 66 | |
| 67 | #[cfg(test)] |
| 68 | pub(crate) mod tests { |
| 69 | use ruma::{MilliSecondsSinceUnixEpoch, UInt}; |
| 70 | |
| 71 | use super::timestamp_to_iso8601; |
| 72 | |
| 73 | #[test] |
| 74 | fn test_timestamp_to_iso8601() { |
| 75 | assert_eq!( |
| 76 | timestamp_to_iso8601(MilliSecondsSinceUnixEpoch(UInt::new_saturating(0))), |
| 77 | Some("1970-01-01T00:00:00.000Z".to_owned()) |
| 78 | ); |
| 79 | assert_eq!( |
| 80 | timestamp_to_iso8601(MilliSecondsSinceUnixEpoch(UInt::new_saturating(1709657033012))), |
| 81 | Some("2024-03-05T16:43:53.012Z".to_owned()) |
| 82 | ); |
| 83 | assert_eq!(timestamp_to_iso8601(MilliSecondsSinceUnixEpoch(UInt::MAX)), None); |
| 84 | } |
| 85 | } |