Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
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
Prev Previous commit
Next Next commit
fixing DateTimeOffset
  • Loading branch information
Petermarcu committed Oct 28, 2016
commit 1321303ba77058eca68a7bdc0a9eb5db2a2035c7
39 changes: 8 additions & 31 deletions src/mscorlib/src/System/Globalization/DateTimeFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,9 @@ private static void FormatCustomizedRoundripTimeZone(DateTime dateTime, TimeSpan
offset = offset.Negate();
}

result.AppendFormat(CultureInfo.InvariantCulture, "{0:00}:{1:00}", offset.Hours, offset.Minutes);
AppendNumber(result, offset.Hours, 2);
result.Append(':');
AppendNumber(result, offset.Minutes, 2);
}


Expand Down Expand Up @@ -958,7 +960,7 @@ internal static String Format(DateTime dateTime, String format, DateTimeFormatIn

if (format.Length == 1) {
if (format[0] == 'o' || format[0] == 'O') {
return FastFormatRoundtrip(dateTime);
return FastFormatRoundtrip(dateTime, offset);
}

format = ExpandPredefinedFormat(format, ref dateTime, ref dtfi, ref offset);
Expand All @@ -967,9 +969,10 @@ internal static String Format(DateTime dateTime, String format, DateTimeFormatIn
return (FormatCustomized(dateTime, format, dtfi, offset));
}

internal static string FastFormatRoundtrip(DateTime dateTime)
internal static string FastFormatRoundtrip(DateTime dateTime, TimeSpan offset)
{
StringBuilder result = StringBuilderCache.Acquire();

AppendNumber(result, dateTime.Year, 4);
result.Append('-');
AppendNumber(result, dateTime.Month, 2);
Expand All @@ -984,39 +987,13 @@ internal static string FastFormatRoundtrip(DateTime dateTime)
result.Append('.');

long fraction = dateTime.Ticks % TimeSpan.TicksPerSecond;

AppendNumber(result, fraction, 7);

switch (dateTime.Kind)
{
case DateTimeKind.Local:
{
TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(dateTime);

if (offset >= TimeSpan.Zero)
{
result.Append('+');
}
else
{
result.Append('-');
offset = offset.Negate();
}

AppendNumber(result, offset.Hours, 2);
result.Append(':');
AppendNumber(result, offset.Minutes, 2);
}
break;

case DateTimeKind.Utc:
result.Append('Z');
break;
}
FormatCustomizedRoundripTimeZone(dateTime, offset, result);

return StringBuilderCache.GetStringAndRelease(result);
}

internal static void AppendNumber(StringBuilder builder, long val, int digits)
{
for (int i = 0; i < digits; i++)
Expand Down