Repository: dotnet/runtime
File: src/libraries/System.Private.Xml/src/System/Xml/Schema/XsdDateTime.cs (C#)

Code before:

        /// <summary>
        /// Constructs an XsdDateTime from a string using specific format.
        /// </summary>
        public XsdDateTime(string text, XsdDateTimeFlags kinds) : this()
        {
            Parser parser = default;
            if (!parser.Parse(text, kinds))
            {
                throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, text, kinds));
            }
            if (!TryInitiateXsdDateTime(parser))
            {
                throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, text, kinds));
            }
        }

        private bool TryInitiateXsdDateTime(Parser parser)
        {
            // Per ISO 8601, 24:00:00 represents end of a calendar day
            // (same instant as next day's 00:00:00). Set hour to 0 and add one day.
            int hour = parser.hour;
            bool isEndOfDay = hour == 24;
            if (isEndOfDay)
            {
                hour = 0;
            }

            _dt = new DateTime(parser.year, parser.month, parser.day, hour, parser.minute, parser.second);
            if (parser.fraction != 0)
            {
                _dt = _dt.AddTicks(parser.fraction);
            }

            if (isEndOfDay)
            {
                if (_dt.Ticks > DateTime.MaxValue.Ticks - TimeSpan.TicksPerDay)
                {
                    return false;
                }
                _dt = _dt.AddDays(1);
            }

            _extra = (uint)(((int)parser.typeCode << TypeShift) | ((int)parser.kind << KindShift) | (parser.zoneHour << ZoneHourShift) | parser.zoneMinute);
            return true;
        }

        internal static bool TryParse(string text, XsdDateTimeFlags kinds, out XsdDateTime result)
        {
            Parser parser = default;
            if (!parser.Parse(text, kinds))

Review comment (bug):
@copilot when the hour is 24, should we check the minute and seconds to be zeros and fail if it is otherwise

Code after:
        private static readonly long s_maxDateTimeTicksForEndOfDay = DateTime.MaxValue.Ticks - TimeSpan.TicksPerDay;

        private const int CharStackBufferSize = 64;

        /// <summary>
        /// Constructs an XsdDateTime from a string using specific format.
        /// </summary>
        public XsdDateTime(string text, XsdDateTimeFlags kinds) : this()
        {
            Parser parser = default;
            if (!parser.Parse(text, kinds))
            {
                throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, text, kinds));
            }
            if (!TryInitiateXsdDateTime(parser))
            {
                throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, text, kinds));
            }
        }

        private bool TryInitiateXsdDateTime(Parser parser)
        {
            // Per ISO 8601, 24:00:00 represents end of a calendar day
            // (same instant as next day's 00:00:00). Set hour to 0 and add one day.
            int hour = parser.hour;
            bool isEndOfDay = hour == 24;
            if (isEndOfDay)
            {
                Debug.Assert(parser.minute == 0, "minute must be zero when hour is 24");
                Debug.Assert(parser.second == 0, "second must be zero when hour is 24");
                Debug.Assert(parser.fraction == 0, "fraction must be zero when hour is 24");
                hour = 0;
            }

            _dt = new DateTime(parser.year, parser.month, parser.day, hour, parser.minute, parser.second);
            if (parser.fraction != 0)
            {
                _dt = _dt.AddTicks(parser.fraction);
            }

            if (isEndOfDay)
            {
                if (_dt.Ticks > s_maxDateTimeTicksForEndOfDay)
                {
                    return false;
                }
                _dt = _dt.AddDays(1);
            }

            _extra = (uint)(((int)parser.typeCode << TypeShift) | ((int)parser.kind << KindShift) | (parser.zoneHour << ZoneHourShift) | parser.zoneMinute);
            return true;