Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 22 additions & 11 deletions src/TriasDev.Tabular/Ods/OdsCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal sealed class OdsCursor : ITabularCursor

/// <summary>
/// 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 <see cref="FromDuration"/>).
/// </summary>
private DateTime? _nullDate;

Expand Down Expand Up @@ -685,15 +685,24 @@ private static RawCell TypedValue(SheetScanner scanner, ReadOnlySpan<char> type,
};

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// Not <see cref="XlsxCursor.TryFromSerial"/>, 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.
/// <para>
/// 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.
/// </para>
/// <para>
/// 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.
/// </para>
/// </remarks>
private static RawCell FromDuration(ReadOnlySpan<char> duration, DateTime? nullDate)
{
Expand All @@ -702,9 +711,11 @@ private static RawCell FromDuration(ReadOnlySpan<char> 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;
}
Expand Down
22 changes: 7 additions & 15 deletions src/TriasDev.Tabular/Xlsx/XlsxCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>The 1900 system's serial for 29 February 1900, a day the calendar does not have.</summary>
private const double PhantomLeapDay = 60;

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
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);

/// <summary>
/// The day and time as many days after <paramref name="epoch"/> as <paramref name="days"/> says,
Expand Down
26 changes: 25 additions & 1 deletion tests/TriasDev.Tabular.Tests/Ods/OdsCursorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

/// <summary>
/// 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.
/// </summary>
[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", "<table:table-row>"
+ Cell($"office:value-type=\"time\" office:time-value=\"{duration}\"")
+ "</table:table-row>"));

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")]
Expand Down
Loading