Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ version = "2.2.0"
edition = "2021"
categories = ["date-and-time"]

[features]
mu = []

[dev-dependencies]
bencher = "0.1.5"
time = { version = "0.3", features = ["formatting"] }
Expand Down
30 changes: 23 additions & 7 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl fmt::Display for Error {
write!(
f,
"unknown time unit {:?}, \
supported units: ns, us, ms, sec, min, hours, days, \
supported units: ns, us/µs, ms, sec, min, hours, days, \
weeks, months, years (and few variations)",
unit
)
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Parser<'_> {
fn parse_unit(&mut self, n: u64, start: usize, end: usize) -> Result<(), Error> {
let (mut sec, nsec) = match &self.src[start..end] {
"nanos" | "nsec" | "ns" => (0u64, n),
"usec" | "us" => (0u64, n.mul(1000)?),
"usec" | "us" | "µs" => (0u64, n.mul(1000)?),
"millis" | "msec" | "ms" => (0u64, n.mul(1_000_000)?),
"seconds" | "second" | "secs" | "sec" | "s" => (n, 0),
"minutes" | "minute" | "min" | "mins" | "m" => (n.mul(60)?, 0),
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Parser<'_> {
.ok_or(Error::NumberOverflow)?;
}
c if c.is_whitespace() => {}
'a'..='z' | 'A'..='Z' => {
'a'..='z' | 'A'..='Z' | 'µ' => {
break;
}
_ => {
Expand All @@ -180,7 +180,7 @@ impl Parser<'_> {
continue 'outer;
}
c if c.is_whitespace() => break,
'a'..='z' | 'A'..='Z' => {}
'a'..='z' | 'A'..='Z' | 'µ' => {}
_ => {
return Err(Error::InvalidCharacter(off));
}
Expand All @@ -202,7 +202,7 @@ impl Parser<'_> {
/// span is an integer number and a suffix. Supported suffixes:
///
/// * `nsec`, `ns` -- nanoseconds
/// * `usec`, `us` -- microseconds
/// * `usec`, `us`, `µs` -- microseconds
/// * `msec`, `ms` -- milliseconds
/// * `seconds`, `second`, `sec`, `s`
/// * `minutes`, `minute`, `min`, `m`
Expand Down Expand Up @@ -314,6 +314,9 @@ impl fmt::Display for FormattedDuration {
item(f, started, "m", minutes as u32)?;
item(f, started, "s", seconds as u32)?;
item(f, started, "ms", millis)?;
#[cfg(feature = "mu")]
item(f, started, "µs", micros)?;
#[cfg(not(feature = "mu"))]
item(f, started, "us", micros)?;
item(f, started, "ns", nanosec)?;
Ok(())
Expand All @@ -337,6 +340,7 @@ mod test {
assert_eq!(parse_duration("33ns"), Ok(Duration::new(0, 33)));
assert_eq!(parse_duration("3usec"), Ok(Duration::new(0, 3000)));
assert_eq!(parse_duration("78us"), Ok(Duration::new(0, 78000)));
assert_eq!(parse_duration("163µs"), Ok(Duration::new(0, 163000)));
assert_eq!(parse_duration("31msec"), Ok(Duration::new(0, 31_000_000)));
assert_eq!(parse_duration("31millis"), Ok(Duration::new(0, 31_000_000)));
assert_eq!(parse_duration("6ms"), Ok(Duration::new(0, 6_000_000)));
Expand Down Expand Up @@ -482,11 +486,23 @@ mod test {
assert_eq!(
parse_duration("10nights").unwrap_err().to_string(),
"unknown time unit \"nights\", supported units: \
ns, us, ms, sec, min, hours, days, weeks, months, \
ns, us/µs, ms, sec, min, hours, days, weeks, months, \
years (and few variations)"
);
}

#[cfg(feature = "mu")]
#[test]
fn test_format_micros() {
assert_eq!(format_duration(Duration::from_micros(123)).to_string(), "123µs");
}

#[cfg(not(feature = "mu"))]
#[test]
fn test_format_micros() {
assert_eq!(format_duration(Duration::from_micros(123)).to_string(), "123us");
}

#[test]
fn test_error_cases() {
assert_eq!(
Expand All @@ -506,6 +522,6 @@ mod test {
"invalid character at 2"
);
assert_eq!(parse_duration("222nsec221nanosmsec7s5msec572s").unwrap_err().to_string(),
"unknown time unit \"nanosmsec\", supported units: ns, us, ms, sec, min, hours, days, weeks, months, years (and few variations)");
"unknown time unit \"nanosmsec\", supported units: ns, us/µs, ms, sec, min, hours, days, weeks, months, years (and few variations)");
}
}