:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(hogvm): parse datetimes at millisecond precision to match node (#68934)
José Sequeira52860feb2e9f
diff --git a/rust/common/hogvm/src/stl.rs b/rust/common/hogvm/src/stl.rsindex c37f2da15b37..213c7c5463cf 100644--- a/rust/common/hogvm/src/stl.rs+++ b/rust/common/hogvm/src/stl.rs@@ -1614,9 +1614,13 @@ fn naive_to_seconds(naive: NaiveDateTime, zone: Option<&str>) -> Result<f64, VmE}}-/// `f64` epoch seconds with sub-second precision, matching Python's `datetime.timestamp()`.+/// `f64` epoch seconds at millisecond precision, matching the reference VM (luxon+/// `DateTime.fromISO(...).toSeconds()`), which parses to milliseconds and truncates finer digits.+/// The Node HogVM is the ingestion shadow baseline, so preserving microseconds (as Python's+/// `datetime.timestamp()` does) surfaced as a `result_mismatch` on any sub-millisecond event+/// timestamp — e.g. `toUnixTimestamp(toDateTime(event.timestamp))` in a "first seen" transform.fn datetime_to_seconds<Tz: TimeZone>(dt: DateTime<Tz>) -> f64 {- dt.timestamp() as f64 + f64::from(dt.timestamp_subsec_nanos()) / 1_000_000_000.0+ dt.timestamp_millis() as f64 / 1000.0}// Extract every element as a Num and return them sorted ascending. Single allocation + early error,diff --git a/rust/common/hogvm/tests/datetime_millis.rs b/rust/common/hogvm/tests/datetime_millis.rsnew file mode 100644index 000000000000..78fc6007861f--- /dev/null+++ b/rust/common/hogvm/tests/datetime_millis.rs@@ -0,0 +1,42 @@+//! `toDateTime`/`toUnixTimestamp` parse strings at millisecond precision, matching the reference+//! VM (luxon `fromISO(...).toSeconds()`). Preserving microseconds — as Python's+//! `datetime.timestamp()` does — made `toUnixTimestamp(toDateTime(event.timestamp))` diverge from+//! the Node shadow on any sub-millisecond timestamp (the dominant EU `result_mismatch` bucket).++use hogvm::{sync_execute, ExecutionContext, Program};+use serde_json::{json, Value};++const OP_CALL_GLOBAL: i64 = 2;+const OP_STRING: i64 = 32;+const OP_RETURN: i64 = 38;++fn unix_ts(input: &str) -> Value {+ let bc = vec![+ json!("_H"),+ json!(1),+ json!(OP_STRING),+ json!(input),+ json!(OP_CALL_GLOBAL),+ json!("toDateTime"),+ json!(1),+ json!(OP_CALL_GLOBAL),+ json!("toUnixTimestamp"),+ json!(1),+ json!(OP_RETURN),+ ];+ let program = Program::new(bc).expect("valid program");+ sync_execute(&ExecutionContext::with_defaults(program), false).expect("execution succeeds")+}++#[test]+fn sub_millisecond_precision_is_truncated_to_millis() {+ // Microseconds are dropped: `.123456` -> `.123`, matching luxon / JS `Date.parse`.+ assert_eq!(+ unix_ts("2026-07-07T12:00:00.123456Z"),+ json!(1783425600.123)+ );+ // Millisecond input is preserved exactly.+ assert_eq!(unix_ts("2026-07-07T12:00:00.123Z"), json!(1783425600.123));+ // Whole seconds stay whole.+ assert_eq!(unix_ts("2026-07-07T12:00:00Z"), json!(1783425600.0));+}