speedate
speedate copied to clipboard
Add TimestampInterpretation for flexible timestamp parsing
Description
I added a new TimestampInterpretation enum and updates the TimeConfig to allow us specify how timestamps should be interpreted. This change provides more flexibility in parsing timestamps, especially for systems that may receive timestamps in different formats.
Changes
- Added
TimestampInterpretationenum withAutoandAlwaysSecondsvariants. - Updated
TimeConfigto include atimestamp_interpretationfield. - Modified
TimeConfigBuilderto allow setting thetimestamp_interpretation. - Updated
DateTime::from_timestamp_with_configto handle different interpretation modes.
How to Use
Setting up TimeConfig with TimestampInterpretation
Anyone can now specify how they want timestamps to be interpreted when creating a TimeConfig:
use speedate::{TimeConfigBuilder, TimestampInterpretation};
// Auto mode (default behavior)
let auto_config = TimeConfigBuilder::new()
.timestamp_interpretation(TimestampInterpretation::Auto)
.build();
// Always interpret as seconds
let always_seconds_config = TimeConfigBuilder::new()
.timestamp_interpretation(TimestampInterpretation::AlwaysSeconds)
.build();
// This will be interpreted as milliseconds in `Auto` mode
let auto_dt = DateTime::from_timestamp_with_config(1654619320123, 0, &auto_config).unwrap();
assert_eq!(auto_dt.to_string(), "2022-06-07T16:28:40.123000");
// This will be interpreted as seconds in `AlwaysSeconds` mode
// Please Take Note: This might result in a `DateTooLarge` error for very large timestamps
let always_seconds_dt = DateTime::from_timestamp_with_config(1654619320, 0, &always_seconds_config).unwrap();
assert_eq!(always_seconds_dt.to_string(), "2022-06-07T16:28:40");
Notes
- The
Automode behaves like the previous implementation, automatically detecting if a timestamp is in seconds or milliseconds based on its magnitude. - The
AlwaysSecondsmode always interprets the timestamp as seconds, which may result inDateTooLargeerrors for timestamps that would previously have been interpreted as milliseconds.
Codecov Report
Attention: Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/time.rs | 97.14% | 1 Missing :warning: |
:loudspeaker: Thoughts on this report? Let us know!
I believe #84 has solved this same problem, will close here. Thanks again.