millis-1.0.0 has been yanked.
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'
// Use long format
ms // '1 minute'
ms // '2 minutes'
ms // '2 days'
ms // '10 hours'
API
ms(value, options)
Parse or format the given value.
Parameters:
value(Value::String(String)|Value::Number(i64)): The string or number to convertoptions(Option<Options>): Optional settings. UseSome(Options { long: true })for verbose formatting
Returns:
- If
valueisValue::String, returnsValue::Number(i64)with milliseconds - If
valueisValue::Number, returnsValue::String(String)with formatted time
Errors:
- Returns
Err(String)if value cannot be parsed or formatted
Example:
use ;
// Parse string
let result = ms?;
match result
// Format number
let result = ms?;
match result
// Long format
let result = ms?;
match result
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 invalid or cannot be parsed
Example:
use parse;
let ms = parse?; // 7200000
let ms = parse?; // 86400000
let ms = parse?; // 10000
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
Example:
use ;
let s = format; // "1m"
let s = format; // "1 minute"
let s = format; // "1h"
Import Options
// Import main function
use ms;
// Import specific functions
use ;
// Import types
use ;
// 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
- 🦀 Zero dependencies (except
regex) - 🔄 Bidirectional conversion (string ↔ milliseconds)
- ⏱️ Supports negative time values
- 📝 Long and short format options
- 🎯 Type-safe with Rust's type system
- 🔥 Cached regex compilation for better performance
- ✅ Comprehensive error handling
Common Use Cases
Setting Timeouts
use thread;
use Duration;
use parse;
// Convert to Duration for thread::sleep()
let timeout = parse?;
sleep;
Async Operations
use ;
use parse;
async
Caching
use ;
use parse;
// Set cache expiration
let cache_duration = parse?;
let now = now
.duration_since?
.as_millis as i64;
let expires_at = now + cache_duration;
Rate Limiting
use parse;
// Define rate limit window
let rate_limit_window = parse?;
let max_requests = 100;
println!;
Calculating Durations
use parse;
// Calculate time differences
let meeting_duration = parse? - parse?; // 5400000 ms (1.5 hours)
println!;
Error Handling
The library returns Result types for proper error handling:
use ;
// Invalid format
match parse
// Empty string
match parse
// String too long (>100 characters)
match parse
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