parse string to std::time::Duration
Features:
- 🚀 Strong compatibility, accommodating leading or trailing whitespaces in strings.
- 👍️ Offers Playground support for online debugging.
- ⭐ Integrated with the serde library.
- 🎉 Supports parsing of various
Duration
types: - 🔥 Enables formatting of
Duration
into human-readable formats. - 🍻 Provides precise error localization for easy troubleshooting.
- ⚡ Compatible with WebAssembly (wasm).
- 🎨 Adapts to the humantime crate, despite its apparent lack of recent updates...
Performance
Suggestion: It is recommended to enable the lowercase
feature to improve performance:
- Default: Calls
to_lowercase()
each time, designed for compatibility, suitable for flexible input but with lower performance. lowercase
feature: Skips the conversion, ideal for lowercase input scenarios, offering better performance."no_calc
feature: When enabled, the parse function only parses and sums expression values without complex logic, ideal for high-performance scenarios not requiring intricate calculations.
Notice ⚠️
The default duration unit is second.Also use below duration unit
Duration Unit List
Parse string to Duration
. The String duration unit support for one
of:["y","mon","w","d","h","m","s", "ms", "µs", "ns"]
unit | Description | unit list option(one of) | example |
---|---|---|---|
y | Year | ["y" , "year" , "Y" , "YEAR" , "Year"] | 1y |
mon | Month | ["mon" , "MON" , "Month" , "month" , "MONTH"] | 1mon |
w | Week | ["w" , "W" , "Week" ,"WEEK" , "week"] | 1w |
d | Day | ["d" , "D" , "Day" , "DAY" , "day"] | 1d |
h | Hour | ["h" , "hr" , "H" , "Hour" , "HOUR" , "hour"] | 1h |
m | Minute | ["m" , "M" , "Minute" , "MINUTE" , "minute" , "min" , "MIN"] | 1m |
s | Second | ["s" , "S" , "Second" , "SECOND" , "second" , "sec" , "SEC"] | 1s |
ms | Millisecond | ["ms" , "MS" , "Millisecond" , "MilliSecond" , "MILLISECOND" , "millisecond" , "mSEC"] | 1ms |
µs | Microsecond | ["µs" , "µS" , "µsecond" , "Microsecond" , "MicroSecond" , "MICROSECOND" , "microsecond" , "µSEC"] | 1µs |
ns | Nanosecond | ["ns" , "NS" , "Nanosecond" , "NanoSecond" , "NANOSECOND" , "nanosecond" , "nSEC"] | 1ns |
Also,duration_str
support time duration simple evaluation(+,*). See example:
example
[]
= "{latest version}"
use parse;
use Duration;
deserialize in struct
deserialize to std::time::Duration
use deserialize_duration;
use *;
use Duration;
- option filed deserialize
use deserialize_option_duration;
use *;
use Duration;
Also you can use deserialize_duration_chrono
or deserialize_duration_time
function
E.g:
use Duration;
use deserialize_duration_chrono;
use *;