1#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
58#[non_exhaustive]
59pub struct Timestamp {
60 seconds: i64,
64
65 nanos: i32,
70}
71
72#[derive(thiserror::Error, Debug)]
87#[non_exhaustive]
88pub enum TimestampError {
89 #[error("seconds and/or nanoseconds out of range")]
91 OutOfRange,
92
93 #[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 pub const MIN_SECONDS: i64 = -62135596800;
107
108 pub const MAX_SECONDS: i64 = 253402300799;
111
112 pub const MIN_NANOS: i32 = 0;
114
115 pub const MAX_NANOS: i32 = Self::NS - 1;
117
118 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 pub fn clamp(seconds: i64, nanos: i32) -> Self {
174 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 pub fn seconds(&self) -> i64 {
204 self.seconds
205 }
206
207 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#[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#[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#[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#[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
337impl 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
356impl 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
381impl 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
399impl 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 let d = e.duration();
426 let s = d.as_secs();
427 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#[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#[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
486impl 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 #[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 #[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(×tamp)?;
614 assert_eq!(
615 format!("\"{input}\""),
616 roundtrip,
617 "mismatched value for input={input}"
618 );
619 Ok(())
620 }
621
622 #[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}