multiversx_chain_core/types/time/
timestamp_seconds.rs1use super::{DurationSeconds, TimestampMillis};
2use core::ops::{Add, Sub};
3
4use crate::codec::*;
5
6#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[repr(transparent)]
9pub struct TimestampSeconds(pub(crate) u64);
10
11impl TimestampSeconds {
12 pub const fn new(seconds: u64) -> Self {
13 TimestampSeconds(seconds)
14 }
15
16 pub fn as_u64_seconds(&self) -> u64 {
17 self.0
18 }
19
20 pub fn to_millis(&self) -> TimestampMillis {
22 TimestampMillis::new(self.0 * 1000)
23 }
24
25 pub const fn zero() -> Self {
26 TimestampSeconds(0)
27 }
28
29 pub const fn max() -> Self {
30 TimestampSeconds(u64::MAX)
31 }
32}
33
34impl Sub<TimestampSeconds> for TimestampSeconds {
36 type Output = DurationSeconds;
37
38 fn sub(self, rhs: TimestampSeconds) -> Self::Output {
39 DurationSeconds(self.0 - rhs.0)
40 }
41}
42
43impl Add<DurationSeconds> for TimestampSeconds {
45 type Output = TimestampSeconds;
46
47 fn add(self, rhs: DurationSeconds) -> Self::Output {
48 TimestampSeconds(self.0 + rhs.0)
49 }
50}
51
52impl Sub<DurationSeconds> for TimestampSeconds {
54 type Output = TimestampSeconds;
55
56 fn sub(self, rhs: DurationSeconds) -> Self::Output {
57 TimestampSeconds(self.0 - rhs.0)
58 }
59}
60
61impl NestedEncode for TimestampSeconds {
62 fn dep_encode_or_handle_err<O, H>(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr>
63 where
64 O: NestedEncodeOutput,
65 H: EncodeErrorHandler,
66 {
67 self.0.dep_encode_or_handle_err(dest, h)
68 }
69}
70
71impl TopEncode for TimestampSeconds {
72 fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
73 where
74 O: TopEncodeOutput,
75 H: EncodeErrorHandler,
76 {
77 self.0.top_encode_or_handle_err(output, h)
78 }
79}
80
81impl NestedDecode for TimestampSeconds {
82 fn dep_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
83 where
84 I: NestedDecodeInput,
85 H: DecodeErrorHandler,
86 {
87 Ok(TimestampSeconds(u64::dep_decode_or_handle_err(input, h)?))
88 }
89}
90
91impl TopDecode for TimestampSeconds {
92 fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
93 where
94 I: TopDecodeInput,
95 H: DecodeErrorHandler,
96 {
97 Ok(TimestampSeconds(u64::top_decode_or_handle_err(input, h)?))
98 }
99}
100
101impl core::fmt::Display for TimestampSeconds {
102 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
103 write!(f, "{} s", self.0)
104 }
105}