From a003e9c8d199069e73078a67da43aaf92add53c7 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Wed, 14 Apr 2021 12:36:32 -0700 Subject: [PATCH] Address the remaining comments from Date/TimeOnly PR --- .../src/System/DateOnly.cs | 121 ++++++++---------- .../src/System/Globalization/DateTimeParse.cs | 1 + .../src/System/TimeOnly.cs | 109 +++++++--------- 3 files changed, 100 insertions(+), 131 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/DateOnly.cs b/src/libraries/System.Private.CoreLib/src/System/DateOnly.cs index 6cee176a31d103..9b3b16b0727824 100644 --- a/src/libraries/System.Private.CoreLib/src/System/DateOnly.cs +++ b/src/libraries/System.Private.CoreLib/src/System/DateOnly.cs @@ -6,15 +6,6 @@ namespace System { - internal enum ParseOperationResult - { - Success, - WrongStyles, - ParseFailure, - WrongParts, - BadFormatSpecifier - } - /// /// Represents dates with values ranging from January 1, 0001 Anno Domini (Common Era) through December 31, 9999 A.D. (C.E.) in the Gregorian calendar. /// @@ -266,17 +257,10 @@ public int CompareTo(object? value) /// An object that is equivalent to the date contained in s, as specified by provider and styles. public static DateOnly Parse(ReadOnlySpan s, IFormatProvider? provider = default, DateTimeStyles style = DateTimeStyles.None) { - ParseOperationResult result = TryParseInternal(s, provider, style, out DateOnly dateOnly); - if (result != ParseOperationResult.Success) + ParseFailureKind result = TryParseInternal(s, provider, style, out DateOnly dateOnly); + if (result != ParseFailureKind.None) { - switch (result) - { - case ParseOperationResult.WrongStyles: throw new ArgumentException(SR.Argument_InvalidDateStyles, nameof(style)); - case ParseOperationResult.ParseFailure: throw new FormatException(SR.Format(SR.Format_BadDateOnly, s.ToString())); - default: - Debug.Assert(result == ParseOperationResult.WrongParts); - throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, s.ToString(), nameof(DateOnly))); - } + ThrowOnError(result, s); } return dateOnly; @@ -296,19 +280,11 @@ public static DateOnly Parse(ReadOnlySpan s, IFormatProvider? provider = d /// An object that is equivalent to the date contained in s, as specified by format, provider, and style. public static DateOnly ParseExact(ReadOnlySpan s, ReadOnlySpan format, IFormatProvider? provider = default, DateTimeStyles style = DateTimeStyles.None) { - ParseOperationResult result = TryParseExactInternal(s, format, provider, style, out DateOnly dateOnly); + ParseFailureKind result = TryParseExactInternal(s, format, provider, style, out DateOnly dateOnly); - if (result != ParseOperationResult.Success) + if (result != ParseFailureKind.None) { - switch (result) - { - case ParseOperationResult.WrongStyles: throw new ArgumentException(SR.Argument_InvalidDateStyles, nameof(style)); - case ParseOperationResult.ParseFailure: throw new FormatException(SR.Format(SR.Format_BadDateOnly, s.ToString())); - default: - Debug.Assert(result == ParseOperationResult.WrongParts); - throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, s.ToString(), nameof(DateOnly))); - - } + ThrowOnError(result, s); } return dateOnly; @@ -334,17 +310,10 @@ public static DateOnly ParseExact(ReadOnlySpan s, ReadOnlySpan forma /// An object that is equivalent to the date contained in s, as specified by format, provider, and style. public static DateOnly ParseExact(ReadOnlySpan s, string[] formats, IFormatProvider? provider, DateTimeStyles style = DateTimeStyles.None) { - ParseOperationResult result = TryParseExactInternal(s, formats, provider, style, out DateOnly dateOnly); - if (result != ParseOperationResult.Success) + ParseFailureKind result = TryParseExactInternal(s, formats, provider, style, out DateOnly dateOnly); + if (result != ParseFailureKind.None) { - switch (result) - { - case ParseOperationResult.WrongStyles: throw new ArgumentException(SR.Argument_InvalidDateStyles, nameof(style)); - case ParseOperationResult.ParseFailure: throw new FormatException(SR.Format(SR.Format_BadDateOnly, s.ToString())); - default: - Debug.Assert(result == ParseOperationResult.BadFormatSpecifier); - throw new FormatException(SR.Argument_BadFormatSpecifier); - } + ThrowOnError(result, s); } return dateOnly; @@ -435,14 +404,14 @@ public static DateOnly ParseExact(string s, string[] formats, IFormatProvider? p /// A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is None. /// When this method returns, contains the DateOnly value equivalent to the date contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is empty string, or does not contain a valid string representation of a date. This parameter is passed uninitialized. /// true if the s parameter was converted successfully; otherwise, false. - public static bool TryParse(ReadOnlySpan s, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) => TryParseInternal(s, provider, style, out result) == ParseOperationResult.Success; + public static bool TryParse(ReadOnlySpan s, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) => TryParseInternal(s, provider, style, out result) == ParseFailureKind.None; - private static ParseOperationResult TryParseInternal(ReadOnlySpan s, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) + private static ParseFailureKind TryParseInternal(ReadOnlySpan s, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) { if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0) { result = default; - return ParseOperationResult.WrongStyles; + return ParseFailureKind.FormatWithParameter; } DateTimeResult dtResult = default; @@ -451,17 +420,17 @@ private static ParseOperationResult TryParseInternal(ReadOnlySpan s, IForm if (!DateTimeParse.TryParse(s, DateTimeFormatInfo.GetInstance(provider), style, ref dtResult)) { result = default; - return ParseOperationResult.ParseFailure; + return ParseFailureKind.FormatWithOriginalDateTime; } if ((dtResult.flags & ParseFlagsDateMask) != 0) { result = default; - return ParseOperationResult.WrongParts; + return ParseFailureKind.WrongParts; } result = new DateOnly(DayNumberFromDateTime(dtResult.parsedDate)); - return ParseOperationResult.Success; + return ParseFailureKind.None; } /// @@ -485,13 +454,13 @@ private static ParseOperationResult TryParseInternal(ReadOnlySpan s, IForm /// When this method returns, contains the DateOnly value equivalent to the date contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s is empty string, or does not contain a date that correspond to the pattern specified in format. This parameter is passed uninitialized. /// true if s was converted successfully; otherwise, false. public static bool TryParseExact(ReadOnlySpan s, ReadOnlySpan format, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) => - TryParseExactInternal(s, format, provider, style, out result) == ParseOperationResult.Success; - private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, ReadOnlySpan format, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) + TryParseExactInternal(s, format, provider, style, out result) == ParseFailureKind.None; + private static ParseFailureKind TryParseExactInternal(ReadOnlySpan s, ReadOnlySpan format, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) { if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0) { result = default; - return ParseOperationResult.WrongStyles; + return ParseFailureKind.FormatWithParameter; } if (format.Length == 1) @@ -518,18 +487,18 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, if (!DateTimeParse.TryParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), style, ref dtResult)) { result = default; - return ParseOperationResult.ParseFailure; + return ParseFailureKind.FormatWithOriginalDateTime; } if ((dtResult.flags & ParseFlagsDateMask) != 0) { result = default; - return ParseOperationResult.WrongParts; + return ParseFailureKind.WrongParts; } result = new DateOnly(DayNumberFromDateTime(dtResult.parsedDate)); - return ParseOperationResult.Success; + return ParseFailureKind.None; } /// @@ -551,14 +520,14 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, /// When this method returns, contains the DateOnly value equivalent to the date contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is Empty, or does not contain a valid string representation of a date. This parameter is passed uninitialized. /// true if the s parameter was converted successfully; otherwise, false. public static bool TryParseExact(ReadOnlySpan s, string[] formats, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) => - TryParseExactInternal(s, formats, provider, style, out result) == ParseOperationResult.Success; + TryParseExactInternal(s, formats, provider, style, out result) == ParseFailureKind.None; - private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, string[] formats, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) + private static ParseFailureKind TryParseExactInternal(ReadOnlySpan s, string[] formats, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) { if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0 || formats == null) { result = default; - return ParseOperationResult.WrongStyles; + return ParseFailureKind.FormatWithParameter; } DateTimeFormatInfo dtfi = DateTimeFormatInfo.GetInstance(provider); @@ -570,7 +539,7 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, if (string.IsNullOrEmpty(format)) { result = default; - return ParseOperationResult.BadFormatSpecifier; + return ParseFailureKind.FormatWithFormatSpecifier; } if (format.Length == 1) @@ -598,12 +567,12 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, if (DateTimeParse.TryParseExact(s, format, dtfiToUse, style, ref dtResult) && ((dtResult.flags & ParseFlagsDateMask) == 0)) { result = new DateOnly(DayNumberFromDateTime(dtResult.parsedDate)); - return ParseOperationResult.Success; + return ParseFailureKind.None; } } result = default; - return ParseOperationResult.ParseFailure; + return ParseFailureKind.FormatWithOriginalDateTime; } /// @@ -624,9 +593,9 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, /// true if the s parameter was converted successfully; otherwise, false. public static bool TryParse(string s, IFormatProvider? provider, DateTimeStyles style, out DateOnly result) { - result = default; if (s == null) { + result = default; return false; } @@ -693,6 +662,20 @@ public static bool TryParseExact(string s, string[] formats, IFormatProvider? pr return TryParseExact(s.AsSpan(), formats, provider, style, out result); } + private static void ThrowOnError(ParseFailureKind result, ReadOnlySpan s) + { + Debug.Assert(result != ParseFailureKind.None); + switch (result) + { + case ParseFailureKind.FormatWithParameter: throw new ArgumentException(SR.Argument_InvalidDateStyles, "style"); + case ParseFailureKind.FormatWithOriginalDateTime: throw new FormatException(SR.Format(SR.Format_BadDateOnly, s.ToString())); + case ParseFailureKind.FormatWithFormatSpecifier: throw new FormatException(SR.Argument_BadFormatSpecifier); + default: + Debug.Assert(result == ParseFailureKind.WrongParts); + throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, s.ToString(), nameof(DateOnly))); + } + } + /// /// Converts the value of the current DateOnly object to its equivalent long date string representation. /// @@ -745,23 +728,19 @@ public string ToString(string? format, IFormatProvider? provider) { case 'o': case 'O': + return string.Create(10, this, (destination, value) => { - return string.Create(10, this, (destination, value) => - { - bool b = DateTimeFormat.TryFormatDateOnlyO(value.Year, value.Month, value.Day, destination); - Debug.Assert(b); - }); - } + bool b = DateTimeFormat.TryFormatDateOnlyO(value.Year, value.Month, value.Day, destination); + Debug.Assert(b); + }); case 'r': case 'R': + return string.Create(16, this, (destination, value) => { - return string.Create(16, this, (destination, value) => - { - bool b = DateTimeFormat.TryFormatDateOnlyR(value.DayOfWeek, value.Year, value.Month, value.Day, destination); - Debug.Assert(b); - }); - } + bool b = DateTimeFormat.TryFormatDateOnlyR(value.DayOfWeek, value.Year, value.Month, value.Day, destination); + Debug.Assert(b); + }); case 'm': case 'M': diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs index 75c5b0bf52e809..e4b260885f8251 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs @@ -5903,6 +5903,7 @@ internal enum ParseFailureKind FormatWithFormatSpecifier = 5, FormatWithOriginalDateTimeAndParameter = 6, FormatBadDateTimeCalendar = 7, // FormatException when ArgumentOutOfRange is thrown by a Calendar.TryToDateTime(). + WrongParts = 8, // DateOnly and TimeOnly specific value. Unrelated date parts when parsing DateOnly or Unrelated time parts when parsing TimeOnly } [Flags] diff --git a/src/libraries/System.Private.CoreLib/src/System/TimeOnly.cs b/src/libraries/System.Private.CoreLib/src/System/TimeOnly.cs index 291bc47f73d56d..12c58c0a630860 100644 --- a/src/libraries/System.Private.CoreLib/src/System/TimeOnly.cs +++ b/src/libraries/System.Private.CoreLib/src/System/TimeOnly.cs @@ -331,17 +331,10 @@ public override int GetHashCode() /// An object that is equivalent to the time contained in s, as specified by provider and styles. public static TimeOnly Parse(ReadOnlySpan s, IFormatProvider? provider = default, DateTimeStyles style = DateTimeStyles.None) { - ParseOperationResult result = TryParseInternal(s, provider, style, out TimeOnly timeOnly); - if (result != ParseOperationResult.Success) + ParseFailureKind result = TryParseInternal(s, provider, style, out TimeOnly timeOnly); + if (result != ParseFailureKind.None) { - switch (result) - { - case ParseOperationResult.WrongStyles: throw new ArgumentException(SR.Argument_InvalidDateStyles, nameof(style)); - case ParseOperationResult.ParseFailure: throw new FormatException(SR.Format(SR.Format_BadTimeOnly, s.ToString())); - default: - Debug.Assert(result == ParseOperationResult.WrongParts); - throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, s.ToString(), nameof(TimeOnly))); - } + ThrowOnError(result, s); } return timeOnly; @@ -361,17 +354,10 @@ public static TimeOnly Parse(ReadOnlySpan s, IFormatProvider? provider = d /// An object that is equivalent to the time contained in s, as specified by format, provider, and style. public static TimeOnly ParseExact(ReadOnlySpan s, ReadOnlySpan format, IFormatProvider? provider = default, DateTimeStyles style = DateTimeStyles.None) { - ParseOperationResult result = TryParseExactInternal(s, format, provider, style, out TimeOnly timeOnly); - if (result != ParseOperationResult.Success) + ParseFailureKind result = TryParseExactInternal(s, format, provider, style, out TimeOnly timeOnly); + if (result != ParseFailureKind.None) { - switch (result) - { - case ParseOperationResult.WrongStyles: throw new ArgumentException(SR.Argument_InvalidDateStyles, nameof(style)); - case ParseOperationResult.ParseFailure: throw new FormatException(SR.Format(SR.Format_BadTimeOnly, s.ToString())); - default: - Debug.Assert(result == ParseOperationResult.WrongParts); - throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, s.ToString(), nameof(TimeOnly))); - } + ThrowOnError(result, s); } return timeOnly; @@ -397,17 +383,10 @@ public static TimeOnly ParseExact(ReadOnlySpan s, ReadOnlySpan forma /// An object that is equivalent to the time contained in s, as specified by format, provider, and style. public static TimeOnly ParseExact(ReadOnlySpan s, string[] formats, IFormatProvider? provider, DateTimeStyles style = DateTimeStyles.None) { - ParseOperationResult result = TryParseExactInternal(s, formats, provider, style, out TimeOnly timeOnly); - if (result != ParseOperationResult.Success) + ParseFailureKind result = TryParseExactInternal(s, formats, provider, style, out TimeOnly timeOnly); + if (result != ParseFailureKind.None) { - switch (result) - { - case ParseOperationResult.WrongStyles: throw new ArgumentException(SR.Argument_InvalidDateStyles, nameof(style)); - case ParseOperationResult.ParseFailure: throw new FormatException(SR.Format(SR.Format_BadTimeOnly, s.ToString())); - default: - Debug.Assert(result == ParseOperationResult.BadFormatSpecifier); - throw new FormatException(SR.Argument_BadFormatSpecifier); - } + ThrowOnError(result, s); } return timeOnly; @@ -499,13 +478,13 @@ public static TimeOnly ParseExact(string s, string[] formats, IFormatProvider? p /// When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is empty string, or does not contain a valid string representation of a date. This parameter is passed uninitialized. /// true if the s parameter was converted successfully; otherwise, false. public static bool TryParse(ReadOnlySpan s, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) => - TryParseInternal(s, provider, style, out result) == ParseOperationResult.Success; - private static ParseOperationResult TryParseInternal(ReadOnlySpan s, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) + TryParseInternal(s, provider, style, out result) == ParseFailureKind.None; + private static ParseFailureKind TryParseInternal(ReadOnlySpan s, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) { if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0) { result = default; - return ParseOperationResult.WrongStyles; + return ParseFailureKind.FormatWithParameter; } DateTimeResult dtResult = default; @@ -515,18 +494,18 @@ private static ParseOperationResult TryParseInternal(ReadOnlySpan s, IForm if (!DateTimeParse.TryParse(s, DateTimeFormatInfo.GetInstance(provider), style, ref dtResult)) { result = default; - return ParseOperationResult.ParseFailure; + return ParseFailureKind.FormatWithOriginalDateTime; } if ((dtResult.flags & ParseFlagsTimeMask) != 0) { result = default; - return ParseOperationResult.WrongParts; + return ParseFailureKind.WrongParts; } result = new TimeOnly(dtResult.parsedDate.TimeOfDay.Ticks); - return ParseOperationResult.Success; + return ParseFailureKind.None; } /// @@ -550,14 +529,14 @@ private static ParseOperationResult TryParseInternal(ReadOnlySpan s, IForm /// When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s is empty string, or does not contain a time that correspond to the pattern specified in format. This parameter is passed uninitialized. /// true if s was converted successfully; otherwise, false. public static bool TryParseExact(ReadOnlySpan s, ReadOnlySpan format, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) => - TryParseExactInternal(s, format, provider, style, out result) == ParseOperationResult.Success; + TryParseExactInternal(s, format, provider, style, out result) == ParseFailureKind.None; - private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, ReadOnlySpan format, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) + private static ParseFailureKind TryParseExactInternal(ReadOnlySpan s, ReadOnlySpan format, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) { if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0) { result = default; - return ParseOperationResult.WrongStyles; + return ParseFailureKind.FormatWithParameter; } if (format.Length == 1) @@ -584,18 +563,18 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, if (!DateTimeParse.TryParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), style, ref dtResult)) { result = default; - return ParseOperationResult.ParseFailure; + return ParseFailureKind.FormatWithOriginalDateTime; } if ((dtResult.flags & ParseFlagsTimeMask) != 0) { result = default; - return ParseOperationResult.WrongParts; + return ParseFailureKind.WrongParts; } result = new TimeOnly(dtResult.parsedDate.TimeOfDay.Ticks); - return ParseOperationResult.Success; + return ParseFailureKind.None; } /// @@ -617,14 +596,14 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, /// When this method returns, contains the TimeOnly value equivalent to the time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is Empty, or does not contain a valid string representation of a time. This parameter is passed uninitialized. /// true if the s parameter was converted successfully; otherwise, false. public static bool TryParseExact(ReadOnlySpan s, string[] formats, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) => - TryParseExactInternal(s, formats, provider, style, out result) == ParseOperationResult.Success; + TryParseExactInternal(s, formats, provider, style, out result) == ParseFailureKind.None; - private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, string[] formats, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) + private static ParseFailureKind TryParseExactInternal(ReadOnlySpan s, string[] formats, IFormatProvider? provider, DateTimeStyles style, out TimeOnly result) { if ((style & ~DateTimeStyles.AllowWhiteSpaces) != 0 || formats == null) { result = default; - return ParseOperationResult.WrongStyles; + return ParseFailureKind.FormatWithParameter; } DateTimeFormatInfo dtfi = DateTimeFormatInfo.GetInstance(provider); @@ -636,7 +615,7 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, if (string.IsNullOrEmpty(format)) { result = default; - return ParseOperationResult.BadFormatSpecifier; + return ParseFailureKind.FormatWithFormatSpecifier; } if (format.Length == 1) @@ -664,12 +643,12 @@ private static ParseOperationResult TryParseExactInternal(ReadOnlySpan s, if (DateTimeParse.TryParseExact(s, format, dtfiToUse, style, ref dtResult) && ((dtResult.flags & ParseFlagsTimeMask) == 0)) { result = new TimeOnly(dtResult.parsedDate.TimeOfDay.Ticks); - return ParseOperationResult.Success; + return ParseFailureKind.None; } } result = default; - return ParseOperationResult.ParseFailure; + return ParseFailureKind.FormatWithOriginalDateTime; } /// @@ -759,6 +738,20 @@ public static bool TryParseExact(string s, string[] formats, IFormatProvider? pr return TryParseExact(s.AsSpan(), formats, provider, style, out result); } + private static void ThrowOnError(ParseFailureKind result, ReadOnlySpan s) + { + Debug.Assert(result != ParseFailureKind.None); + switch (result) + { + case ParseFailureKind.FormatWithParameter: throw new ArgumentException(SR.Argument_InvalidDateStyles, "style"); + case ParseFailureKind.FormatWithOriginalDateTime: throw new FormatException(SR.Format(SR.Format_BadTimeOnly, s.ToString())); + case ParseFailureKind.FormatWithFormatSpecifier: throw new FormatException(SR.Argument_BadFormatSpecifier); + default: + Debug.Assert(result == ParseFailureKind.WrongParts); + throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, s.ToString(), nameof(TimeOnly))); + } + } + /// /// Converts the value of the current TimeOnly object to its equivalent long date string representation. /// @@ -813,23 +806,19 @@ public string ToString(string? format, IFormatProvider? provider) { case 'o': case 'O': + return string.Create(16, this, (destination, value) => { - return string.Create(16, this, (destination, value) => - { - bool b = DateTimeFormat.TryFormatTimeOnlyO(value.Hour, value.Minute, value.Second, value._ticks % TimeSpan.TicksPerSecond, destination); - Debug.Assert(b); - }); - } + bool b = DateTimeFormat.TryFormatTimeOnlyO(value.Hour, value.Minute, value.Second, value._ticks % TimeSpan.TicksPerSecond, destination); + Debug.Assert(b); + }); case 'r': case 'R': + return string.Create(8, this, (destination, value) => { - return string.Create(8, this, (destination, value) => - { - bool b = DateTimeFormat.TryFormatTimeOnlyR(value.Hour, value.Minute, value.Second, destination); - Debug.Assert(b); - }); - } + bool b = DateTimeFormat.TryFormatTimeOnlyR(value.Hour, value.Minute, value.Second, destination); + Debug.Assert(b); + }); case 't': case 'T':