#ms #milliseconds #converter


Fast abstraction for converting human-like times into milliseconds

21 releases (8 stable)

1.4.0 May 2, 2020
1.2.1 Apr 28, 2020
1.1.1 Mar 28, 2020
0.7.4 Mar 24, 2020

#221 in Date and time

Download history 21/week @ 2023-08-04 21/week @ 2023-08-11 48/week @ 2023-08-18 34/week @ 2023-08-25 11/week @ 2023-09-01 71/week @ 2023-09-08 18/week @ 2023-09-15 15/week @ 2023-09-22 18/week @ 2023-09-29 31/week @ 2023-10-06 25/week @ 2023-10-13 28/week @ 2023-10-20 68/week @ 2023-10-27 36/week @ 2023-11-03 57/week @ 2023-11-10 31/week @ 2023-11-17

197 downloads per month
Used in updatehub-sdk

MIT license

512 lines

MS converter library

Fast abstraction for converting human-like times into milliseconds. Like, are 1d to 86400000.

There are two ways to calculate milliseconds:

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

Getting Started


Add this to your Cargo.toml:

ms-converter = "1.4"


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


You can check the performance diff between ms_converter and ms libraries here.

Also, the macro has no time in the runtime! It will be converted into the const value.

Running the tests

Unit tests

cargo test

Coding style tests

Running code style tests

cargo fmt --all -- --check


Just create pr or issue. You welcome.


We use SemVer for versioning. For the versions available, see the tags on this repository.


  • Mikhail Panfilov - Initial work - Mnwa

See also the list of contributors who participated in this project.


This project is licensed under the MIT License - see the LICENSE.md file for details

No runtime deps