From 537ae159f4b2e022f9f75a1082a41db8b5289b12 Mon Sep 17 00:00:00 2001 From: Slava Date: Tue, 17 Nov 2026 19:57:47 +0100 Subject: [PATCH] fix: read an ods duration of one to fifty-nine days on the day LibreOffice shows, not a day later --- docs/formats.md | 9 ++--- src/TriasDev.Tabular/Ods/OdsCursor.cs | 33 ++++++++++++------- src/TriasDev.Tabular/Xlsx/XlsxCursor.cs | 22 ++++--------- .../Ods/OdsCursorTests.cs | 26 ++++++++++++++- 4 files changed, 59 insertions(+), 31 deletions(-) diff --git a/docs/formats.md b/docs/formats.md index 84182e5..a457101 100644 --- a/docs/formats.md +++ b/docs/formats.md @@ -21,10 +21,11 @@ its cell, and the profile counts it as a number in a date column. A workbook guesses dates from number formats; a `.ods` cell states its type beside its value, so there is nothing to guess. `float`, `percentage` and `currency` read as numbers — a value that is no finite number, such as `NaN` or `1e400`, reads as the cell's text, as an xlsx one does —, `date` as a date, -`time` as the workbook serial of as many days — a time of day on 31 December 1899, the day an xlsx -time-only cell reads on, and a longer duration on the day that serial names; a spreadsheet that -states another null date in its calculation settings (LibreOffice offers 1 January 1904) has its -durations counted from that day — and `boolean` as a +`time` as a duration — under a day, a time of day on 31 December 1899, the day an xlsx time-only +cell reads on; a day or longer, on the day LibreOffice shows, counted from its null date of +30 December 1899 (its calendar has no 29 February 1900, so one day is 31 December and sixty days +28 February); a spreadsheet that states another null date in its calculation settings (LibreOffice +offers 1 January 1904) has all its durations counted from that day — and `boolean` as a boolean. Anything else is text: the cell's `office:string-value` when it has one, else its paragraphs joined by a line feed, comments left out. Paragraph text is read with ODF's white-space rule, as LibreOffice shows it: a space, tab or line end written as a character is one space, a run of them diff --git a/src/TriasDev.Tabular/Ods/OdsCursor.cs b/src/TriasDev.Tabular/Ods/OdsCursor.cs index 33cab88..8f208c5 100644 --- a/src/TriasDev.Tabular/Ods/OdsCursor.cs +++ b/src/TriasDev.Tabular/Ods/OdsCursor.cs @@ -90,7 +90,7 @@ internal sealed class OdsCursor : ITabularCursor /// /// The day the spreadsheet counts its durations from, when it states one other than the default - /// 30 December 1899; null for the default, which reads as a workbook serial does. + /// 30 December 1899; null for the default (see ). /// private DateTime? _nullDate; @@ -685,15 +685,24 @@ private static RawCell TypedValue(SheetScanner scanner, ReadOnlySpan type, }; /// - /// A time cell, an ISO 8601 duration, read as the workbook serial of as many days: a time of day - /// on 31 December 1899, as an xlsx time-only cell, and a longer one — LibreOffice's form for a - /// date-time under a time-only format — on the day the serial names. A spreadsheet that states - /// another null date counts the duration from that day instead, as LibreOffice does. + /// A time cell, an ISO 8601 duration, read as a time of day on 31 December 1899 when it is shorter + /// than a day — the day an xlsx time-only cell reads on — and otherwise on the day LibreOffice + /// shows for it: as many days after its null date, 30 December 1899 unless the spreadsheet states + /// another. A longer duration is LibreOffice's form for a date-time under a time-only format. /// /// - /// Not , which refuses Excel's phantom 29 February 1900: - /// LibreOffice's calendar has no such day, so sixty days from its null date is 28 February, a - /// real one. + /// + /// LibreOffice's calendar has no 29 February 1900, so unlike an xlsx serial no day is refused and + /// every day from one on counts from 30 December: a duration of one day is 31 December 1899, of + /// sixty days 28 February 1900. They used to count from 31 December below sixty days, as an xlsx + /// serial does, which read the dates of January and February 1900 a day later than LibreOffice + /// shows them. + /// + /// + /// Under a day the reading keeps to 31 December, so that a time column reads alike from an ods and + /// an xlsx; the cost is that a duration just under a day reads later than one of a day exactly, + /// at a point no date-time of LibreOffice's ever falls on. + /// /// private static RawCell FromDuration(ReadOnlySpan duration, DateTime? nullDate) { @@ -702,9 +711,11 @@ private static RawCell FromDuration(ReadOnlySpan duration, DateTime? nullD return RawCell.Empty; } - bool read = nullDate is { } day - ? XlsxCursor.TryFromDays(day, span.TotalDays, out DateTime date) - : XlsxCursor.TryFromWorkbookDays(span.TotalDays, out date); + DateTime from = nullDate + ?? (span < TimeSpan.FromDays(1) + ? new DateTime(1899, 12, 31, 0, 0, 0, DateTimeKind.Unspecified) + : XlsxCursor.LibreOfficeNullDate); + bool read = XlsxCursor.TryFromDays(from, span.TotalDays, out DateTime date); return read ? RawCell.FromDate(date) : RawCell.Empty; } diff --git a/src/TriasDev.Tabular/Xlsx/XlsxCursor.cs b/src/TriasDev.Tabular/Xlsx/XlsxCursor.cs index dc1b3f1..20d58cc 100644 --- a/src/TriasDev.Tabular/Xlsx/XlsxCursor.cs +++ b/src/TriasDev.Tabular/Xlsx/XlsxCursor.cs @@ -1597,28 +1597,20 @@ internal static bool TryFromSerial(double serial, bool date1904, out DateTime da return false; } - return TryFromWorkbookDays(serial, out date); + return TryFromDays( + serial > PhantomLeapDay ? LibreOfficeNullDate : new DateTime(1899, 12, 31, 0, 0, 0, DateTimeKind.Unspecified), + serial, + out date); } /// The 1900 system's serial for 29 February 1900, a day the calendar does not have. private const double PhantomLeapDay = 60; /// - /// A count of days read as a 1900-system serial is read, but with nothing refused for the phantom - /// leap day: 60 days and more count from 30 December 1899, fewer from 31 December. + /// The day the 1900 system's serials past the phantom leap day count from, and the day LibreOffice + /// counts every serial from, as its calendar has no 29 February 1900. /// - /// - /// For an OpenDocument time cell, a duration since LibreOffice's null date. LibreOffice's calendar - /// has no 29 February 1900, so sixty days from 30 December 1899 is 28 February, a real day, and is - /// read as one; a time of day, under one day, reads on 31 December as a workbook's does. - /// - internal static bool TryFromWorkbookDays(double days, out DateTime date) => - TryFromDays( - days >= PhantomLeapDay - ? new DateTime(1899, 12, 30, 0, 0, 0, DateTimeKind.Unspecified) - : new DateTime(1899, 12, 31, 0, 0, 0, DateTimeKind.Unspecified), - days, - out date); + internal static readonly DateTime LibreOfficeNullDate = new(1899, 12, 30, 0, 0, 0, DateTimeKind.Unspecified); /// /// The day and time as many days after as says, diff --git a/tests/TriasDev.Tabular.Tests/Ods/OdsCursorTests.cs b/tests/TriasDev.Tabular.Tests/Ods/OdsCursorTests.cs index 1106cf4..dc5345c 100644 --- a/tests/TriasDev.Tabular.Tests/Ods/OdsCursorTests.cs +++ b/tests/TriasDev.Tabular.Tests/Ods/OdsCursorTests.cs @@ -487,10 +487,34 @@ public void ReadsATimeWithMoreFractionalDigitsThanATimeSpanHolds() Assert.Equal(RawCell.FromDate(epoch.AddHours(11)), cells[0]); Assert.Equal(RawCell.FromDate(epoch.Add(new TimeSpan(0, 1, 2, 3, 457))), cells[1]); - Assert.Equal(RawCell.FromDate(epoch.AddHours(1234).AddMinutes(5).AddSeconds(6)), cells[2]); + Assert.Equal(RawCell.FromDate(epoch.AddDays(-1).AddHours(1234).AddMinutes(5).AddSeconds(6)), cells[2]); Assert.Equal(RawCell.FromDate(epoch.Add(new TimeSpan(0, 10, 59, 59, 999))), cells[3]); } + /// + /// A duration of a day or more is a date-time under a time-only format, read on the day LibreOffice + /// shows: counted from 30 December 1899, as its calendar has no 29 February 1900, so the dates of + /// January and February 1900 are not a day late. Under a day it stays a time on 31 December. + /// + [Theory] + [InlineData("PT23H59M59S", "1899-12-31T23:59:59")] + [InlineData("PT24H", "1899-12-31T00:00:00")] + [InlineData("P1DT12H", "1899-12-31T12:00:00")] + [InlineData("P2D", "1900-01-01T00:00:00")] + [InlineData("P59D", "1900-02-27T00:00:00")] + [InlineData("P60D", "1900-02-28T00:00:00")] + [InlineData("P61D", "1900-03-01T00:00:00")] + public void ReadsADurationOfADayOrMoreOnTheDayLibreOfficeShows(string duration, string expected) + { + using OdsCursor cursor = Open(new OdsPackage().WithTable("S", "" + + Cell($"office:value-type=\"time\" office:time-value=\"{duration}\"") + + "")); + + Assert.Equal( + RawCell.FromDate(DateTime.Parse(expected, System.Globalization.CultureInfo.InvariantCulture)), + Assert.Single(ReadAll(cursor))[0]); + } + [Theory] [InlineData("1904-01-01", "1904-01-01T12:30:00", "1904-03-01T00:00:00")] [InlineData("1980-06-15", "1980-06-15T12:30:00", "1980-08-14T00:00:00")]