scsys_time/epoch.rs
1/*
2 Appellation: epoch <module>
3 Created At: 2025.09.08:17:31:13
4 Contrib: @FL03
5*/
6use crate::timestamp::Timestamp;
7use crate::traits::RawTimestamp;
8
9/// An [`Epoch`] represents a span of time defined by a starting [`Timestamp`] and a size in
10/// units of time (e.g., seconds, minutes, hours). The size indicates the duration of the epoch
11/// and is used to determine when the epoch ends.
12#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
13#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
14pub struct Epoch<Ts = u128>
15where
16 Ts: RawTimestamp,
17{
18 size: u128,
19 timestamp: Timestamp<Ts>,
20}
21
22impl<Ts> Epoch<Ts>
23where
24 Ts: RawTimestamp,
25{
26 /// returns a new instance of the [`Epoch`] using the given size and timestamp.
27 pub const fn new(size: u128, timestamp: Ts) -> Self {
28 Self {
29 size,
30 timestamp: Timestamp(timestamp),
31 }
32 }
33 /// The size of the epoch; epochs generally consider the number of steps, or size,
34 /// (e.g. the number of seconds) that will need to be taken before the epoch is considered
35 /// to be complete and a new one begins.
36 pub const fn size(&self) -> u128 {
37 self.size
38 }
39 /// returns an immutable reference to the timestamp
40 pub const fn timestamp(&self) -> &Timestamp<Ts> {
41 &self.timestamp
42 }
43 /// update the size of the epoch
44 pub fn set_size(&mut self, size: u128) {
45 self.size = size;
46 }
47 /// consumes the current instance to create another with the given size and current
48 /// timestamp.
49 pub fn with_size(self, size: u128) -> Self {
50 Self { size, ..self }
51 }
52 /// consumes the current instance to create another with the given timestamp and current
53 /// size.
54 pub fn with_timestamp<U: RawTimestamp>(self, timestamp: U) -> Epoch<U> {
55 Epoch {
56 timestamp: Timestamp(timestamp),
57 size: self.size,
58 }
59 }
60}