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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use core::fmt::Debug;
#[derive(Debug, PartialEq)]
pub struct ParsedUnitValue<T: Eq + Debug = u8, U: Eq + Debug = u64> {
pub unit: T,
pub total: U,
}
impl ParsedUnitValue<u8, u64> {
/// Converts the provided total seconds into its corresponding seconds (0-59) and the remaining total minutes.
///
/// > The unit is in the range of 0-59, while the total is the result of division by 60.
///
///## example
///```rust
/// use millisecond::pretty::utils::ParsedUnitValue;
///
/// let parsed = ParsedUnitValue::parse_secs(61);
/// assert_eq!(parsed.unit, 1_u8); // 1 second
/// assert_eq!(parsed.total, 1); // 1 minute
/// ```
#[inline]
pub fn parse_secs(total_secs: u64) -> Self {
ParsedUnitValue {
unit: (total_secs % 60) as _,
total: total_secs / 60,
}
}
/// Converts the provided total minutes into its corresponding minutes (0-59) and the remaining total hours.
///
/// > The unit is in the range of 0-59, while the total is the result of division by 60.
///
///## example
///```rust
/// use std::time::Duration;
/// use millisecond::pretty::utils::ParsedUnitValue;
///
/// let parsed = ParsedUnitValue::parse_mins(61);
/// assert_eq!(parsed.unit, 1_u8); // 1 minute
/// assert_eq!(parsed.total, 1); // 1 hour
/// ```
///
#[inline]
pub fn parse_mins(total_mins: u64) -> ParsedUnitValue<u8, u64> {
ParsedUnitValue {
unit: (total_mins % 60) as u8,
total: total_mins / 60,
}
}
/// Converts the provided total hours into its corresponding hours (0-23) and the remaining total days.
///
/// > The unit is in the range of 0-23, while the total is the result of division by 24.
///
/// ## example
///```rust
/// use std::time::Duration;
/// use millisecond::pretty::utils::ParsedUnitValue;
///
/// let parsed = ParsedUnitValue::parse_hours(25);
/// assert_eq!(parsed.unit, 1_u8); // 1 hour
/// assert_eq!(parsed.total, 1); // 1 day
/// ```
///
#[inline]
pub fn parse_hours(total_hours: u64) -> ParsedUnitValue<u8, u64> {
ParsedUnitValue {
unit: (total_hours % 24) as u8,
total: total_hours / 24,
}
}
/// Converts the provided total days into its corresponding days (0-365) and the remaining total years.
///
/// > The unit is in the range of 0-365, while the total is the result of division by 365.
///
/// ## example
///```rust
/// use std::time::Duration;
/// use millisecond::pretty::utils::ParsedUnitValue;
///
/// let parsed = ParsedUnitValue::parse_days(366);
/// assert_eq!(parsed.unit, 1_u16); // 1 day
/// assert_eq!(parsed.total, 1); // 1 year
/// ```
///
#[inline]
pub fn parse_days(total_days: u64) -> ParsedUnitValue<u16, u64> {
ParsedUnitValue {
unit: (total_days % 365) as u16,
total: total_days / 365,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
#[test]
fn should_parse_seconds_and_minutes() {
let cases = vec![
(0, ParsedUnitValue { unit: 0, total: 0 }),
(1, ParsedUnitValue { unit: 1, total: 0 }),
(59, ParsedUnitValue { unit: 59, total: 0 }),
(60, ParsedUnitValue { unit: 0, total: 1 }),
(61, ParsedUnitValue { unit: 1, total: 1 }),
(119, ParsedUnitValue { unit: 59, total: 1 }),
(120, ParsedUnitValue { unit: 0, total: 2 }),
(121, ParsedUnitValue { unit: 1, total: 2 }),
(
3599,
ParsedUnitValue {
unit: 59,
total: 59,
},
),
(3600, ParsedUnitValue { unit: 0, total: 60 }),
(3601, ParsedUnitValue { unit: 1, total: 60 }),
];
for (actual, expected) in cases {
let sec_res = ParsedUnitValue::parse_secs(actual);
let min_res = ParsedUnitValue::parse_mins(actual);
assert_eq!(
sec_res, expected,
"Seconds -> Expected:{expected:?} Got:{sec_res:?}"
);
assert_eq!(
min_res, expected,
"Minutes -> Expected:{expected:?} Got:{min_res:?}"
);
}
}
#[test]
fn should_parse_hours() {
let cases = vec![
(0, ParsedUnitValue { unit: 0, total: 0 }),
(1, ParsedUnitValue { unit: 1, total: 0 }),
(23, ParsedUnitValue { unit: 23, total: 0 }),
(24, ParsedUnitValue { unit: 0, total: 1 }),
(25, ParsedUnitValue { unit: 1, total: 1 }),
(47, ParsedUnitValue { unit: 23, total: 1 }),
(48, ParsedUnitValue { unit: 0, total: 2 }),
(49, ParsedUnitValue { unit: 1, total: 2 }),
(
575,
ParsedUnitValue {
unit: 23,
total: 23,
},
),
(576, ParsedUnitValue { unit: 0, total: 24 }),
(577, ParsedUnitValue { unit: 1, total: 24 }),
];
for (actual, expected) in cases {
let res = ParsedUnitValue::parse_hours(actual);
assert_eq!(res, expected);
}
}
#[test]
fn should_parse_days() {
let cases = vec![
(0, ParsedUnitValue { unit: 0, total: 0 }),
(1, ParsedUnitValue { unit: 1, total: 0 }),
(
364,
ParsedUnitValue {
unit: 364,
total: 0,
},
),
(365, ParsedUnitValue { unit: 0, total: 1 }),
(366, ParsedUnitValue { unit: 1, total: 1 }),
];
for (actual, expected) in cases {
let res = ParsedUnitValue::parse_days(actual);
assert_eq!(res, expected);
}
}
}