Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Parser/Utf8Parser.Date.Helpers.cs (C#)

Code before:
            Debug.Assert(year <= 9999); // All of our callers to date parse the year from fixed 4-digit fields so this value is trusted.

            if ((((uint)month) - 1) >= 12)
            {
                value = default;
                return false;
            }

            uint dayMinusOne = ((uint)day) - 1;
            if (dayMinusOne >= 28 && dayMinusOne >= DateTime.DaysInMonth(year, month))
            {
                value = default;
                return false;
            }

            // ISO 8601 allows hour=24 to represent end of day (midnight), but only when minute, second, and fraction are all zero.
            // In this case, we advance to the next day at 00:00.
            if (hour == 24)
            {
                if (minute != 0 || second != 0 || fraction != 0)
                {
                    value = default;
                    return false;
                }

                ReadOnlySpan<int> daysInMonth = DateTime.IsLeapYear(year) ? GregorianCalendar.DaysToMonth366 : GregorianCalendar.DaysToMonth365;
                int yearMinus1 = year - 1;
                int daysCount = (yearMinus1 * 365) + (yearMinus1 / 4) - (yearMinus1 / 100) + (yearMinus1 / 400) + daysInMonth[month - 1] + day - 1;
                // Calculate ticks for current day and add one day to get to midnight of the next day
                long nextDayTicks = daysCount * TimeSpan.TicksPerDay + TimeSpan.TicksPerDay;

                if ((ulong)nextDayTicks > DateTime.MaxTicks)
                {
                    value = default;
                    return false;
                }

                value = new DateTime(ticks: nextDayTicks, kind: kind);
                return true;
            }

            if (((uint)hour) > 23)
            {
                value = default;
                return false;
            }

            if (((uint)minute) > 59)
            {
                value = default;
                return false;

Review comment (suggestion):
@copilot instead of duplicating the code, maybe better to set the hour to zero and then fall back to normal code flow.

Code after:
            Debug.Assert(year <= 9999); // All of our callers to date parse the year from fixed 4-digit fields so this value is trusted.

            if ((((uint)month) - 1) >= 12)
            {
                value = default;
                return false;
            }

            uint dayMinusOne = ((uint)day) - 1;
            if (dayMinusOne >= 28 && dayMinusOne >= DateTime.DaysInMonth(year, month))
            {
                value = default;
                return false;
            }

            // Per ISO 8601 (Date and time — Representations for information interchange), 24:00:00 represents end of a calendar day
            // (same instant as next day's 00:00:00), but only when minute, second, and fraction are all zero.
            // We treat it as hour=0 and add one day at the end.
            bool isEndOfDay = false;
            if (hour == 24)
            {
                if (minute != 0 || second != 0 || fraction != 0)
                {
                    value = default;
                    return false;
                }

                hour = 0;
                isEndOfDay = true;
            }

            if (((uint)hour) > 23)
            {
                value = default;
                return false;
            }

            if (((uint)minute) > 59)
            {
                value = default;
                return false;
            }

            if (((uint)second) > 59)
            {
                value = default;
                return false;
            }

            Debug.Assert(fraction >= 0 && fraction <= Utf8Constants.MaxDateTimeFraction); // All of our callers to date parse the fraction from fixed 7-digit fields so this value is trusted.