ntex_grpc/google_types/
duration.rs

1#![allow(
2    dead_code,
3    unused_mut,
4    unused_variables,
5    clippy::identity_op,
6    clippy::derivable_impls,
7    clippy::unit_arg,
8    clippy::derive_partial_eq_without_eq
9)]
10//! DO NOT MODIFY. Auto-generated file
11
12///  A Duration represents a signed, fixed-length span of time represented
13///  as a count of seconds and fractions of seconds at nanosecond
14///  resolution. It is independent of any calendar and concepts like "day"
15///  or "month". It is related to Timestamp in that the difference between
16///  two Timestamp values is a Duration and it can be added or subtracted
17///  from a Timestamp. Range is approximately +-10,000 years.
18///
19///  # Examples
20///
21///  Example 1: Compute Duration from two Timestamps in pseudo code.
22///
23///      Timestamp start = ...;
24///      Timestamp end = ...;
25///      Duration duration = ...;
26///
27///      duration.seconds = end.seconds - start.seconds;
28///      duration.nanos = end.nanos - start.nanos;
29///
30///      if (duration.seconds < 0 && duration.nanos > 0) {
31///        duration.seconds += 1;
32///        duration.nanos -= 1000000000;
33///      } else if (duration.seconds > 0 && duration.nanos < 0) {
34///        duration.seconds -= 1;
35///        duration.nanos += 1000000000;
36///      }
37///
38///  Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
39///
40///      Timestamp start = ...;
41///      Duration duration = ...;
42///      Timestamp end = ...;
43///
44///      end.seconds = start.seconds + duration.seconds;
45///      end.nanos = start.nanos + duration.nanos;
46///
47///      if (end.nanos < 0) {
48///        end.seconds -= 1;
49///        end.nanos += 1000000000;
50///      } else if (end.nanos >= 1000000000) {
51///        end.seconds += 1;
52///        end.nanos -= 1000000000;
53///      }
54///
55///  Example 3: Compute Duration from datetime.timedelta in Python.
56///
57///      td = datetime.timedelta(days=3, minutes=10)
58///      duration = Duration()
59///      duration.FromTimedelta(td)
60///
61///  # JSON Mapping
62///
63///  In JSON format, the Duration type is encoded as a string rather than an
64///  object, where the string ends in the suffix "s" (indicating seconds) and
65///  is preceded by the number of seconds, with nanoseconds expressed as
66///  fractional seconds. For example, 3 seconds with 0 nanoseconds should be
67///  encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
68///  be expressed in JSON format as "3.000000001s", and 3 seconds and 1
69///  microsecond should be expressed in JSON format as "3.000001s".
70///
71///
72#[derive(Clone, PartialEq, Debug)]
73pub struct Duration {
74    ///  Signed seconds of the span of time. Must be from -315,576,000,000
75    ///  to +315,576,000,000 inclusive. Note: these bounds are computed from:
76    ///  60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
77    pub seconds: i64,
78    ///  Signed fractions of a second at nanosecond resolution of the span
79    ///  of time. Durations less than one second are represented with a 0
80    ///  `seconds` field and a positive or negative `nanos` field. For durations
81    ///  of one second or more, a non-zero value for the `nanos` field must be
82    ///  of the same sign as the `seconds` field. Must be from -999,999,999
83    ///  to +999,999,999 inclusive.
84    pub nanos: i32,
85}
86
87mod _priv_impl {
88    use super::*;
89
90    impl crate::Message for Duration {
91        #[inline]
92        fn write(&self, dst: &mut crate::BytesMut) {
93            crate::NativeType::serialize(
94                &self.seconds,
95                1,
96                crate::types::DefaultValue::Default,
97                dst,
98            );
99            crate::NativeType::serialize(&self.nanos, 2, crate::types::DefaultValue::Default, dst);
100        }
101
102        #[inline]
103        fn read(src: &mut crate::Bytes) -> ::std::result::Result<Self, crate::DecodeError> {
104            const STRUCT_NAME: &str = "Duration";
105            let mut msg = Self::default();
106            while !src.is_empty() {
107                let (tag, wire_type) = crate::encoding::decode_key(src)?;
108                match tag {
109                    1 => crate::NativeType::deserialize(&mut msg.seconds, tag, wire_type, src)
110                        .map_err(|err| err.push(STRUCT_NAME, "seconds"))?,
111                    2 => crate::NativeType::deserialize(&mut msg.nanos, tag, wire_type, src)
112                        .map_err(|err| err.push(STRUCT_NAME, "nanos"))?,
113                    _ => crate::encoding::skip_field(wire_type, tag, src)?,
114                }
115            }
116            Ok(msg)
117        }
118
119        #[inline]
120        fn encoded_len(&self) -> usize {
121            0 + crate::NativeType::serialized_len(
122                &self.seconds,
123                1,
124                crate::types::DefaultValue::Default,
125            ) + crate::NativeType::serialized_len(
126                &self.nanos,
127                2,
128                crate::types::DefaultValue::Default,
129            )
130        }
131    }
132
133    impl ::std::default::Default for Duration {
134        #[inline]
135        fn default() -> Self {
136            Self {
137                seconds: ::core::default::Default::default(),
138                nanos: ::core::default::Default::default(),
139            }
140        }
141    }
142}