public static void FormatParseTest() { string[] patterns = new string[] { CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern, CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern, "t", "T", "o", "r" }; TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now); foreach (string format in patterns) { string formattedTime = timeOnly.ToString(format); timeOnly = TimeOnly.Parse(formattedTime); Assert.True(TimeOnly.TryParse(formattedTime, out TimeOnly parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly1); TimeOnly parsedTimeOnly = TimeOnly.Parse(formattedTime.AsSpan()); Assert.True(TimeOnly.TryParse(formattedTime.AsSpan(), out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.Parse(formattedTime, CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParse(formattedTime, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.Parse(formattedTime.AsSpan(), CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParse(formattedTime.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime, format); Assert.True(TimeOnly.TryParseExact(formattedTime, format, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), format.AsSpan()); Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), format.AsSpan(), out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime, format, CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParseExact(formattedTime, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime, patterns); Assert.True(TimeOnly.TryParseExact(formattedTime, patterns, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), patterns); Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), patterns, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime, patterns, CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParseExact(formattedTime, patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), patterns, CultureInfo.CurrentCulture); Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1)); Assert.Equal(timeOnly, parsedTimeOnly); Assert.Equal(timeOnly, parsedTimeOnly1); } }