class DateTimeFormat
{
internal const int MaxSecondsFractionDigits = 7;
- internal static readonly TimeSpan NullOffset = TimeSpan.MinValue;
+ internal const long NullOffset = long.MinValue;
internal static char[] allStandardFormats =
{
private static void FormatCustomizedTimeZone(DateTime dateTime, TimeSpan offset, int tokenLen, bool timeOnly, StringBuilder result)
{
// See if the instance already has an offset
- bool dateTimeFormat = (offset == NullOffset);
+ bool dateTimeFormat = (offset.Ticks == NullOffset);
if (dateTimeFormat)
{
// No offset. The instance is a DateTime and the output should be the local time zone
}
else if (dateTime.Kind == DateTimeKind.Utc)
{
- offset = TimeSpan.Zero;
+ offset = default; // TimeSpan.Zero
}
else
{
offset = TimeZoneInfo.GetLocalUtcOffset(dateTime, TimeZoneInfoOptions.NoThrowOnInvalidTime);
}
}
- if (offset >= TimeSpan.Zero)
+ if (offset.Ticks >= 0)
{
result.Append('+');
}
// For DateTime it should round-trip the Kind value and preserve the time zone.
// DateTimeOffset instance, it should do so by using the internal time zone.
- if (offset == NullOffset)
+ if (offset.Ticks == NullOffset)
{
// source is a date time, so behavior depends on the kind.
switch (dateTime.Kind)
return;
}
}
- if (offset >= TimeSpan.Zero)
+ if (offset.Ticks >= 0)
{
result.Append('+');
}
// This method also convert the dateTime if necessary (e.g. when the format is in Universal time),
// and change dtfi if necessary (e.g. when the format should use invariant culture).
//
- private static string ExpandPredefinedFormat(ReadOnlySpan<char> format, ref DateTime dateTime, ref DateTimeFormatInfo dtfi, ref TimeSpan offset)
+ private static string ExpandPredefinedFormat(ReadOnlySpan<char> format, ref DateTime dateTime, ref DateTimeFormatInfo dtfi, TimeSpan offset)
{
switch (format[0])
{
case 'r':
case 'R': // RFC 1123 Standard
case 'u': // Universal time in sortable format.
- if (offset != NullOffset)
+ if (offset.Ticks != NullOffset)
{
// Convert to UTC invariants mean this will be in range
dateTime -= offset;
dtfi = DateTimeFormatInfo.InvariantInfo;
break;
case 'U': // Universal time in culture dependent format.
- if (offset != NullOffset)
+ if (offset.Ticks != NullOffset)
{
// This format is not supported by DateTimeOffset
throw new FormatException(SR.Format_InvalidString);
internal static string Format(DateTime dateTime, string? format, IFormatProvider? provider)
{
- return Format(dateTime, format, provider, NullOffset);
+ return Format(dateTime, format, provider, new TimeSpan(NullOffset));
}
internal static string Format(DateTime dateTime, string? format, IFormatProvider? provider, TimeSpan offset)
}
internal static bool TryFormat(DateTime dateTime, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
- TryFormat(dateTime, destination, out charsWritten, format, provider, NullOffset);
+ TryFormat(dateTime, destination, out charsWritten, format, provider, new TimeSpan(NullOffset));
internal static bool TryFormat(DateTime dateTime, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider, TimeSpan offset)
{
break;
}
}
- if (offset == NullOffset)
+ if (offset.Ticks == NullOffset)
{
// Default DateTime.ToString case.
format = timeOnlySpecialCase ? "s" : "G";
if (format.Length == 1)
{
- format = ExpandPredefinedFormat(format, ref dateTime, ref dtfi, ref offset);
+ format = ExpandPredefinedFormat(format, ref dateTime, ref dtfi, offset);
}
return FormatCustomized(dateTime, format, dtfi, offset, result: null);
int charsRequired = MinimumBytesNeeded;
DateTimeKind kind = DateTimeKind.Local;
- if (offset == NullOffset)
+ if (offset.Ticks == NullOffset)
{
kind = dateTime.Kind;
if (kind == DateTimeKind.Local)
return false;
}
- if (offset != NullOffset)
+ if (offset.Ticks != NullOffset)
{
// Convert to UTC invariants.
dateTime -= offset;