Skip to main content

bolt_cw_sdk/
timestamp.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug)]
4pub struct Timestamp {
5    /// The number of seconds since the Unix epoch (1970-01-01T00:00:00Z)
6    pub seconds: u64,
7    /// The number of nanoseconds since the last second
8    pub nanos: u64,
9}
10
11impl Timestamp {
12    pub fn new(seconds: u64, nanos: u64) -> Self {
13        Timestamp { seconds, nanos }
14    }
15}
16
17impl From<Timestamp> for cosmwasm_std::Timestamp {
18    fn from(ts: Timestamp) -> Self {
19        cosmwasm_std::Timestamp::from_seconds(ts.seconds).plus_nanos(ts.nanos)
20    }
21}
22impl From<cosmrs::tendermint::Time> for Timestamp {
23    fn from(ts: cosmrs::tendermint::Time) -> Self {
24        Timestamp {
25            seconds: ts.unix_timestamp() as u64,
26            nanos: 0,
27        }
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn test_tendermint_conversion() {
37        let tm_time = cosmrs::tendermint::Time::from_unix_timestamp(1753112894, 0)
38            .expect("Failed to create tendermint time");
39        let ts: Timestamp = tm_time.into();
40        assert_eq!(ts.seconds, 1753112894);
41        assert_eq!(ts.nanos, 0);
42    }
43}