about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/txn_log.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/storage/txn_log.rs')
-rw-r--r--crates/rocie-server/src/storage/txn_log.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/crates/rocie-server/src/storage/txn_log.rs b/crates/rocie-server/src/storage/txn_log.rs
new file mode 100644
index 0000000..d07b514
--- /dev/null
+++ b/crates/rocie-server/src/storage/txn_log.rs
@@ -0,0 +1,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)
+    }
+}