pydantic / speedate

Fast and simple datetime, date, time and duration parsing for rust.
https://docs.rs/speedate/latest/speedate/
MIT License
199 stars 17 forks source link

Add TimestampInterpretation for flexible timestamp parsing #66

Open doziestar opened 3 months ago

doziestar commented 3 months ago

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

codecov[bot] commented 3 months ago

Codecov Report

Attention: Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review. Files Patch % Lines
src/time.rs 97.14% 1 Missing :warning:

:loudspeaker: Thoughts on this report? Let us know!