This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Added custom DateTimeFormatter #716
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.Net.Http.Headers | ||
{ | ||
internal static class DateTimeFormatter | ||
{ | ||
private static readonly DateTimeFormatInfo FormatInfo = CultureInfo.InvariantCulture.DateTimeFormat; | ||
|
||
private static readonly string[] MonthNames = FormatInfo.AbbreviatedMonthNames; | ||
private static readonly string[] DayNames = FormatInfo.AbbreviatedDayNames; | ||
|
||
private static readonly int Rfc1123DateLength = "ddd, dd MMM yyyy HH:mm:ss GMT".Length; | ||
private static readonly int QuotedRfc1123DateLength = Rfc1123DateLength + 2; | ||
|
||
// ASCII numbers are in the range 48 - 57. | ||
private const int AsciiNumberOffset = 0x30; | ||
|
||
private const string Gmt = "GMT"; | ||
private const char Comma = ','; | ||
private const char Space = ' '; | ||
private const char Colon = ':'; | ||
private const char Quote = '"'; | ||
|
||
public static string ToRfc1123String(this DateTimeOffset dateTime) | ||
{ | ||
return ToRfc1123String(dateTime, false); | ||
} | ||
|
||
public static string ToRfc1123String(this DateTimeOffset dateTime, bool quoted) | ||
{ | ||
var universal = dateTime.UtcDateTime; | ||
|
||
var length = quoted ? QuotedRfc1123DateLength : Rfc1123DateLength; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. QuotedRfc1123DateLength is only used once, you might as well put the +2 here. |
||
var target = new InplaceStringBuilder(length); | ||
|
||
if (quoted) | ||
{ | ||
target.Append(Quote); | ||
} | ||
|
||
target.Append(DayNames[(int)universal.DayOfWeek]); | ||
target.Append(Comma); | ||
target.Append(Space); | ||
AppendNumber(ref target, universal.Day); | ||
target.Append(Space); | ||
target.Append(MonthNames[universal.Month - 1]); | ||
target.Append(Space); | ||
AppendYear(ref target, universal.Year); | ||
target.Append(Space); | ||
AppendTimeOfDay(ref target, universal.TimeOfDay); | ||
target.Append(Space); | ||
target.Append(Gmt); | ||
|
||
if (quoted) | ||
{ | ||
target.Append(Quote); | ||
} | ||
|
||
return target.ToString(); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static void AppendYear(ref InplaceStringBuilder target, int year) | ||
{ | ||
target.Append(GetAsciiChar(year / 1000)); | ||
target.Append(GetAsciiChar(year % 1000 / 100)); | ||
target.Append(GetAsciiChar(year % 100 / 10)); | ||
target.Append(GetAsciiChar(year % 10)); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static void AppendTimeOfDay(ref InplaceStringBuilder target, TimeSpan timeOfDay) | ||
{ | ||
AppendNumber(ref target, timeOfDay.Hours); | ||
target.Append(Colon); | ||
AppendNumber(ref target, timeOfDay.Minutes); | ||
target.Append(Colon); | ||
AppendNumber(ref target, timeOfDay.Seconds); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static void AppendNumber(ref InplaceStringBuilder target, int number) | ||
{ | ||
target.Append(GetAsciiChar(number / 10)); | ||
target.Append(GetAsciiChar(number % 10)); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static char GetAsciiChar(int value) | ||
{ | ||
return (char)(AsciiNumberOffset + value); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
test/Microsoft.Net.Http.Headers.Tests/HeaderUtilitiesTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Xunit; | ||
|
||
namespace Microsoft.Net.Http.Headers | ||
{ | ||
public static class HeaderUtilitiesTest | ||
{ | ||
private const string Rfc1123Format = "r"; | ||
|
||
[Theory] | ||
[MemberData(nameof(TestValues))] | ||
public static void ReturnsSameResultAsRfc1123String(DateTimeOffset dateTime, bool quoted) | ||
{ | ||
var formatted = dateTime.ToString(Rfc1123Format); | ||
var expected = quoted ? $"\"{formatted}\"" : formatted; | ||
var actual = HeaderUtilities.FormatDate(dateTime, quoted); | ||
|
||
Assert.Equal(expected, actual); | ||
} | ||
|
||
public static TheoryData<DateTimeOffset, bool> TestValues | ||
{ | ||
get | ||
{ | ||
var data = new TheoryData<DateTimeOffset, bool>(); | ||
|
||
var now = DateTimeOffset.Now; | ||
|
||
foreach (var quoted in new[] { true, false }) | ||
{ | ||
for (var i = 0; i < 60; i++) | ||
{ | ||
data.Add(now.AddSeconds(i), quoted); | ||
data.Add(now.AddMinutes(i), quoted); | ||
data.Add(now.AddDays(i), quoted); | ||
data.Add(now.AddMonths(i), quoted); | ||
data.Add(now.AddYears(i), quoted); | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure of the level of optimization that's really required for this code, but a few off the cuff thoughts:
foreach
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about now?
@hallatore did some testing with
Buffer.Memcpy
earlier today. It didn't change much. He also verified that stuff was inlined nicely.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was testing changing to foreach to use Buffer.memcpy, but the results where identical.
Ref memcpy: https://gist.github.com/hallatore/a10ffd0d69a508d09b74a4f720c0511a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also consider vectorizing this where the lengths are known, using
(int *)
or(long *)
. I think that what's there is probably fine, and this is important it would come up again.