Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/DateOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,15 +817,13 @@ public string ToString(string? format, IFormatProvider? provider)
return DateTimeFormat.TryFormat(GetEquivalentDateTime(), destination, out charsWritten, format, provider);

default:
charsWritten = 0;
return false;
throw new FormatException(SR.Argument_BadFormatSpecifier);
}
}

if (!DateTimeFormat.IsValidCustomDateFormat(format, throwOnError: false))
{
charsWritten = 0;
return false;
throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, format.ToString(), nameof(DateOnly)));
}

return DateTimeFormat.TryFormat(GetEquivalentDateTime(), destination, out charsWritten, format, provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ Patterns Format Description Example
"M" "0" month w/o leading zero 2
"MM" "00" month with leading zero 02
"MMM" short month name (abbreviation) Feb
"MMMM" full month name Febuary
"MMMM*" full month name Febuary
"MMMM" full month name February
"MMMM*" full month name February

"y" "0" two digit year (year % 100) w/o leading zero 0
"yy" "00" two digit year (year % 100) with leading zero 00
Expand Down Expand Up @@ -734,7 +734,7 @@ private static StringBuilder FormatCustomized(
break;
case '\\':
// Escaped character. Can be used to insert a character into the format string.
// For exmple, "\d" will insert the character 'd' into the string.
// For example, "\d" will insert the character 'd' into the string.
//
// NOTENOTE : we can remove this format character if we enforce the enforced quote
// character rule.
Expand Down Expand Up @@ -966,7 +966,7 @@ private static string ExpandPredefinedFormat(ReadOnlySpan<char> format, ref Date
// This format is not supported by DateTimeOffset
throw new FormatException(SR.Format_InvalidString);
}
// Universal time is always in Greogrian calendar.
// Universal time is always in Gregorian calendar.
//
// Change the Calendar to be Gregorian Calendar.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface ISpanFormattable : IFormattable
/// <remarks>
/// An implementation of this interface should produce the same string of characters as an implementation of <see cref="IFormattable.ToString(string?, IFormatProvider?)"/>
/// on the same type.
/// TryFormat should return false only if there is not enough space in the destination buffer. Any other failures should throw an exception.
/// </remarks>
bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider);
}
Expand Down
6 changes: 2 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/TimeOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -892,15 +892,13 @@ public string ToString(string? format, IFormatProvider? provider)
return DateTimeFormat.TryFormat(ToDateTime(), destination, out charsWritten, format, provider);

default:
charsWritten = 0;
return false;
throw new FormatException(SR.Argument_BadFormatSpecifier);
}
}

if (!DateTimeFormat.IsValidCustomTimeFormat(format, throwOnError: false))
{
charsWritten = 0;
return false;
throw new FormatException(SR.Format(SR.Format_DateTimeOnlyContainsNoneDateParts, format.ToString(), nameof(TimeOnly)));
}

return DateTimeFormat.TryFormat(ToDateTime(), destination, out charsWritten, format, provider);
Expand Down
8 changes: 8 additions & 0 deletions src/libraries/System.Runtime/tests/System/DateOnlyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,5 +514,13 @@ public static void TryFormatTest()
Assert.False(dateOnly.TryFormat(buffer.Slice(0, 3), out charsWritten, "r"));
Assert.False(dateOnly.TryFormat(buffer.Slice(0, 3), out charsWritten, "O"));
}

[Fact]
public static void InvalidFormattingWithInterpolatedStringTest()
{
DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today);
Assert.Throws<FormatException>(() => $"{dateOnly:u}");
Assert.Throws<FormatException>(() => $"{dateOnly:hh-ss}");
}
}
}
8 changes: 8 additions & 0 deletions src/libraries/System.Runtime/tests/System/TimeOnlyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,13 @@ public static void TryFormatTest()
Assert.False(timeOnly.TryFormat(buffer.Slice(0, 3), out charsWritten, "r"));
Assert.False(timeOnly.TryFormat(buffer.Slice(0, 3), out charsWritten, "O"));
}

[Fact]
public static void InvalidFormattingWithInterpolatedStringTest()
{
TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now);
Assert.Throws<FormatException>(() => $"{timeOnly:u}");
Assert.Throws<FormatException>(() => $"{timeOnly:dd-yyyy}");
}
}
}