feat: add time unit conversion #996
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds a new module for converting between different time units (seconds, minutes, hours, days, weeks, months, and years).
Features Added
convert_time: Convert time values between any two supported unitsTimeUnitenum for type-safe time unit representationType of Change
Testing
All tests pass:
cargo test time_units::testsResults:
Algorithm Complexity
Implementation Details
Conversion Algorithm
Time Unit Values
All conversions use these standardized values:
Enum-Based Design
TimeUnitenum instead of string dictionaries for type safetyCopytrait for efficient passingCase Handling
TimeUnit::from_str()normalizes input with.to_lowercase()"HOURS","Hours","hours", etc.Error Handling
Returns
Result<f64, String>with descriptive error messages:"'time_value' must be a non-negative number."NaNandInfinityvalues"Invalid unit {unit} is not in seconds, minutes, hours, days, weeks, months, years."Example error handling:
References
Checklist
TimeUnit)src/conversions/mod.rsExample Usage
Additional Notes
std