Skip to main content

datetime_to_f64

Function datetime_to_f64 

Source
pub fn datetime_to_f64<TZ>(dt: &DateTime<TZ>) -> f64
where TZ: TimeZone,
Expand description

Converts a chrono::DateTime to an f64 timestamp representation.

This function converts a datetime with any timezone to a floating-point timestamp where the integer part represents seconds since Unix epoch and the fractional part represents nanoseconds as a decimal fraction.

§Arguments

  • dt - A reference to a DateTime with any timezone

§Returns

Returns an f64 timestamp where:

  • Integer part: seconds since Unix epoch (1970-01-01 00:00:00 UTC)
  • Fractional part: nanoseconds expressed as a decimal (e.g., 0.123456789)

§Example

use chrono::{DateTime, Utc, TimeZone, Timelike};
use strand_datetime_conversion::datetime_to_f64;

let dt = Utc.with_ymd_and_hms(2023, 12, 25, 15, 30, 45).unwrap()
    .with_nanosecond(123456789).unwrap();
let timestamp = datetime_to_f64(&dt);
assert!( (timestamp - 1703518245.123456789).abs() < 1e-9 );