Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs (C#)

Code before:
        **              next character after str.Index.
        **Exceptions: FormatException if a time mark can not be found.
        ==============================================================================*/

        private static bool MatchTimeMark(ref __DTString str, DateTimeFormatInfo dtfi, scoped ref TM result)
        {
            result = TM.NotSet;
            // In some cultures have empty strings in AM/PM mark. E.g. af-ZA (0x0436), the AM mark is "", and PM mark is "nm".
            if (dtfi.AMDesignator.Length == 0)
            {
                result = TM.AM;
            }
            if (dtfi.PMDesignator.Length == 0)
            {
                result = TM.PM;
            }

            if (str.GetNext())
            {
                string searchStr = dtfi.AMDesignator;
                if (searchStr.Length > 0)
                {
                    if (str.MatchSpecifiedWord(searchStr))
                    {
                        string searchStr1 = dtfi.PMDesignator;
                        if (searchStr1.Length > searchStr.Length && searchStr1.StartsWith(searchStr1, StringComparison.Ordinal) && str.MatchSpecifiedWord(searchStr1))
                        {
                            // AM designator is a prefix of PM designator and we have matched PM designator. Use longer match.
                            str.Index += (searchStr1.Length - 1);
                            result = TM.PM;
                            return true;
                        }

                        // Found an AM timemark with length > 0.
                        str.Index += (searchStr.Length - 1);
                        result = TM.AM;
                        return true;
                    }
                }
                searchStr = dtfi.PMDesignator;
                if (searchStr.Length > 0)
                {
                    if (str.MatchSpecifiedWord(searchStr))
                    {
                        // Found a PM timemark with length > 0.
                        str.Index += (searchStr.Length - 1);
                        result = TM.PM;
                        return true;
                    }
                }
                str.Index--; // Undo the GetNext call.

Review comment (suggestion):
The StartsWith call is comparing searchStr1 (PM designator) with itself instead of checking if searchStr1 starts with searchStr (AM designator). This should be `searchStr1.StartsWith(searchStr, StringComparison.Ordinal)` to correctly check if the PM designator starts with the AM designator.
```suggestion
                        if (searchStr1.Length > searchStr.Length && searchStr1.StartsWith(searchStr, StringComparison.Ordinal) && str.MatchSpecifiedWord(searchStr1))
```

Code after:
        **              next character after str.Index.
        **Exceptions: FormatException if a time mark can not be found.
        ==============================================================================*/

        private static bool MatchTimeMark(ref __DTString str, DateTimeFormatInfo dtfi, scoped ref TM result)
        {
            result = TM.NotSet;
            // In some cultures have empty strings in AM/PM mark. E.g. af-ZA (0x0436), the AM mark is "", and PM mark is "nm".
            if (dtfi.AMDesignator.Length == 0)
            {
                result = TM.AM;
            }
            if (dtfi.PMDesignator.Length == 0)
            {
                result = TM.PM;
            }

            if (str.GetNext())
            {
                string amDesignator = dtfi.AMDesignator;
                string pmDesignator;

                if (amDesignator.Length > 0)
                {
                    if (str.MatchSpecifiedWord(amDesignator))
                    {
                        pmDesignator = dtfi.PMDesignator;
                        if (pmDesignator.StartsWith(amDesignator, StringComparison.Ordinal) && str.MatchSpecifiedWord(pmDesignator))
                        {
                            // AM designator is a prefix of PM designator and we have matched PM designator. Use longer match.
                            str.Index += (pmDesignator.Length - 1);
                            result = TM.PM;
                            return true;
                        }

                        // Found an AM timemark with length > 0.
                        str.Index += (amDesignator.Length - 1);
                        result = TM.AM;
                        return true;
                    }
                }

                pmDesignator = dtfi.PMDesignator;
                if (pmDesignator.Length > 0)
                {
                    if (str.MatchSpecifiedWord(pmDesignator))
                    {
                        // Found a PM timemark with length > 0.
                        str.Index += (pmDesignator.Length - 1);
                        result = TM.PM;
                        return true;