1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Time types for mesh protobuf encoding.

use crate::inplace::InplaceOption;
use crate::inplace_some;
use crate::protobuf::MessageReader;
use crate::protobuf::MessageSizer;
use crate::protobuf::MessageWriter;
use crate::protofile::DescribeField;
use crate::protofile::FieldType;
use crate::protofile::MessageDescription;
use crate::table::DescribeTable;
use crate::table::TableEncoder;
use crate::DecodeError;
use crate::MessageDecode;
use crate::MessageEncode;
use mesh_protobuf::Protobuf;
use std::time::Duration;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use thiserror::Error;

const NANOS_PER_SEC: u32 = 1_000_000_000;

impl DescribeTable for Timestamp {
    const DESCRIPTION: MessageDescription<'static> = MessageDescription::External {
        name: "google.protobuf.Timestamp",
        import_path: "google/protobuf/timestamp.proto",
    };
}

/// A timestamp representing a point in UTC time with nanosecond resolution.
#[derive(Debug, Protobuf, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Timestamp {
    /// The number of seconds of UTC time since the Unix epoch.
    #[mesh(1, encoding = "mesh_protobuf::encoding::VarintField")]
    pub seconds: i64,
    /// Non-negative fractions of a second at nanosecond resolution.
    #[mesh(2, encoding = "mesh_protobuf::encoding::VarintField")]
    pub nanos: i32,
}

impl From<SystemTime> for Timestamp {
    fn from(value: SystemTime) -> Self {
        match value.duration_since(UNIX_EPOCH) {
            Ok(since_epoch) => Self {
                seconds: since_epoch.as_secs() as i64,
                nanos: since_epoch.subsec_nanos() as i32,
            },
            Err(err) => {
                let since_epoch = err.duration();
                if since_epoch.subsec_nanos() == 0 {
                    Self {
                        seconds: -(since_epoch.as_secs() as i64),
                        nanos: 0,
                    }
                } else {
                    Self {
                        seconds: -(since_epoch.as_secs() as i64) - 1,
                        nanos: (1_000_000_000 - since_epoch.subsec_nanos()) as i32,
                    }
                }
            }
        }
    }
}

#[derive(Debug, Error)]
#[error("timestamp out of range for system time")]
pub struct TimestampOutOfRange;

impl TryFrom<Timestamp> for SystemTime {
    type Error = TimestampOutOfRange;

    fn try_from(value: Timestamp) -> Result<Self, Self::Error> {
        if value.nanos < 0 || value.nanos >= NANOS_PER_SEC as i32 {
            return Err(TimestampOutOfRange);
        }
        if value.seconds >= 0 {
            SystemTime::UNIX_EPOCH
                .checked_add(Duration::new(value.seconds as u64, value.nanos as u32))
        } else {
            let secs = value.seconds.checked_neg().ok_or(TimestampOutOfRange)? as u64;
            if value.nanos == 0 {
                SystemTime::UNIX_EPOCH.checked_sub(Duration::new(secs, 0))
            } else {
                SystemTime::UNIX_EPOCH
                    .checked_sub(Duration::new(secs - 1, NANOS_PER_SEC - value.nanos as u32))
            }
        }
        .ok_or(TimestampOutOfRange)
    }
}

/// Protobuf-compatible encoding for [`Duration`].
pub struct DurationEncoding;

impl DescribeField<Duration> for DurationEncoding {
    const FIELD_TYPE: FieldType<'static> = FieldType::builtin("google.protobuf.Duration");
}

impl<R> MessageEncode<Duration, R> for DurationEncoding {
    fn write_message(item: Duration, writer: MessageWriter<'_, '_, R>) {
        TableEncoder::write_message((item.as_secs(), item.subsec_nanos()), writer);
    }

    fn compute_message_size(item: &mut Duration, sizer: MessageSizer<'_>) {
        <TableEncoder as MessageEncode<_, R>>::compute_message_size(
            &mut (item.as_secs(), item.subsec_nanos()),
            sizer,
        );
    }
}

impl<R> MessageDecode<'_, Duration, R> for DurationEncoding {
    fn read_message(
        item: &mut InplaceOption<'_, Duration>,
        reader: MessageReader<'_, '_, R>,
    ) -> crate::Result<()> {
        let duration = item.take().unwrap_or_default();
        let message = (duration.as_secs(), duration.subsec_nanos());
        inplace_some!(message);
        TableEncoder::read_message(&mut message, reader)?;
        let (secs, nanos) = message.take().unwrap();
        if (secs as i64) < 0 || nanos >= NANOS_PER_SEC {
            return Err(DecodeError::DurationRange.into());
        }
        item.set(Duration::new(secs, nanos));
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::Timestamp;
    use std::time::SystemTime;

    #[test]
    fn test_timestamp_system_time() {
        let check = |st: SystemTime| {
            let st2 = SystemTime::try_from(Timestamp::from(st)).unwrap();
            assert_eq!(st, st2);
        };

        check(SystemTime::now());
        check(SystemTime::now() + std::time::Duration::from_secs(1));
        check(SystemTime::now() - std::time::Duration::from_secs(1));
        check(SystemTime::UNIX_EPOCH - std::time::Duration::from_nanos(1_500_000_000));
        check(SystemTime::UNIX_EPOCH + std::time::Duration::from_nanos(1_500_000_000));

        assert_eq!(
            Timestamp::from(
                SystemTime::UNIX_EPOCH - std::time::Duration::from_nanos(1_500_000_000)
            ),
            Timestamp {
                seconds: -2,
                nanos: 500_000_000,
            }
        );
    }
}