Skip to main content

google_cloud_wkt/
timestamp.rs

1// Copyright 2024 Google LLC
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//     https://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/// Well-known point in time representation for Google APIs.
16///
17/// # Examples
18/// ```
19/// # use google_cloud_wkt::{Timestamp, TimestampError};
20/// let ts = Timestamp::try_from("2025-05-16T09:46:12.500Z")?;
21/// assert_eq!(ts.seconds(), 1747388772);
22/// assert_eq!(ts.nanos(), 500_000_000);
23///
24/// assert_eq!(ts, Timestamp::new(1747388772, 500_000_000)?);
25/// assert_eq!(ts, Timestamp::clamp(1747388772, 500_000_000));
26/// # Ok::<(), TimestampError>(())
27/// ```
28///
29/// A Timestamp represents a point in time independent of any time zone or local
30/// calendar, encoded as a count of seconds and fractions of seconds at
31/// nanosecond resolution. The count is relative to an epoch at UTC midnight on
32/// January 1, 1970, in the proleptic Gregorian calendar which extends the
33/// Gregorian calendar backwards to year one.
34///
35/// All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap
36/// second table is needed for interpretation, using a [24-hour linear
37/// smear](https://developers.google.com/time/smear).
38///
39/// The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By
40/// restricting to that range, we ensure that we can convert to and from [RFC
41/// 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings.
42///
43/// # JSON Mapping
44///
45/// In JSON format, the Timestamp type is encoded as a string in the
46/// [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the
47/// format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z"
48/// where {year} is always expressed using four digits while {month}, {day},
49/// {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional
50/// seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution),
51/// are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone
52/// is required.
53///
54/// For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past
55/// 01:30 UTC on January 15, 2017.
56///
57#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
58#[non_exhaustive]
59pub struct Timestamp {
60    /// Represents seconds of UTC time since Unix epoch
61    /// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
62    /// 9999-12-31T23:59:59Z inclusive.
63    seconds: i64,
64
65    /// Non-negative fractions of a second at nanosecond resolution. Negative
66    /// second values with fractions must still have non-negative nanos values
67    /// that count forward in time. Must be from 0 to 999,999,999
68    /// inclusive.
69    nanos: i32,
70}
71
72/// Represent failures in converting or creating [Timestamp] instances.
73///
74/// Examples
75/// ```
76/// # use google_cloud_wkt::{Timestamp, TimestampError};
77/// let ts = Timestamp::new(Timestamp::MAX_SECONDS + 2, 0);
78/// assert!(matches!(ts, Err(TimestampError::OutOfRange)));
79///
80/// let ts = Timestamp::new(0, 1_500_000_000);
81/// assert!(matches!(ts, Err(TimestampError::OutOfRange)));
82///
83/// let ts = Timestamp::try_from("invalid");
84/// assert!(matches!(ts, Err(TimestampError::Deserialize(_))));
85/// ```
86#[derive(thiserror::Error, Debug)]
87#[non_exhaustive]
88pub enum TimestampError {
89    /// One of the components (seconds and/or nanoseconds) was out of range.
90    #[error("seconds and/or nanoseconds out of range")]
91    OutOfRange,
92
93    /// There was a problem deserializing a timestamp.
94    #[error("cannot deserialize timestamp, source={0}")]
95    Deserialize(#[source] BoxedError),
96}
97
98type BoxedError = Box<dyn std::error::Error + Send + Sync>;
99type Error = TimestampError;
100
101impl Timestamp {
102    const NS: i32 = 1_000_000_000;
103
104    // Obtained via: `date +%s --date='0001-01-01T00:00:00Z'`
105    /// The minimum value for the `seconds` component. Corresponds to '0001-01-01T00:00:00Z'.
106    pub const MIN_SECONDS: i64 = -62135596800;
107
108    // Obtained via: `date +%s --date='9999-12-31T23:59:59Z'`
109    /// The maximum value for the `seconds` component. Corresponds to '9999-12-31T23:59:59Z'.
110    pub const MAX_SECONDS: i64 = 253402300799;
111
112    /// The minimum value for the `nanos` component.
113    pub const MIN_NANOS: i32 = 0;
114
115    /// The maximum value for the `nanos` component.
116    pub const MAX_NANOS: i32 = Self::NS - 1;
117
118    /// Creates a new [Timestamp] from the seconds and nanoseconds.
119    ///
120    /// If either value is out of range it returns an error.
121    ///
122    /// # Examples
123    /// ```
124    /// # use google_cloud_wkt::{Timestamp, TimestampError};
125    /// let ts = Timestamp::new(1747388772, 0)?;
126    /// assert_eq!(String::from(ts), "2025-05-16T09:46:12Z");
127    ///
128    /// let ts = Timestamp::new(1747388772, 2_000_000_000);
129    /// assert!(matches!(ts, Err(TimestampError::OutOfRange)));
130    /// # Ok::<(), TimestampError>(())
131    /// ```
132    ///
133    /// # Parameters
134    ///
135    /// * `seconds` - the seconds on the timestamp.
136    /// * `nanos` - the nanoseconds on the timestamp.
137    pub fn new(seconds: i64, nanos: i32) -> Result<Self, Error> {
138        if !(Self::MIN_SECONDS..=Self::MAX_SECONDS).contains(&seconds) {
139            return Err(Error::OutOfRange);
140        }
141        if !(Self::MIN_NANOS..=Self::MAX_NANOS).contains(&nanos) {
142            return Err(Error::OutOfRange);
143        }
144        Ok(Self { seconds, nanos })
145    }
146
147    /// Create a normalized, clamped [Timestamp].
148    ///
149    /// # Examples
150    /// ```
151    /// # use google_cloud_wkt::{Timestamp, TimestampError};
152    /// let ts = Timestamp::clamp(1747388772, 0);
153    /// assert_eq!(String::from(ts), "2025-05-16T09:46:12Z");
154    ///
155    /// let ts = Timestamp::clamp(1747388772, 2_000_000_000);
156    /// // extra nanoseconds are carried as seconds
157    /// assert_eq!(String::from(ts), "2025-05-16T09:46:14Z");
158    /// # Ok::<(), TimestampError>(())
159    /// ```
160    ///
161    /// Timestamps must be between 0001-01-01T00:00:00Z and
162    /// 9999-12-31T23:59:59.999999999Z, and the nanoseconds component must
163    /// always be in the range [0, 999_999_999]. This function creates a
164    /// new [Timestamp] instance clamped to those ranges.
165    ///
166    /// The function effectively adds the nanoseconds part (with carry) to the
167    /// seconds part, with saturation.
168    ///
169    /// # Parameters
170    ///
171    /// * `seconds` - the seconds on the timestamp.
172    /// * `nanos` - the nanoseconds added to the seconds.
173    pub fn clamp(seconds: i64, nanos: i32) -> Self {
174        // `div_euclid` rounds toward negative infinity, so the remainder from
175        // `rem_euclid` is always in `[0, NS)`, even for negative `nanos`.
176        let seconds = seconds.saturating_add(i64::from(nanos.div_euclid(Self::NS)));
177        let nanos = nanos.rem_euclid(Self::NS);
178        if seconds < Self::MIN_SECONDS {
179            return Self {
180                seconds: Self::MIN_SECONDS,
181                nanos: 0,
182            };
183        } else if seconds > Self::MAX_SECONDS {
184            return Self {
185                seconds: Self::MAX_SECONDS,
186                nanos: 0,
187            };
188        }
189        Self { seconds, nanos }
190    }
191
192    /// Represents seconds of UTC time since Unix epoch (1970-01-01T00:00:00Z).
193    ///
194    /// Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.
195    ///
196    /// # Examples
197    /// ```
198    /// # use google_cloud_wkt::{Timestamp, TimestampError};
199    /// let ts = Timestamp::new(120, 500_000_000)?;
200    /// assert_eq!(ts.seconds(), 120);
201    /// # Ok::<(), TimestampError>(())
202    /// ```
203    pub fn seconds(&self) -> i64 {
204        self.seconds
205    }
206
207    /// Non-negative fractions of a second at nanosecond resolution.
208    ///
209    /// Negative second values (before the Unix epoch) with fractions must still
210    /// have non-negative nanos values that count forward in time. Must be from
211    /// 0 to 999,999,999 inclusive.
212    ///
213    /// # Examples
214    /// ```
215    /// # use google_cloud_wkt::{Timestamp, TimestampError};
216    /// let ts = Timestamp::new(120, 500_000_000)?;
217    /// assert_eq!(ts.nanos(), 500_000_000);
218    /// # Ok::<(), TimestampError>(())
219    /// ```
220    pub fn nanos(&self) -> i32 {
221        self.nanos
222    }
223}
224
225impl crate::message::Message for Timestamp {
226    fn typename() -> &'static str {
227        "type.googleapis.com/google.protobuf.Timestamp"
228    }
229
230    #[allow(private_interfaces)]
231    fn serializer() -> impl crate::message::MessageSerializer<Self> {
232        crate::message::ValueSerializer::<Self>::new()
233    }
234}
235
236const NS: i128 = 1_000_000_000;
237
238/// Implement [`serde`](::serde) serialization for timestamps.
239#[cfg_attr(not(feature = "_internal-semver"), doc(hidden))]
240impl serde::ser::Serialize for Timestamp {
241    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
242    where
243        S: serde::ser::Serializer,
244    {
245        String::from(*self).serialize(serializer)
246    }
247}
248
249struct TimestampVisitor;
250
251impl serde::de::Visitor<'_> for TimestampVisitor {
252    type Value = Timestamp;
253
254    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
255        formatter.write_str("a string with a timestamp in RFC 3339 format")
256    }
257
258    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
259    where
260        E: serde::de::Error,
261    {
262        Timestamp::try_from(value).map_err(E::custom)
263    }
264}
265
266/// Implement [`serde`](::serde) deserialization for timestamps.
267#[cfg_attr(not(feature = "_internal-semver"), doc(hidden))]
268impl<'de> serde::de::Deserialize<'de> for Timestamp {
269    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
270    where
271        D: serde::Deserializer<'de>,
272    {
273        deserializer.deserialize_str(TimestampVisitor)
274    }
275}
276
277/// Convert from [time::OffsetDateTime] to [Timestamp].
278///
279/// This conversion may fail if the [time::OffsetDateTime] value is out of range.
280///
281/// # Examples
282/// ```
283/// # use google_cloud_wkt::Timestamp;
284/// use time::{macros::datetime, OffsetDateTime};
285/// let dt = datetime!(2025-05-16 09:46:12 UTC);
286/// let ts = Timestamp::try_from(dt)?;
287/// assert_eq!(String::from(ts), "2025-05-16T09:46:12Z");
288/// # Ok::<(), anyhow::Error>(())
289/// ```
290#[cfg(feature = "time")]
291#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
292impl TryFrom<time::OffsetDateTime> for Timestamp {
293    type Error = TimestampError;
294
295    fn try_from(value: time::OffsetDateTime) -> Result<Self, Self::Error> {
296        let seconds = value.unix_timestamp();
297        let nanos = (value.unix_timestamp_nanos() - seconds as i128 * NS) as i32;
298        Self::new(seconds, nanos)
299    }
300}
301
302/// Convert from [Timestamp] to [OffsetDateTime][time::OffsetDateTime]
303///
304/// This conversion may fail if the [Timestamp] value is out of range.
305///
306/// # Examples
307/// ```
308/// # use google_cloud_wkt::Timestamp;
309/// use time::{macros::datetime, OffsetDateTime};
310/// let ts = Timestamp::try_from("2025-05-16T09:46:12Z")?;
311/// let dt = OffsetDateTime::try_from(ts)?;
312/// assert_eq!(dt, datetime!(2025-05-16 09:46:12 UTC));
313/// # Ok::<(), anyhow::Error>(())
314/// ```
315#[cfg(feature = "time")]
316#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
317impl TryFrom<Timestamp> for time::OffsetDateTime {
318    type Error = time::error::ComponentRange;
319    fn try_from(value: Timestamp) -> Result<Self, Self::Error> {
320        let ts = time::OffsetDateTime::from_unix_timestamp(value.seconds())?;
321        Ok(ts + time::Duration::nanoseconds(value.nanos() as i64))
322    }
323}
324
325const EXPECT_OFFSET_DATE_TIME_CONVERTS: &str = concat!(
326    "converting Timestamp to time::OffsetDateTime should always succeed. ",
327    "The Timestamp values are always in range. ",
328    "If this is not the case, please file a bug at https://github.com/googleapis/google-cloud-rust/issues"
329);
330const EXPECT_TIMESTAMP_FORMAT_SUCCEEDS: &str = concat!(
331    "formatting a Timestamp using RFC-3339 should always succeed. ",
332    "The Timestamp values are always in range, and we use a well-known constant for the format specifier. ",
333    "If this is not the case, please file a bug at https://github.com/googleapis/google-cloud-rust/issues"
334);
335use time::format_description::well_known::Rfc3339;
336
337/// Converts a [Timestamp] to its [String] representation.
338///
339/// # Example
340/// ```
341/// # use google_cloud_wkt::{Timestamp, TimestampError};
342/// let ts = Timestamp::new(1747388772, 0)?;
343/// assert_eq!(String::from(ts), "2025-05-16T09:46:12Z");
344/// # Ok::<(), anyhow::Error>(())
345/// ```
346impl From<Timestamp> for String {
347    fn from(timestamp: Timestamp) -> Self {
348        let ts = time::OffsetDateTime::from_unix_timestamp_nanos(
349            timestamp.seconds as i128 * NS + timestamp.nanos as i128,
350        )
351        .expect(EXPECT_OFFSET_DATE_TIME_CONVERTS);
352        ts.format(&Rfc3339).expect(EXPECT_TIMESTAMP_FORMAT_SUCCEEDS)
353    }
354}
355
356/// Converts the string representation of a timestamp to [Timestamp].
357///
358/// # Example
359/// ```
360/// # use google_cloud_wkt::{Timestamp, TimestampError};
361/// let ts = Timestamp::try_from("2025-05-16T09:46:12.500Z")?;
362/// assert_eq!(ts.seconds(), 1747388772);
363/// assert_eq!(ts.nanos(), 500_000_000);
364/// # Ok::<(), anyhow::Error>(())
365/// ```
366impl TryFrom<&str> for Timestamp {
367    type Error = TimestampError;
368    fn try_from(value: &str) -> Result<Self, Self::Error> {
369        let odt = time::OffsetDateTime::parse(value, &Rfc3339)
370            .map_err(|e| TimestampError::Deserialize(e.into()))?;
371        let nanos_since_epoch = odt.unix_timestamp_nanos();
372        let seconds = (nanos_since_epoch / NS) as i64;
373        let nanos = (nanos_since_epoch % NS) as i32;
374        if nanos < 0 {
375            return Timestamp::new(seconds - 1, Self::NS + nanos);
376        }
377        Timestamp::new(seconds, nanos)
378    }
379}
380
381/// Converts the string representation of a timestamp to [Timestamp].
382///
383/// # Example
384/// ```
385/// # use google_cloud_wkt::{Timestamp, TimestampError};
386/// let s = "2025-05-16T09:46:12.500Z".to_string();
387/// let ts = Timestamp::try_from(&s)?;
388/// assert_eq!(ts.seconds(), 1747388772);
389/// assert_eq!(ts.nanos(), 500_000_000);
390/// # Ok::<(), anyhow::Error>(())
391/// ```
392impl TryFrom<&String> for Timestamp {
393    type Error = TimestampError;
394    fn try_from(value: &String) -> Result<Self, Self::Error> {
395        Timestamp::try_from(value.as_str())
396    }
397}
398
399/// Converts from [std::time::SystemTime] to [Timestamp].
400///
401/// This conversion may fail if the [std::time::SystemTime] value is out of
402/// range.
403///
404/// # Example
405/// ```
406/// # use std::time::SystemTime;
407/// # use google_cloud_wkt::{Timestamp, TimestampError};
408/// let ts = Timestamp::try_from(SystemTime::now())?;
409/// println!("now={ts:?}");
410/// # Ok::<(), anyhow::Error>(())
411/// ```
412impl TryFrom<std::time::SystemTime> for Timestamp {
413    type Error = TimestampError;
414    fn try_from(value: std::time::SystemTime) -> Result<Self, Self::Error> {
415        match value.duration_since(std::time::SystemTime::UNIX_EPOCH) {
416            Ok(d) => {
417                let s = d.as_secs();
418                if s > i64::MAX as u64 {
419                    return Err(TimestampError::OutOfRange);
420                }
421                Timestamp::new(s as i64, d.subsec_nanos() as i32)
422            }
423            Err(e) => {
424                // The error case essentially gives us a negative duration.
425                let d = e.duration();
426                let s = d.as_secs();
427                // We might add 1 to s later, but recall that `-i64::MIN == i64::MAX + 1`
428                if s > i64::MAX as u64 {
429                    return Err(TimestampError::OutOfRange);
430                }
431                let seconds = -(s as i64);
432                let nanos = d.subsec_nanos() as i32;
433                if nanos > 0 {
434                    return Timestamp::new(seconds - 1, Self::NS - nanos);
435                }
436                Timestamp::new(seconds, 0)
437            }
438        }
439    }
440}
441
442/// Converts from [chrono::DateTime] to [Timestamp].
443///
444/// This conversion may fail if the [chrono::DateTime] value is out of range.
445///
446/// # Example
447/// ```
448/// # use google_cloud_wkt::{Timestamp, TimestampError};
449/// use chrono::{DateTime, TimeZone, Utc};
450/// let date : DateTime<Utc> = Utc.with_ymd_and_hms(2025, 5, 16, 10, 15, 00).unwrap();
451/// let ts = Timestamp::try_from(date)?;
452/// assert_eq!(String::from(ts), "2025-05-16T10:15:00Z");
453/// # Ok::<(), anyhow::Error>(())
454/// ```
455#[cfg(feature = "chrono")]
456#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
457impl TryFrom<chrono::DateTime<chrono::Utc>> for Timestamp {
458    type Error = TimestampError;
459
460    fn try_from(value: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
461        assert!(value.timestamp_subsec_nanos() <= (i32::MAX as u32));
462        Timestamp::new(value.timestamp(), value.timestamp_subsec_nanos() as i32)
463    }
464}
465
466/// Converts from [Timestamp] to [chrono::DateTime].
467///
468/// # Example
469/// ```
470/// # use google_cloud_wkt::{Timestamp, TimestampError};
471/// use chrono::{DateTime, TimeZone, Utc};
472/// let ts = Timestamp::try_from("2025-05-16T10:15:00Z")?;
473/// let date = DateTime::try_from(ts)?;
474/// # Ok::<(), anyhow::Error>(())
475/// ```
476#[cfg(feature = "chrono")]
477#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
478impl TryFrom<Timestamp> for chrono::DateTime<chrono::Utc> {
479    type Error = TimestampError;
480    fn try_from(value: Timestamp) -> Result<Self, Self::Error> {
481        let ts = chrono::DateTime::from_timestamp(value.seconds, 0).unwrap();
482        Ok(ts + chrono::Duration::nanoseconds(value.nanos as i64))
483    }
484}
485
486/// Converts from [Timestamp] to [std::time::SystemTime].
487///
488/// This conversion may fail if the [std::time::SystemTime] value is out of
489/// range.
490///
491/// # Example
492/// ```
493/// # use std::time::SystemTime;
494/// # use google_cloud_wkt::{Timestamp, TimestampError};
495/// let ts = Timestamp::new(0, 0)?;
496/// let epoch = SystemTime::try_from(ts)?;
497/// assert_eq!(epoch, SystemTime::UNIX_EPOCH);
498/// # Ok::<(), anyhow::Error>(())
499/// ```
500impl TryFrom<Timestamp> for std::time::SystemTime {
501    type Error = TimestampError;
502    fn try_from(value: Timestamp) -> Result<std::time::SystemTime, Self::Error> {
503        let s = value.seconds();
504        let ts = if s >= 0 {
505            let d = std::time::Duration::from_secs(s as u64);
506            std::time::SystemTime::UNIX_EPOCH
507                .checked_add(d)
508                .ok_or_else(|| TimestampError::OutOfRange)?
509        } else {
510            let d = std::time::Duration::from_secs(-s as u64);
511            std::time::SystemTime::UNIX_EPOCH
512                .checked_sub(d)
513                .ok_or_else(|| TimestampError::OutOfRange)?
514        };
515        let d = std::time::Duration::from_nanos(value.nanos() as u64);
516        ts.checked_add(d).ok_or_else(|| TimestampError::OutOfRange)
517    }
518}
519
520#[cfg(test)]
521mod tests {
522    use super::*;
523    use serde_json::json;
524    use test_case::test_case;
525    type Result = std::result::Result<(), Box<dyn std::error::Error>>;
526
527    // Verify the epoch converts as expected.
528    #[test]
529    fn unix_epoch() -> Result {
530        let proto = Timestamp::default();
531        let json = serde_json::to_value(proto)?;
532        let expected = json!("1970-01-01T00:00:00Z");
533        assert_eq!(json, expected);
534        let roundtrip = serde_json::from_value::<Timestamp>(json)?;
535        assert_eq!(proto, roundtrip);
536        Ok(())
537    }
538
539    fn get_seconds(input: &str) -> i64 {
540        let odt = time::OffsetDateTime::parse(input, &Rfc3339);
541        let odt = odt.unwrap();
542        odt.unix_timestamp()
543    }
544
545    fn get_min_seconds() -> i64 {
546        self::get_seconds("0001-01-01T00:00:00Z")
547    }
548
549    fn get_max_seconds() -> i64 {
550        self::get_seconds("9999-12-31T23:59:59Z")
551    }
552
553    #[test_case(get_min_seconds() - 1, 0; "seconds below range")]
554    #[test_case(get_max_seconds() + 1, 0; "seconds above range")]
555    #[test_case(0, -1; "nanos below range")]
556    #[test_case(0, 1_000_000_000; "nanos above range")]
557    fn new_out_of_range(seconds: i64, nanos: i32) -> Result {
558        let t = Timestamp::new(seconds, nanos);
559        assert!(matches!(t, Err(Error::OutOfRange)), "{t:?}");
560        Ok(())
561    }
562
563    #[test_case(0, 0, 0, 0; "zero")]
564    #[test_case(0, 1_234_567_890, 1, 234_567_890; "nanos overflow")]
565    #[test_case(0, 2_100_000_123, 2, 100_000_123; "nanos overflow x2")]
566    #[test_case(0, -1_400_000_000, -2, 600_000_000; "nanos underflow")]
567    #[test_case(0, -2_100_000_000, -3, 900_000_000; "nanos underflow x2")]
568    #[test_case(0, -1, -1, 999_999_999; "nanos underflow by one")]
569    #[test_case(0, -1_000_000_000, -1, 0; "nanos underflow exact")]
570    #[test_case(0, -2_000_000_000, -2, 0; "nanos underflow exact x2")]
571    #[test_case(0, i32::MIN, -3, 852_516_352; "nanos min")]
572    #[test_case(0, i32::MAX, 2, 147_483_647; "nanos max")]
573    #[test_case(self::get_max_seconds() + 1, 0, get_max_seconds(), 0; "seconds over range")]
574    #[test_case(self::get_min_seconds() - 1, 0, get_min_seconds(), 0; "seconds below range")]
575    #[test_case(self::get_max_seconds() - 1, 2_000_000_001, get_max_seconds(), 0; "nanos overflow range"
576	)]
577    #[test_case(self::get_min_seconds() + 1, -1_500_000_000, get_min_seconds(), 0; "nanos underflow range"
578	)]
579    #[test_case(self::get_max_seconds() + 1, -1_000_000_000, get_max_seconds(), 0; "nanos underflow exact to max")]
580    #[test_case(self::get_max_seconds() + 2, -1_000_000_000, get_max_seconds(), 0; "nanos underflow exact range")]
581    fn clamp(seconds: i64, nanos: i32, want_seconds: i64, want_nanos: i32) {
582        let got = Timestamp::clamp(seconds, nanos);
583        let want = Timestamp {
584            seconds: want_seconds,
585            nanos: want_nanos,
586        };
587        assert_eq!(got, want);
588    }
589
590    // Verify timestamps can roundtrip from string -> struct -> string without loss.
591    #[test_case("0001-01-01T00:00:00.123456789Z")]
592    #[test_case("0001-01-01T00:00:00.123456Z")]
593    #[test_case("0001-01-01T00:00:00.123Z")]
594    #[test_case("0001-01-01T00:00:00Z")]
595    #[test_case("1960-01-01T00:00:00.123456789Z")]
596    #[test_case("1960-01-01T00:00:00.123456Z")]
597    #[test_case("1960-01-01T00:00:00.123Z")]
598    #[test_case("1960-01-01T00:00:00Z")]
599    #[test_case("1970-01-01T00:00:00.123456789Z")]
600    #[test_case("1970-01-01T00:00:00.123456Z")]
601    #[test_case("1970-01-01T00:00:00.123Z")]
602    #[test_case("1970-01-01T00:00:00Z")]
603    #[test_case("9999-12-31T23:59:59.999999999Z")]
604    #[test_case("9999-12-31T23:59:59.123456789Z")]
605    #[test_case("9999-12-31T23:59:59.123456Z")]
606    #[test_case("9999-12-31T23:59:59.123Z")]
607    #[test_case("2024-10-19T12:34:56Z")]
608    #[test_case("2024-10-19T12:34:56.789Z")]
609    #[test_case("2024-10-19T12:34:56.789123456Z")]
610    fn roundtrip(input: &str) -> Result {
611        let json = serde_json::Value::String(input.to_string());
612        let timestamp = serde_json::from_value::<Timestamp>(json)?;
613        let roundtrip = serde_json::to_string(&timestamp)?;
614        assert_eq!(
615            format!("\"{input}\""),
616            roundtrip,
617            "mismatched value for input={input}"
618        );
619        Ok(())
620    }
621
622    // Verify timestamps work for some well know times, including fractional
623    // seconds.
624    #[test_case(
625        "0001-01-01T00:00:00.123456789Z",
626        Timestamp::clamp(Timestamp::MIN_SECONDS, 123_456_789)
627    )]
628    #[test_case(
629        "0001-01-01T00:00:00.123456Z",
630        Timestamp::clamp(Timestamp::MIN_SECONDS, 123_456_000)
631    )]
632    #[test_case(
633        "0001-01-01T00:00:00.123Z",
634        Timestamp::clamp(Timestamp::MIN_SECONDS, 123_000_000)
635    )]
636    #[test_case("0001-01-01T00:00:00Z", Timestamp::clamp(Timestamp::MIN_SECONDS, 0))]
637    #[test_case("1970-01-01T00:00:00.123456789Z", Timestamp::clamp(0, 123_456_789))]
638    #[test_case("1970-01-01T00:00:00.123456Z", Timestamp::clamp(0, 123_456_000))]
639    #[test_case("1970-01-01T00:00:00.123Z", Timestamp::clamp(0, 123_000_000))]
640    #[test_case("1970-01-01T00:00:00Z", Timestamp::clamp(0, 0))]
641    #[test_case(
642        "9999-12-31T23:59:59.123456789Z",
643        Timestamp::clamp(Timestamp::MAX_SECONDS, 123_456_789)
644    )]
645    #[test_case(
646        "9999-12-31T23:59:59.123456Z",
647        Timestamp::clamp(Timestamp::MAX_SECONDS, 123_456_000)
648    )]
649    #[test_case(
650        "9999-12-31T23:59:59.123Z",
651        Timestamp::clamp(Timestamp::MAX_SECONDS, 123_000_000)
652    )]
653    #[test_case("9999-12-31T23:59:59Z", Timestamp::clamp(Timestamp::MAX_SECONDS, 0))]
654    fn well_known(input: &str, want: Timestamp) -> Result {
655        let json = serde_json::Value::String(input.to_string());
656        let got = serde_json::from_value::<Timestamp>(json)?;
657        assert_eq!(want, got);
658        Ok(())
659    }
660
661    #[test_case("1970-01-01T00:00:00Z", Timestamp::clamp(0, 0); "zulu offset")]
662    #[test_case("1970-01-01T00:00:00+02:00", Timestamp::clamp(-2 * 60 * 60, 0); "2h positive")]
663    #[test_case("1970-01-01T00:00:00+02:45", Timestamp::clamp(-2 * 60 * 60 - 45 * 60, 0); "2h45m positive"
664	)]
665    #[test_case("1970-01-01T00:00:00-02:00", Timestamp::clamp(2 * 60 * 60, 0); "2h negative")]
666    #[test_case("1970-01-01T00:00:00-02:45", Timestamp::clamp(2 * 60 * 60 + 45 * 60, 0); "2h45m negative"
667	)]
668    fn deserialize_offsets(input: &str, want: Timestamp) -> Result {
669        let json = serde_json::Value::String(input.to_string());
670        let got = serde_json::from_value::<Timestamp>(json)?;
671        assert_eq!(want, got);
672        Ok(())
673    }
674
675    #[test_case("0000-01-01T00:00:00Z"; "below range")]
676    #[test_case("10000-01-01T00:00:00Z"; "above range")]
677    fn deserialize_out_of_range(input: &str) -> Result {
678        let value = serde_json::to_value(input)?;
679        let got = serde_json::from_value::<Timestamp>(value);
680        assert!(got.is_err(), "{got:?}");
681        Ok(())
682    }
683
684    #[test]
685    fn deserialize_unexpected_input_type() -> Result {
686        let got = serde_json::from_value::<Timestamp>(serde_json::json!({}));
687        assert!(got.is_err(), "{got:?}");
688        let msg = format!("{got:?}");
689        assert!(msg.contains("RFC 3339"), "message={msg}");
690        Ok(())
691    }
692
693    #[serde_with::skip_serializing_none]
694    #[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
695    #[serde(rename_all = "camelCase")]
696    struct Helper {
697        pub create_time: Option<Timestamp>,
698    }
699
700    #[test]
701    fn access() {
702        let ts = Timestamp::default();
703        assert_eq!(ts.nanos(), 0);
704        assert_eq!(ts.seconds(), 0);
705    }
706
707    #[test]
708    fn serialize_in_struct() -> Result {
709        let input = Helper {
710            ..Default::default()
711        };
712        let json = serde_json::to_value(input)?;
713        assert_eq!(json, json!({}));
714
715        let input = Helper {
716            create_time: Some(Timestamp::new(12, 345_678_900)?),
717        };
718
719        let json = serde_json::to_value(input)?;
720        assert_eq!(
721            json,
722            json!({ "createTime": "1970-01-01T00:00:12.3456789Z" })
723        );
724        Ok(())
725    }
726
727    #[test]
728    fn deserialize_in_struct() -> Result {
729        let input = json!({});
730        let want = Helper {
731            ..Default::default()
732        };
733        let got = serde_json::from_value::<Helper>(input)?;
734        assert_eq!(want, got);
735
736        let input = json!({ "createTime": "1970-01-01T00:00:12.3456789Z" });
737        let want = Helper {
738            create_time: Some(Timestamp::new(12, 345678900)?),
739        };
740        let got = serde_json::from_value::<Helper>(input)?;
741        assert_eq!(want, got);
742        Ok(())
743    }
744
745    #[test]
746    fn compare() -> Result {
747        let ts0 = Timestamp::default();
748        let ts1 = Timestamp::new(1, 100)?;
749        let ts2 = Timestamp::new(1, 200)?;
750        let ts3 = Timestamp::new(2, 0)?;
751        assert_eq!(ts0.partial_cmp(&ts0), Some(std::cmp::Ordering::Equal));
752        assert_eq!(ts0.partial_cmp(&ts1), Some(std::cmp::Ordering::Less));
753        assert_eq!(ts2.partial_cmp(&ts3), Some(std::cmp::Ordering::Less));
754        Ok(())
755    }
756
757    #[test]
758    fn convert_from_string() -> Result {
759        let input = "2025-05-16T18:00:00Z".to_string();
760        let a = Timestamp::try_from(input.as_str())?;
761        let b = Timestamp::try_from(&input)?;
762        assert_eq!(a, b);
763        Ok(())
764    }
765
766    #[test]
767    fn convert_from_time() -> Result {
768        let ts = time::OffsetDateTime::from_unix_timestamp(123)?
769            + time::Duration::nanoseconds(456789012);
770        let got = Timestamp::try_from(ts)?;
771        let want = Timestamp::new(123, 456789012)?;
772        assert_eq!(got, want);
773        Ok(())
774    }
775
776    #[test]
777    fn convert_to_time() -> Result {
778        let ts = Timestamp::new(123, 456789012)?;
779        let got = time::OffsetDateTime::try_from(ts)?;
780        let want = time::OffsetDateTime::from_unix_timestamp(123)?
781            + time::Duration::nanoseconds(456789012);
782        assert_eq!(got, want);
783        Ok(())
784    }
785
786    #[test]
787    fn convert_from_chrono_time() -> Result {
788        let ts = chrono::DateTime::from_timestamp(123, 456789012).unwrap();
789        let got = Timestamp::try_from(ts)?;
790        let want = Timestamp::new(123, 456789012)?;
791        assert_eq!(got, want);
792        Ok(())
793    }
794
795    #[test]
796    fn convert_to_chrono_time() -> Result {
797        let ts = Timestamp::new(123, 456789012)?;
798        let got = chrono::DateTime::try_from(ts)?;
799        let want = chrono::DateTime::from_timestamp(123, 456789012).unwrap();
800        assert_eq!(got, want);
801        Ok(())
802    }
803
804    #[test]
805    fn convert_from_std_time() -> Result {
806        let now = std::time::SystemTime::now();
807        let want = now.duration_since(std::time::SystemTime::UNIX_EPOCH)?;
808
809        let wkt = Timestamp::try_from(now)?;
810        assert_eq!(wkt.seconds(), want.as_secs() as i64);
811        assert_eq!(wkt.nanos(), want.subsec_nanos() as i32);
812
813        let past = std::time::SystemTime::UNIX_EPOCH
814            .checked_sub(std::time::Duration::from_secs(123))
815            .unwrap()
816            .checked_sub(std::time::Duration::from_nanos(100))
817            .unwrap();
818        let wkt = Timestamp::try_from(past)?;
819        assert_eq!(wkt.seconds(), -124);
820        assert_eq!(wkt.nanos(), Timestamp::NS - 100);
821        Ok(())
822    }
823
824    #[test]
825    fn convert_to_std_time() -> Result {
826        let ts = Timestamp::clamp(123, 456789000);
827        let got = std::time::SystemTime::try_from(ts)?;
828        let want = std::time::SystemTime::UNIX_EPOCH
829            .checked_add(std::time::Duration::from_secs(123))
830            .unwrap()
831            .checked_add(std::time::Duration::from_nanos(456789000))
832            .unwrap();
833        assert_eq!(got, want);
834
835        let ts = Timestamp::clamp(-123, -456789000);
836        let got = std::time::SystemTime::try_from(ts)?;
837        let want = std::time::SystemTime::UNIX_EPOCH
838            .checked_sub(std::time::Duration::from_secs(123))
839            .unwrap()
840            .checked_sub(std::time::Duration::from_nanos(456789000))
841            .unwrap();
842        assert_eq!(got, want);
843        Ok(())
844    }
845}