about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/txn_log.rs
blob: d07b5144ab8e38f116ccd27bd368aa8fe880dd62 (plain) (blame)
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
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use std::fmt::Display;

use chrono::{DateTime, Utc};

use crate::storage::sql::insert::Transactionable;

pub(crate) struct TxnLog<O: Transactionable> {
    inner: Vec<(TimeStamp, O)>,
}

impl<O: Transactionable> TxnLog<O> {
    pub(crate) fn new(inner: Vec<(TimeStamp, O)>) -> Self {
        Self { inner }
    }
    pub(crate) fn inner(&self) -> &[(TimeStamp, O)] {
        &self.inner
    }
}

/// An UNIX time stamp.
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct TimeStamp {
    value: i64,
}
impl TimeStamp {
    /// Return the seconds since the UNIX epoch for this [`TimeStamp`].
    #[must_use]
    pub(crate) fn as_secs(self) -> i64 {
        self.value
    }

    /// Construct a [`TimeStamp`] from a count of seconds since the UNIX epoch.
    #[must_use]
    pub(crate) fn from_secs(value: i64) -> Self {
        Self { value }
    }

    /// Construct a [`TimeStamp`] from the current time.
    #[must_use]
    pub(crate) fn from_now() -> Self {
        Self {
            value: Utc::now().timestamp(),
        }
    }
}
impl Display for TimeStamp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        DateTime::from_timestamp(self.value, 0)
            .expect("The timestamps should always be valid")
            .format("%Y-%m-%d")
            .fmt(f)
    }
}