Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1900 year DateTime correction #599

Merged
Merged
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
28 changes: 25 additions & 3 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,20 @@ private Tuple<string, string, string> GetCellValue(int rowIndex, int cellIndex,
}
else if (columnInfo == null || columnInfo.ExcelFormat == null)
{
var oaDate = ((DateTime)value).ToOADate();

// Excel says 1900 was a leap year :( Replicate an incorrect behavior thanks
// to Lotus 1-2-3 decision from 1983...
// https://github.com/ClosedXML/ClosedXML/blob/develop/ClosedXML/Extensions/DateTimeExtensions.cs#L45
const int nonExistent1900Feb29SerialDate = 60;
if (oaDate <= nonExistent1900Feb29SerialDate)
{
oaDate -= 1;
}

dataType = null;
styleIndex = "3";
cellValue = ((DateTime)value).ToOADate().ToString(CultureInfo.InvariantCulture);
cellValue = oaDate.ToString(CultureInfo.InvariantCulture);
}
else
{
Expand All @@ -585,9 +596,20 @@ private Tuple<string, string, string> GetCellValue(int rowIndex, int cellIndex,
else if (columnInfo == null || columnInfo.ExcelFormat == null)
{
var day = (DateOnly)value;
dataType = "n";
var oaDate = day.ToDateTime(TimeOnly.MinValue).ToOADate();

// Excel says 1900 was a leap year :( Replicate an incorrect behavior thanks
// to Lotus 1-2-3 decision from 1983...
// https://github.com/ClosedXML/ClosedXML/blob/develop/ClosedXML/Extensions/DateTimeExtensions.cs#L45
const int nonExistent1900Feb29SerialDate = 60;
if (oaDate <= nonExistent1900Feb29SerialDate)
{
oaDate -= 1;
}

dataType = null;
styleIndex = "3";
cellValue = day.ToDateTime(TimeOnly.MinValue).ToOADate().ToString(CultureInfo.InvariantCulture);
cellValue = oaDate.ToString(CultureInfo.InvariantCulture);
}
else
{
Expand Down
Loading