i64
(PackedTimestamp
).
Each timestamp component uses the minimal number of bits, leaving enough bits for
arbitrary timezone offsets in minutes and enough range for years from -9999 to 9999.
This is a useful storage format if the timestamps are only parsed, formatted or compared. Parsing uses SSE instructions when compiled for a target that supports them. There is a special fast-path when the millisecond uses 3 digits and the timezone is UTC. Without SSE a hand-written recursive descent parser is used.
rust
assert_eq!(
"2022-08-21T17:30:15.250Z".parse(),
Ok(PackedTimestamp::new_utc(2022, 8, 21, 17, 30, 15, 250))
);
assert_eq!(
"2022-08-21T17:30:15.25Z".parse(),
Ok(PackedTimestamp::new_utc(2022, 8, 21, 17, 30, 15, 250))
);
assert_eq!(
"2022-08-21 17:30:15.1Z".parse(),
Ok(PackedTimestamp::new_utc(2022, 8, 21, 17, 30, 15, 100))
);
assert_eq!(
"2022-08-21 17:30:15Z".parse(),
Ok(PackedTimestamp::new_utc(2022, 8, 21, 17, 30, 15, 0))
);
Note that formatting currently ignores the timezone offset and always writes a Z
as the offset.
Milliseconds are always included and printed using 3 digits.
rust
assert_eq!(
PackedTimestamp::new_utc(2022, 8, 21, 17, 30, 15, 100).to_string(),
"2022-08-21T17:30:15.100Z".to_owned()
);
assert_eq!(
PackedTimestamp::new_utc(2022, 8, 21, 17, 30, 15, 123).to_string(),
"2022-08-21T17:30:15.123Z".to_owned()
);
assert_eq!(
PackedTimestamp::new_utc(2022, 8, 21, 17, 30, 15, 250).to_string(),
"2022-08-21T17:30:15.250Z".to_owned()
);
The date_trunc
and date_add_month
kernels are written in a way that the compiler can auto-vectorize when used in a loop.
```rust asserteq!(datetruncyeartimestampmillis(1658765238000), 1640995200000); asserteq!(datetruncmonthtimestampmillis(1658765238000), 1656633600000);
asserteq!(dateaddmonthtimestampmillis(1661102969000, 1), 1663718400000); asserteq!(dateaddmonthtimestampmillis(1661102969000, 12), 1692576000000); ```
The package net.jhorstmann:packedtime implements the same packed layout for Java.