Function datetime_to_timestamp

Source
pub fn datetime_to_timestamp(dt: DateTime<Utc>) -> f64
Expand description

Converts a DateTime to a f64, dealing with sub-microsecond float inaccuracies.

f64s cannot store nanoseconds. To verify this just try to fit the current timestamp in nanoseconds into a 52-bit number (which is the significand of a double).

Round off to microseconds to not show more decimal points than we know are correct. Anything else might trick the user into thinking the nanoseconds in those timestamps mean anything.

This needs to be done regardless of whether the input value was a ISO-formatted string or a number because it all ends up as a f64 on serialization.

If we want to support nanoseconds at some point we will probably have to start using strings everywhere. Even then it’s unclear how to deal with it in Python code as a datetime cannot store nanoseconds.

See also: timestamp_to_datetime.