1900 year DateTime correction (#599)

Co-authored-by: Lukasz Arciszewski <lukasz.arciszewski@accenture.com>
This commit is contained in:
duszekmestre 2024-05-17 15:29:11 +02:00 committed by GitHub
parent 4d1ee8e327
commit 49bc8e99ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -563,9 +563,20 @@ namespace MiniExcelLibs.OpenXml
}
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
{
@ -585,9 +596,20 @@ namespace MiniExcelLibs.OpenXml
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
{