simple_duration
simple_duration is a crate that provides a "simple and minimal dependency" second-precision Duration type for Rust.
It is optimized for everyday use with hours, minutes, and seconds, and is suitable for embedded (no_std) environments.
Features
-
Simple time representation in seconds
Specialized for use cases where high precision like milliseconds or nanoseconds is not needed. -
Intuitive creation and formatting
Easily create from hours, minutes, and seconds, and convert to"hh:mm:ss"format strings. -
String parsing support
Create Duration objects from"hh:mm:ss"format strings. -
Addition and subtraction operations
Add and subtract Duration objects (results never become negative). -
SystemTime integration
Create Duration from twoSystemTimevalues (whenstdfeature is enabled). -
no_std support & minimal dependencies
Safe to use in embedded projects or projects that want to minimize dependencies. -
Safe error handling
Failures like string parsing return explicit errors via Option/Result without panicking.
Serde support (JSON serialization/deserialization)
Enable the serde feature to serialize/deserialize Duration to/from JSON and other formats.
- Well-tested and documented
Includes tests and documentation for quality assurance.
Usage Example
use Duration;
// Create from hours, minutes, seconds
let duration = from_hms; // 1 hour 30 minutes 45 seconds
// Create from hours
let duration = from_hours; // 2 hours
// Create from minutes
let duration = from_minutes; // 90 minutes (1 hour 30 minutes)
// Create from seconds
let duration = from_seconds; // 1 hour 1 minute 1 second
// Create from string
let duration = parse.unwrap;
// Format
println!; // "01:30:45"
// Get total amounts in each unit
assert_eq!;
assert_eq!; // 90 minutes
assert_eq!; // 1 hour (truncated)
// Get each component (in h:m:s format)
assert_eq!; // seconds component (0-59)
assert_eq!; // minutes component (0-59)
assert_eq!; // hours component
// Arithmetic operations
let d1 = from_seconds;
let d2 = from_seconds;
let sum = d1 + d2; // 150 seconds
let diff = d1 - d2; // 50 seconds
// --- Serde integration example (JSON save/load) ---
SystemTime integration (when std feature is enabled)
use Duration;
use SystemTime;
let start = now;
// Some processing...
let end = now;
if let Some = from_system_time_diff
Installation
Add the following to your Cargo.toml:
[]
= "0.1"
Enable serde support
If you want to serialize/deserialize Duration to/from JSON or other formats, enable the serde feature:
[]
= { = "0.1", = ["serde"] }
Usage in no_std environments
If you want to use it in a no_std environment, disable the default feature:
[]
= { = "0.1", = false }
Intended Use Cases
- Time management in embedded/IoT devices
- Simple timers and countdowns
- Web apps or CLI tools where second precision is sufficient
- When you don't need the precision or features of the standard
Durationorchrono
API Documentation
Creation Methods
Duration::from_seconds()- Create from secondsDuration::from_minutes()- Create from minutesDuration::from_hours()- Create from hoursDuration::from_hms()- Create from hours, minutes, secondsDuration::parse()- Parse from string
Get total amount in each unit
Duration::as_seconds()- Get total secondsDuration::as_minutes()- Get total minutes (truncated)Duration::as_hours()- Get total hours (truncated)
Get each component (h:m:s format)
Duration::seconds_part()- Get seconds component (0-59)Duration::minutes_part()- Get minutes component (0-59)Duration::hours_part()- Get hours component (0-∞)
Others
Duration::format()- Format as "hh:mm:ss"
Development & Testing
# Run tests
# Run tests (no_std)
# Generate documentation
# Format
# Lint
License
MIT OR Apache-2.0
Contribution
Please report bugs or feature requests via GitHub Issues. Pull requests are also welcome.