Skip to content

Commit

Permalink
Simplify some date handling edge-cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkPflug committed Aug 16, 2023
1 parent 318192c commit f45f4f4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions source/Sylvan.Data.Excel/ExcelDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract partial class ExcelDataReader : DbDataReader, IDisposable, IDbCo
private protected int rowCount;
private protected int rowFieldCount;

static readonly DateTime Epoch1900 = new DateTime(1899, 12, 31);
static readonly DateTime Epoch1900 = new DateTime(1899, 12, 30);
static readonly DateTime Epoch1904 = new DateTime(1904, 1, 1);

private protected DateMode dateMode;
Expand Down Expand Up @@ -704,6 +704,7 @@ static internal bool TryGetDate(ExcelFormat fmt, double value, DateMode mode, ou
{
dt = DateTime.MinValue;
DateTime epoch = Epoch1904;
// Excel doesn't render negative values as dates.
if (value < 0.0)
return false;
if (mode == DateMode.Mode1900)
Expand All @@ -727,12 +728,11 @@ static internal bool TryGetDate(ExcelFormat fmt, double value, DateMode mode, ou
if (value >= 60d)
{
// 1900 wasn't a leapyear, but Excel thinks it was
// values in this range are in-expressible as .NET dates
// Excel renders it as 1900-2-29 (not a real day)
return false;
}
}
else
{
value -= 1;
value += 1;
}
}
dt = epoch.AddDays(value);
Expand Down
6 changes: 3 additions & 3 deletions source/Sylvan.Data.Excel/Xlsx/XlsxDataWriter+FieldWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Sylvan.Data.Excel.Xlsx;

partial class XlsxDataWriter
{
static DateTime Epoch = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);
static DateTime Epoch = new DateTime(1899, 12, 30, 0, 0, 0, DateTimeKind.Unspecified);

sealed class Context
{
Expand Down Expand Up @@ -228,7 +228,7 @@ public static void WriteDateTime(Context c, DateTime value)
var w = c.xw;
w.Write("<c s=\"1\"><v>");

var val = (value - Epoch).TotalDays + 2;
var val = (value - Epoch).TotalDays;
#if SPAN
var scratch = c.GetCharBuffer();
if (val.TryFormat(scratch.AsSpan(), out var sl, default, CultureInfo.InvariantCulture))
Expand Down Expand Up @@ -288,7 +288,7 @@ public static void WriteDateOnly(Context c, DateOnly value)
var w = c.xw;
w.Write("<c s=\"2\"><v>");

var val = (value.ToDateTime(Midnight) - Epoch).TotalDays + 2;
var val = (value.ToDateTime(Midnight) - Epoch).TotalDays;

var scratch = c.GetCharBuffer();
if (val.TryFormat(scratch.AsSpan(), out var sl, default, CultureInfo.InvariantCulture))
Expand Down

0 comments on commit f45f4f4

Please sign in to comment.