Skip to content
Open
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,22 @@ internal static unsafe string Format(DateTime dateTime, string? format, IFormatP
format = dtfi.FullDateTimePattern;
break;

// For invariant DateTime ToString("G"), the expanded pattern is
// "MM/dd/yyyy HH:mm:ss" which is exactly what TryFormatInvariantG
// produces in the NullOffset case. Take the same fast path that
// ToString(InvariantCulture) (null format) already uses.
case 'G':
dtfi = DateTimeFormatInfo.GetInstance(provider);
if (offset.Ticks == NullOffset && ReferenceEquals(dtfi, DateTimeFormatInfo.InvariantInfo))
{
str = string.FastAllocateString(FormatInvariantGMinLength);
TryFormatInvariantG(dateTime, offset, new Span<char>(ref str.GetRawStringData(), str.Length), out charsWritten);
Debug.Assert(charsWritten == FormatInvariantGMinLength);
return str;
}
format = dtfi.GeneralLongTimePattern;
break;

// All other standard formats
default:
dtfi = DateTimeFormatInfo.GetInstance(provider);
Expand Down Expand Up @@ -1098,6 +1114,19 @@ internal static bool TryFormat<TChar>(DateTime dateTime, Span<TChar> destination
format = dtfi.FullDateTimePattern;
break;

// For invariant DateTime ToString("G"), the expanded pattern is
// "MM/dd/yyyy HH:mm:ss" which is exactly what TryFormatInvariantG
// produces in the NullOffset case. Take the same fast path that
// ToString(InvariantCulture) (null format) already uses.
case 'G':
dtfi = DateTimeFormatInfo.GetInstance(provider);
if (offset.Ticks == NullOffset && ReferenceEquals(dtfi, DateTimeFormatInfo.InvariantInfo))
{
return TryFormatInvariantG(dateTime, offset, destination, out charsWritten);
}
format = dtfi.GeneralLongTimePattern;
break;

// All other standard formats
default:
dtfi = DateTimeFormatInfo.GetInstance(provider);
Expand Down
Loading