web_codecs/
timestamp.rs

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::{fmt, ops, time::Duration};

use derive_more::{From, Into};

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, From, Into)]
pub struct Timestamp(Duration);

impl Timestamp {
	pub fn from_micros(micros: u64) -> Self {
		Self(Duration::from_micros(micros))
	}

	pub fn from_millis(millis: u64) -> Self {
		Self(Duration::from_millis(millis))
	}

	pub fn from_secs(seconds: u64) -> Self {
		Self(Duration::from_secs(seconds))
	}

	pub fn from_minutes(minutes: u64) -> Self {
		Self::from_secs(minutes * 60)
	}

	pub fn from_hours(hours: u64) -> Self {
		Self::from_minutes(hours * 60)
	}

	pub fn from_units(value: u64, base: u64) -> Self {
		Self::from_micros((value * 1_000_000) / base)
	}

	pub fn as_micros(self) -> u64 {
		self.0.as_micros() as u64
	}

	pub fn as_millis(self) -> u64 {
		self.0.as_millis() as u64
	}

	pub fn as_secs(self) -> u64 {
		self.0.as_secs()
	}

	pub fn as_minutes(self) -> u64 {
		self.as_secs() / 60
	}

	pub fn as_hours(self) -> u64 {
		self.as_minutes() / 60
	}

	pub fn as_units(self, base: u64) -> u64 {
		(self.as_micros() * base) / 1_000_000
	}
}

impl ops::Deref for Timestamp {
	type Target = Duration;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl ops::Add<Duration> for Timestamp {
	type Output = Timestamp;

	fn add(self, rhs: Duration) -> Self::Output {
		Timestamp(self.0 + rhs)
	}
}

impl ops::Sub<Duration> for Timestamp {
	type Output = Timestamp;

	fn sub(self, rhs: Duration) -> Self::Output {
		Timestamp(self.0 - rhs)
	}
}

impl ops::Sub<Timestamp> for Timestamp {
	type Output = Duration;

	fn sub(self, rhs: Timestamp) -> Self::Output {
		self.0 - rhs.0
	}
}

impl fmt::Debug for Timestamp {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		self.0.fmt(f)
	}
}