toe_beans/v4/message/options/
time_offset.rs

1use serde::{Deserialize, Serialize};
2
3/// This type specifies the offset of the client's subnet in
4/// seconds from Coordinated Universal Time (UTC) through an `i32`.
5///
6/// The offset is expressed as a two's complement 32-bit integer.
7/// A positive offset indicates a location east of the zero meridian.
8/// A negative offset indicates a location west of the zero meridian.
9#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
10pub struct TimeOffset(i32);
11
12impl TimeOffset {
13    /// Create a `TimeOffset`.
14    pub fn new(seconds: i32) -> Self {
15        Self(seconds)
16    }
17
18    /// Used, when serializing, to add to the byte buffer.
19    #[inline]
20    pub fn extend_into(&self, bytes: &mut Vec<u8>, tag: u8) {
21        bytes.push(tag); // tag
22        bytes.push(4); // length
23        bytes.extend_from_slice(&self.0.to_be_bytes()); // value
24    }
25}
26
27impl From<&[u8]> for TimeOffset {
28    fn from(value: &[u8]) -> Self {
29        let bytes: [u8; 4] = value.try_into().unwrap();
30        Self(i32::from_be_bytes(bytes))
31    }
32}