|
|
От: |
Михаил Романов
|
https://mihailromanov.wordpress.com/ |
| Дата: | 03.07.22 11:21 | ||
| Оценка: | 135 (2) | ||
public class BadCalendar : GregorianCalendar
{
static DateTime TresholdDateTime = (new DateTime(1900, 3, 1)).AddTicks(-1);
public override DateTime ToDateTime(int year, int month, int day, int hour,
int minute, int second, int millisecond, int era)
{
if (year == 1900 && month == 2 && day == 29)
return base.ToDateTime(year, month, 28, hour, minute,
second, millisecond, era).AddDays(1);
return base.ToDateTime(year, month, day, hour, minute,
second, millisecond, era);
}
public override int GetMonth(DateTime time)
{
if (time > TresholdDateTime)
return base.GetMonth(time.AddDays(-1));
return base.GetMonth(time);
}
public override int GetYear(DateTime time)
{
if (time > TresholdDateTime)
return base.GetYear(time.AddDays(-1));
return base.GetYear(time);
}
public override int GetDayOfMonth(DateTime time)
{
if (time.Year == 1900 && time.Month == 3 && time.Day == 1)
return 29;
if (time > TresholdDateTime)
{
return base.GetDayOfMonth(time.AddDays(-1));
}
return base.GetDayOfMonth(time);
}
}var calendar = new BadCalendar();
var formatInfo = new DateTimeFormatInfo();
var calendarField = formatInfo.GetType().GetField(
"calendar",
System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
calendarField.SetValue(formatInfo, calendar);
var date = new DateTime(1900, 2, 29, calendar);
Console.WriteLine(date.ToString(formatInfo));var date1 = DateTime.Parse("02/29/1900", formatInfo);System.FormatException: String '02/29/1900' was not recognized as a valid DateTime.
internal virtual CalendarId ID => CalendarId.UNINITIALIZED_VALUE;
internal enum CalendarId : ushort
{
UNINITIALIZED_VALUE = 0,
GREGORIAN = 1, // Gregorian (localized) calendar
GREGORIAN_US = 2, // Gregorian (U.S.) calendar
JAPAN = 3, // Japanese Emperor Era calendar
/* SSS_WARNINGS_OFF */
TAIWAN = 4, // Taiwan Era calendar /* SSS_WARNINGS_ON */
KOREA = 5, // Korean Tangun Era calendar
HIJRI = 6, // Hijri (Arabic Lunar) calendar
THAI = 7, // Thai calendar
HEBREW = 8, // Hebrew (Lunar) calendar
GREGORIAN_ME_FRENCH = 9, // Gregorian Middle East French calendar
GREGORIAN_ARABIC = 10, // Gregorian Arabic calendar
GREGORIAN_XLIT_ENGLISH = 11, // Gregorian Transliterated English calendar
GREGORIAN_XLIT_FRENCH = 12,
// Note that all calendars after this point are MANAGED ONLY for now.
JULIAN = 13,
JAPANESELUNISOLAR = 14,
CHINESELUNISOLAR = 15,
SAKA = 16, // reserved to match Office but not implemented in our code
LUNAR_ETO_CHN = 17, // reserved to match Office but not implemented in our code
LUNAR_ETO_KOR = 18, // reserved to match Office but not implemented in our code
LUNAR_ETO_ROKUYOU = 19, // reserved to match Office but not implemented in our code
KOREANLUNISOLAR = 20,
TAIWANLUNISOLAR = 21,
PERSIAN = 22,
UMALQURA = 23,
LAST_CALENDAR = 23 // Last calendar ID
}