PivotTables: only pivot cache dates count real days from 1899-12-30

The previous commit changed the shared ISO date parser, so worksheet t="d"
cells dated before March 1900 also read one day later. The evidence for the
real-day convention comes from Excel's pivot caches only, so worksheet cells
read exactly as before again. The parser takes the date conversion as a
parameter: worksheet cells keep DATE() rules, pivot cache items use the
real-day conversion that the cache writer mirrors.
This commit is contained in:
Local contributor
2026-10-14 09:19:06 -04:00
parent b60d37e4e9
commit 0739f71fac
3 changed files with 23 additions and 16 deletions
+6 -6
View File
@@ -139,12 +139,12 @@ pub fn serial_from_ymd(sys: DateSystem, year: i64, month: i64, day: i64) -> Opti
Some(serial as f64)
}
/// The calendar date ISO 8601 text (pivot cache `<d>` items, `t="d"` cells) gives a date of a
/// date system's calendar, as [`datetime_from_serial`] returns it. ISO dates count real days:
/// from 1899-12-30 in the 1900 system and from 1904-01-01 in the 1904 system. Up to February
/// 1900 the 1900 system's own calendar, with its day 1900-01-00 and the fictitious 1900-02-29,
/// runs one day ahead of that: serial 0 (the date part of a time) is 1899-12-30, serial 1 is
/// 1899-12-31 and serial 60 is 1900-02-28. Excel writes them that way.
/// The calendar date a pivot cache's ISO 8601 date items (`<d>`, `minDate`, `startDate`…) give
/// a date of a date system's calendar, as [`datetime_from_serial`] returns it. Those dates count
/// real days: from 1899-12-30 in the 1900 system and from 1904-01-01 in the 1904 system. Up to
/// February 1900 the 1900 system's own calendar, with its day 1900-01-00 and the fictitious
/// 1900-02-29, runs one day ahead of that: serial 0 (the date part of a time) is 1899-12-30,
/// serial 1 is 1899-12-31 and serial 60 is 1900-02-28. Excel writes its caches that way.
pub fn iso_ymd(sys: DateSystem, year: i32, month: u32, day: u32) -> (i32, u32, u32) {
if sys == DateSystem::D1900 && year == 1900 && month <= 2 {
let serial = if month <= 1 { day } else { 31 + day };
+5 -2
View File
@@ -14,7 +14,7 @@
use std::collections::HashMap;
use std::fmt::Write as _;
use gridcraft_core::date::{datetime_from_serial, iso_ymd};
use gridcraft_core::date::{datetime_from_serial, iso_ymd, serial_from_iso_ymd};
use gridcraft_core::{CellRef, DateSystem, RangeRef, Value, number_to_text};
use gridcraft_formula::quote_sheet;
use gridcraft_model::{
@@ -1215,7 +1215,10 @@ fn item_label_el(e: &El, sys: DateSystem) -> String {
"m" => "(blank)".into(),
"n" => v.trim().parse::<f64>().ok().filter(|n| n.is_finite()).map(number_to_text).unwrap_or_else(|| v.to_string()),
"b" => if matches!(v.trim(), "1" | "true") { "TRUE" } else { "FALSE" }.into(),
"d" => crate::sheet_read::parse_iso_datetime(v, sys).map(number_to_text).unwrap_or_else(|| v.to_string()),
// Cache dates count real days from the epoch, as Excel writes them (see `iso`).
"d" => crate::sheet_read::parse_iso_datetime_with(v, |y, m, d| serial_from_iso_ymd(sys, y, m, d))
.map(number_to_text)
.unwrap_or_else(|| v.to_string()),
_ => v.to_string(),
}
}
+12 -8
View File
@@ -3,7 +3,7 @@
use std::collections::HashMap;
use std::sync::Arc;
use gridcraft_core::date::{serial_from_iso_ymd, time_fraction};
use gridcraft_core::date::{serial_from_ymd, time_fraction};
use gridcraft_core::{CellError, CellRef, DateSystem, MAX_COLS, MAX_ROWS, RangeRef, Value};
use gridcraft_formula::Expr;
use gridcraft_model::{
@@ -508,9 +508,13 @@ fn read_formula(cx: &mut Ctx<'_>, st: &mut RowState, f: &El, pos: CellRef, dynam
}
}
/// `2024-03-15`, `2024-03-15T10:30:00Z`, `10:30:00` → serial. Dates count real days from the
/// epoch, so `1899-12-30T08:00:00` is the time 8:00 (see [`serial_from_iso_ymd`]).
/// `2024-03-15`, `2024-03-15T10:30:00Z`, `10:30:00` → serial.
pub fn parse_iso_datetime(s: &str, sys: DateSystem) -> Option<f64> {
parse_iso_datetime_with(s, |y, m, d| serial_from_ymd(sys, y, m, d))
}
/// [`parse_iso_datetime`] with `ymd` turning the date part (year, month 1–12, day 1–31) into a serial.
pub(crate) fn parse_iso_datetime_with(s: &str, ymd: impl Fn(i64, i64, i64) -> Option<f64>) -> Option<f64> {
let s = s.trim_end_matches('Z');
let (date, time) = match s.split_once('T') {
Some((d, t)) => (d, Some(t)),
@@ -523,7 +527,10 @@ pub fn parse_iso_datetime(s: &str, sys: DateSystem) -> Option<f64> {
let y: i64 = it.next()?.parse().ok()?;
let m: i64 = it.next()?.parse().ok()?;
let d: i64 = it.next()?.parse().ok()?;
serial = serial_from_iso_ymd(sys, y, m, d)?;
if !(1..=12).contains(&m) || !(1..=31).contains(&d) {
return None;
}
serial = ymd(y, m, d)?;
}
if let Some(t) = time {
// Drop a time-zone offset.
@@ -1005,10 +1012,7 @@ mod tests {
#[test]
fn iso_dates() {
assert_eq!(parse_iso_datetime("1900-01-01", DateSystem::D1900), Some(2.0));
assert_eq!(parse_iso_datetime("1899-12-31T12:30:00", DateSystem::D1900), Some(1.0 + time_fraction(12.0, 30.0, 0.0)));
assert_eq!(parse_iso_datetime("1899-12-30T08:00:00", DateSystem::D1900), Some(time_fraction(8.0, 0.0, 0.0)));
assert_eq!(parse_iso_datetime("1900-03-01", DateSystem::D1900), Some(61.0));
assert_eq!(parse_iso_datetime("1900-01-01", DateSystem::D1900), Some(1.0));
assert_eq!(parse_iso_datetime("2024-01-01T12:00:00Z", DateSystem::D1900), Some(45292.5));
assert_eq!(parse_iso_datetime("1904-01-02", DateSystem::D1904), Some(1.0));
assert_eq!(parse_iso_datetime("06:00:00", DateSystem::D1900), Some(0.25));