private static void CheckDefaultDateTime(ref DateTimeResult result, ref Calendar cal, DateTimeStyles styles) {
if ((result.Year == -1) || (result.Month == -1) || (result.Day == -1)) {
/*
The following table describes the behaviors of getting the default value
when a certain year/month/day values are missing.
An "X" means that the value exists. And "--" means that value is missing.
Year Month Day => ResultYear ResultMonth ResultDay Note
X X X Parsed year Parsed month Parsed day
X X -- Parsed Year Parsed month First day If we have year and month, assume the first day of that month.
X -- X Parsed year First month Parsed day If the month is missing, assume first month of that year.
X -- -- Parsed year First month First day If we have only the year, assume the first day of that year.
-- X X CurrentYear Parsed month Parsed day If the year is missing, assume the current year.
-- X -- CurrentYear Parsed month First day If we have only a month value, assume the current year and current day.
-- -- X CurrentYear First month Parsed day If we have only a day value, assume current year and first month.
-- -- -- CurrentYear Current month Current day So this means that if the date string only contains time, you will get current date.
*/
DateTime now = DateTime.Now;
if (result.Month == -1 && result.Day == -1) {
if (result.Year == -1) {
if ((styles & DateTimeStyles.NoCurrentDateDefault) != 0) {
// If there is no year/month/day values, and NoCurrentDateDefault flag is used,
// set the year/month/day value to the beginning year/month/day of DateTime().
// Note we should be using Gregorian for the year/month/day.
cal = GregorianCalendar.GetDefaultInstance();
result.Year = result.Month = result.Day = 1;
} else {
// Year/Month/Day are all missing.
result.Year = cal.GetYear(now);
result.Month = cal.GetMonth(now);
result.Day = cal.GetDayOfMonth(now);
}
} else {
// Month/Day are both missing.
result.Month = 1;
result.Day = 1;
}
} else {
if (result.Year == -1) {
result.Year = cal.GetYear(now);
}
if (result.Month == -1) {
result.Month = 1;
}
if (result.Day == -1) {
result.Day = 1;
}
}
}
// Set Hour/Minute/Second to zero if these value are not in str.
if (result.Hour == -1) result.Hour = 0;
if (result.Minute == -1) result.Minute = 0;
if (result.Second == -1) result.Second = 0;
if (result.era == -1) result.era = Calendar.CurrentEra;
}