speedate icon indicating copy to clipboard operation
speedate copied to clipboard

Add TimestampInterpretation for flexible timestamp parsing

Open doziestar opened this issue 1 year ago • 1 comments

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

  1. Added TimestampInterpretation enum with Auto and AlwaysSeconds variants.
  2. Updated TimeConfig to include a timestamp_interpretation field.
  3. Modified TimeConfigBuilder to allow setting the timestamp_interpretation.
  4. Updated DateTime::from_timestamp_with_config to 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 Auto mode behaves like the previous implementation, automatically detecting if a timestamp is in seconds or milliseconds based on its magnitude.
  • The AlwaysSeconds mode always interprets the timestamp as seconds, which may result in DateTooLarge errors for timestamps that would previously have been interpreted as milliseconds.

doziestar avatar Jun 25 '24 16:06 doziestar

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!

codecov[bot] avatar Jun 25 '24 16:06 codecov[bot]

I believe #84 has solved this same problem, will close here. Thanks again.

davidhewitt avatar Jun 16 '25 11:06 davidhewitt