millis
A tiny Rust library that converts various time formats to milliseconds.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Usage
use ms;
// Parse time strings to milliseconds
ms? // 172800000
ms? // 86400000
ms? // 36000000
ms? // 9000000
ms? // 7200000
ms? // 60000
ms? // 5000
ms? // 31557600000
ms? // 100
ms? // -259200000
ms? // -3600000
ms? // -200
// Format milliseconds to strings
ms? // "1m"
ms? // "2m"
ms? // "-3m"
ms? // "2d"
Long Format
For verbose output, use the format() function with options:
use ;
format // "1 minute"
format // "2 minutes"
format // "2 days"
// Combine parse and format
let ms_value = ms?;
format // "10 hours"
API
ms(value)
Parse or format the given value. The return type is automatically inferred based on the input type.
Parameters:
value: Can be&str,String(to parse), ori64(to format)
Returns:
Result<i64, String>when input is a string (parsing to milliseconds)Result<String, String>when input is a number (formatting to time string)
Errors:
- Returns
Err(String)if the value cannot be parsed
Examples:
use ms;
// Parse string to milliseconds
let milliseconds: i64 = ms?;
assert_eq!;
let milliseconds = ms?;
assert_eq!;
// Format milliseconds to string
let formatted: String = ms?;
assert_eq!;
let formatted = ms?;
assert_eq!;
parse(value)
Parse the given string and return milliseconds.
Parameters:
value(&str): A string to parse to milliseconds
Returns:
Result<i64, String>: The parsed value in milliseconds
Errors:
- Returns
Errif the string is empty, longer than 100 characters, or cannot be parsed
Examples:
use parse;
let ms = parse?; // 7200000
let ms = parse?; // 86400000
let ms = parse?; // 10000
let ms = parse?; // -3600000
format(ms_value, options)
Format the given milliseconds as a string.
Parameters:
ms_value(i64): Milliseconds to formatoptions(Option<Options>): UseSome(Options { long: true })for verbose formatting
Returns:
String: The formatted string
Examples:
use ;
let s = format; // "1m"
let s = format; // "1 minute"
let s = format; // "1h"
let s = format; // "-1h"
Import Options
// Import main function
use ms;
// Import specific functions
use ;
// Import types
use Options;
// Import everything
use ;
Supported Time Units
Short Format
ms,msec,msecs,millisecond,milliseconds- Millisecondss,sec,secs,second,seconds- Secondsm,min,mins,minute,minutes- Minutesh,hr,hrs,hour,hours- Hoursd,day,days- Daysw,week,weeks- Weeksmo,month,months- Months (calculated as 1/12 of a year)y,yr,yrs,year,years- Years (calculated as 365.25 days)
Case Insensitive
All units are case-insensitive, so 1D, 1d, 1 Day, 1 DAY are all equivalent.
Features
- 🚀 Simple and intuitive API
- 🦀 Minimal dependencies (only
regex) - 🔄 Bidirectional conversion (string ↔ milliseconds)
- ⏱️ Supports negative time values
- 📝 Long and short format options
- 🎯 Type-safe with Rust's type system and trait-based design
- 🔥 Cached regex compilation for better performance
- ✅ Comprehensive error handling
Common Use Cases
Setting Timeouts
use thread;
use Duration;
use ms;
// Convert to Duration for thread::sleep()
let timeout = ms?;
sleep;
Async Operations
use ;
use ms;
async
Caching
use ;
use ms;
// Set cache expiration
let cache_duration = ms?;
let now = now
.duration_since?
.as_millis as i64;
let expires_at = now + cache_duration;
Rate Limiting
use ms;
// Define rate limit window
let rate_limit_window = ms?;
let max_requests = 100;
println!;
Calculating Durations
use ms;
// Calculate time differences
let meeting_duration = ms? - ms?; // 5400000 ms (1.5 hours)
println!;
Working with Configuration Files
use ms;
use ;
Error Handling
The library returns Result types for proper error handling:
use ms;
// Invalid format
match ms
// Empty string
match ms
// String too long (>100 characters)
match ms
Notes
Precision
- Month calculation: 1 month = 1/12 year ≈ 30.44 days (average value)
- Year calculation: 1 year = 365.25 days (accounting for leap years)
- Return type: All parsed values are returned as
i64(no decimal points)
Rounding
When parsing, decimal values are rounded to the nearest integer millisecond:
use parse;
assert_eq!; // 1.5 * 1000 = 1500
assert_eq!; // 0.5 rounds to 1
When formatting, values are rounded to the nearest integer for the selected unit:
use format;
assert_eq!; // rounded from 1.5s
assert_eq!; // rounded from 1.5m
Advanced Usage
Custom Type Conversion
The library uses the ToMillis trait for type conversion. You can extend it for custom types:
use ToMillis;
// Note: The trait is used internally by the ms() function
// For custom types, use parse() and format() directly