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