Parses a relative time string and returns a `Duration`

Overview

humantime_to_duration

Crates.io License CodeCov

A Rust crate for parsing human-readable relative time strings and converting them to a Duration.

Features

  • Parses a variety of human-readable time formats.
  • Supports positive and negative durations.
  • Allows for chaining time units (e.g., "1 hour 2 minutes" or "2 days and 2 hours").

Usage

Add this to your Cargo.toml:

[dependencies]
humantime_to_duration = "0.1.1"

Then, import the crate and use the from_str function:

use humantime_to_duration::from_str;
use time::Duration;

let duration = from_str("+3 days");
assert_eq!(duration.unwrap(), Duration::days(3));

Supported Formats

The from_str function supports the following formats for relative time:

  • num unit (e.g., "-1 hour", "+3 days")
  • unit (e.g., "hour", "day")
  • "now" or "today"
  • "yesterday"
  • "tomorrow"
  • use "ago" for the past
  • combined units with "and" or "," (e.g., "2 years and 1 month", "1 day, 2 hours" or "2 weeks 1 second")

num can be a positive or negative integer. unit can be one of the following: "fortnight", "week", "day", "hour", "minute", "min", "second", "sec" and their plural forms.

Return Values

The from_str function returns:

  • Ok(Duration) - If the input string can be parsed as a relative time
  • Err(ParseDurationError) - If the input string cannot be parsed as a relative time

This function will return Err(ParseDurationError::InvalidInput) if the input string cannot be parsed as a relative time.

Fuzzer

To run the fuzzer:

$ cargo fuzz run fuzz_from_str

License

This project is licensed under the MIT License.

You might also like...
Compile-time stuff and other goodies for rustaceans 🦀

🥯 bagel: Always baked, never fried bagel is a collection of macros and other things that we frequently use at Skytable, primarily to get work done at

Rust crate: Overloaded Literals to construct your datatypes without boilerplate and with compile-time validation.

overloaded_literals   Overloaded Literals to construct your datatypes without boilerplate and with compile-time validation. Features Compile-time vali

Mindful Time Tracking: Simplify Your Focus and Boost Productivity Effortlessly.
Mindful Time Tracking: Simplify Your Focus and Boost Productivity Effortlessly.

Mindful Time Tracking: Simplify Your Focus and Boost Productivity Effortlessly. About pace is a mindful productivity tool designed to help you keep tr

Safe, comp time generated queries in rust

query_builder For each struct field following methods will be generated. All fields where_FIELDNAME_eq Numeric fields where_FIELDNAME_le where_FIELDNA

A real-time mixer

Pagana Pagana is a real-time mixer. This project is still in early stages of development and is not ready for any kind of production use or testing. D

Compile time static maps for Rust

Rust-PHF Documentation Rust-PHF is a library to generate efficient lookup tables at compile time using perfect hash functions. It currently uses the C

Isn't it time to be a bit nicer to rustc?

politeness-macro Aren't we all too rude to computers? Isn't it time to bring a bit more politeness into our programming? Shouldn't we be a bit nicer t

Simple time handling in Rust

time Documentation: latest release main branch book Minimum Rust version policy The time crate is guaranteed to compile with any release of rustc from

A real-time data backend for browser-based applications.

DriftDB DriftDB is a real-time data backend for browser-based applications. For more information, see driftdb.com. Structure of this repo docs/: main

Owner
null
A framebuffer that takes a `&[bool]` slice and returns 2x4 dot (pixel) braille `char`s

A framebuffer that takes a &[bool] slice and returns 2x4 "dot" (pixel) braille chars.

Odin Dutton 3 Mar 28, 2022
🚀simple server that returns error codes with their respective messages and debug information, written in rust 🦀

ErrorServer ?? A simple & lightweight server that returns a HTML page of the error code with its respective message and debug information, written in

Jakob 2 Dec 15, 2022
Simple string matching with questionmark- and star-wildcard operator

wildmatch Match strings against a simple wildcard pattern. Tests a wildcard pattern p against an input string s. Returns true only when p matches the

Armin Becher 38 Dec 18, 2022
Compact, clone-on-write vector and string.

ecow Compact, clone-on-write vector and string. Types An EcoVec is a reference-counted clone-on-write vector. It takes up two words of space (= 2 usiz

Typst 78 Apr 18, 2023
Stack heap flexible string designed to improve performance for Rust

flexible-string A stack heap flexible string designed to improve performance. FlexibleString was first implemented in spdlog-rs crate, which improved

Sprite 6 Feb 9, 2022
A crate for converting an ASCII text string or file to a single unicode character

A crate for converting an ASCII text string or file to a single unicode character. Also provides a macro to embed encoded source code into a Rust source file. Can also do the same to Python code while still letting the code run as before by wrapping it in a decoder.

Johanna Sörngård 17 Dec 31, 2022
Rust crate for obfuscating string literals.

Obfustring This crate provides a obfuscation macro for string literals. This makes it easy to protect them from common reverse engineering attacks lik

null 7 Mar 1, 2023
A special rope, designed to work with any data type that is not String

AnyRope AnyRope is an arbitrary data type rope for Rust, designed for similar operations that a rope would do, but targeted at data types that are not

ahoyiski 27 Mar 22, 2023
Idiomatic Rust implementations for various Windows string types (like UNICODE_STRING)

nt-string by Colin Finck <[email protected]> Provides idiomatic Rust implementations for various Windows string types: NtUnicodeString (with NtUnicode

Colin Finck 5 Jun 4, 2023
Rust based magic-string with source map chains support

enhanced-magic-string Rust implementation of https://www.npmjs.com/package/magic-string with original sourcemap chain support. license. This project i

Farm 3 Nov 5, 2023