Crate ms_converter

Source
Expand description

Fast abstraction for converting human-like times into milliseconds.

There are two ways to calculate milliseconds:

  • In the runtime crate::ms_converter::ms
  • In the compilation time crate::ms_converter::ms_expr

§Usage

§Running ms converter in Runtime:

use crate::ms_converter::ms;

let value = ms("1d").unwrap();
assert_eq!(value, 86400000)

§Convert ms in the compilation step:

use crate::ms_converter::ms_expr;

const VALUE: i64 = ms_expr!(i64, 1 d);
assert_eq!(VALUE, 86400000)

§Convert ms into time.Duration

use crate::ms_converter::ms_into_time;

let value = ms_into_time("1d").unwrap();
assert_eq!(value.as_millis(), 86400000)

§Convert milliseconds into human-like time string

use crate::ms_converter::{get_duration_by_postfix, DAY};

let value = get_duration_by_postfix(DAY as i64, " day").unwrap();
assert_eq!(value, "1 day")

§Convert milliseconds into human-like time string without postfix

use crate::ms_converter::{get_max_possible_duration, DAY};

let value = get_max_possible_duration(DAY as i64).unwrap();
assert_eq!(value, "1d")

§Convert milliseconds into long human-like time string without postfix

use crate::ms_converter::{get_max_possible_duration_long, WEEK};

let value = get_max_possible_duration_long(2 * WEEK as i64).unwrap();
assert_eq!(value, "14 days") // Max possible period is a day

§Supported time strings

  • Years: years, year, yrs, yr, y
  • Weeks: weeks, week, w
  • Days: days, day, d
  • Hours: hours, hour, hrs, hr, h
  • Minutes: minutes, minute, mins, min, m
  • Seconds: seconds, second, secs, sec, s
  • Milliseconds: milliseconds, millisecond, msecs, msec, ms and empty postfix

Macros§

ms_expr
Zero cost converter from human-like time into a number. In the first argument, you need to pass type of your number (i64, f64 and etc). The second argument is human-time construction, like 1 day, 2 h. The output will be a number with type what you set in the first argument.

Structs§

Error
Error which return ms_converter functions in runtime, if something is going wrong.

Constants§

DAY
How many milliseconds in one day
HOUR
How many milliseconds in one hour
MINUTE
How many milliseconds in one minute
SECOND
How many milliseconds in one second
WEEK
How many milliseconds in one week
YEAR
How many milliseconds in one year

Functions§

get_duration_by_postfix
Getting human-like time from milliseconds. get_duration_by_postfix function gets a milliseconds count and str slice or String as postfix and returns a string with your time.
get_max_possible_duration
Getting human-like time from milliseconds. get_max_possible_duration function gets a milliseconds count and returns a max possible string with your time. get_max_possible_duration has some limitations maximum of avalable postfixes is a day.
get_max_possible_duration_long
Getting human-like time from milliseconds. get_max_possible_duration_long function gets a milliseconds count and returns a max possible string with your time. get_max_possible_duration_long has some limitations maximum of avalable postfixes is a day.
ms
Fast abstraction for converting human-like times into milliseconds. ms function gets an str slice or String and returns how much milliseconds in your pattern.
ms_into_time
Ms into time is the abstraction on ms function, which converts result into time.Duration type. ms_into_time function gets an str slice or String and returns time.Duration. ms_into_time has some limitations, it’s not working with negative values: