Excel 29.02.1900 и .NET/C#
От: fortnum  
Дата: 01.07.22 10:00
Оценка:
Есть широко известный узких кругах баг в Excel, Office и (по идее) некоторых еще прочих электронных таблицах.

Microsoft Excel has, since its earliest versions, incorrectly considered 1900 to be a leap year, and therefore that February 29 comes between February 28 and March 1 of that year. The bug originated from Lotus 1-2-3, and was purposely implemented in Excel for the purpose of backward compatibility. Microsoft has written an article about this bug, explaining the reasons for treating 1900 as a leap year.[7] This bug has been promoted into a requirement in the Ecma Office Open XML (OOXML) specification.[8][9]


А я хочу сымитировать поведение этой ерунды на .NET/C#

Проблема в том, что:
new DateTime(1900, 2, 29);


даёт ArgumentOutOfRangeException, а всякие манипуляции с JulianCalendar ни к чему не приводят, так как:

Currently, the JulianCalendar is not used by any of the cultures supported by the CultureInfo class. Therefore, the JulianCalendar class can be used only to calculate dates in the Julian calendar.


+ еще:

With the exception of the constructors that include a parameter of type Calendar and allow the elements of a date (that is, the month, the day, and the year) to reflect values in a designated calendar, both DateTime and DateTimeOffset values are always based on the Gregorian calendar.


И на выходе получаем просто 13.03.1900. Как-то взломать через Reflection тоже пока не получилось.

Как можно, не изобретая велосипед, в C# преобразовать такую дату в соответствующую строку, используя множество всех уже доступных строк форматирования?


ЗЫ. Пока вышел из положения заменой данной конкретной даты на 1896 г. (законно високосный по григорианскому) с последующим двойным Replace'ом по строке '1896' на '1900' и '96' на '00' Но может быть есть более кошерный способ?
Отредактировано 01.07.2022 10:34 fortnum . Предыдущая версия .
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.