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
#![doc = "Fast converter various time formats into milliseconds."]
#![doc(issue_tracker_base_url = "https://github.com/Mnwa/ms/issues/")]
#![doc(html_root_url = "https://docs.rs/ms-converter/")]
use std::fmt::Formatter;
use std::time::Duration;
pub const SECOND: f64 = 1000_f64;
pub const MINUTE: f64 = SECOND * 60_f64;
pub const HOUR: f64 = MINUTE * 60_f64;
pub const DAY: f64 = HOUR * 24_f64;
pub const WEEK: f64 = DAY * 7_f64;
pub const YEAR: f64 = DAY * 365.25_f64;
#[inline(always)]
pub fn ms(s: &str) -> Result<i64, Error> {
let value_count = s
.bytes()
.take_while(|c| (b'0'..=b'9').contains(c) || c == &b'.' || c == &b'-')
.count();
let (value, postfix) = s.split_at(value_count);
let value: f64 = value
.parse()
.map_or(Err(Error::new("invalid value")), |v| Ok(v))?;
let postfix = postfix.trim();
let ret = match postfix {
"years" | "year" | "yrs" | "yr" | "y" => value * YEAR,
"weeks" | "week" | "w" => value * WEEK,
"days" | "day" | "d" => value * DAY,
"hours" | "hour" | "hrs" | "hr" | "h" => value * HOUR,
"minutes" | "minute" | "mins" | "min" | "m" => value * MINUTE,
"seconds" | "second" | "secs" | "sec" | "s" => value * SECOND,
"milliseconds" | "millisecond" | "msecs" | "msec" | "ms" | "" => value,
_ => return Err(Error::new("invalid postfix")),
};
Ok(ret.round() as i64)
}
#[macro_export]
macro_rules! ms_expr {
($type:ty, $x:literal $(milliseconds)?$(millisecond)?$(msecs)?$(msec)?$(ms)?) => {{
let x: $type = $x;
x
}};
($type:ty, $x:literal $(seconds)?$(second)?$(secs)?$(sec)?$(s)?) => {{
let x: $type = $x * ($crate::SECOND as $type);
x
}};
($type:ty, $x:literal $(minutes)?$(minute)?$(mins)?$(min)?$(m)?) => {{
let x: $type = $x * ($crate::MINUTE as $type);
x
}};
($type:ty, $x:literal $(hours)?$(hour)?$(hrs)?$(hr)?$(h)?) => {{
let x: $type = $x * ($crate::HOUR as $type);
x
}};
($type:ty, $x:literal $(days)?$(day)?$(d)?) => {{
let x: $type = $x * ($crate::DAY as $type);
x
}};
($type:ty, $x:literal $(weeks)?$(week)?$(w)?) => {{
let x: $type = $x * ($crate::WEEK as $type);
x
}};
($type:ty, $x:literal $(years)?$(year)?$(yrs)?$(yr)?$(y)?) => {{
let x: $type = $x * ($crate::YEAR as $type);
x
}};
}
pub fn ms_into_time(s: &str) -> Result<Duration, Error> {
let milliseconds = ms(s)?;
if milliseconds < 0 {
return Err(Error::new("Time Duration cannot work with negative values"));
}
Ok(Duration::from_millis(milliseconds as u64))
}
#[derive(Debug)]
pub struct Error {
message: &'static str,
}
impl Error {
pub fn new(message: &'static str) -> Error {
Error { message }
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for Error {}
#[cfg(test)]
mod tests;