-
Notifications
You must be signed in to change notification settings - Fork 191
Added custom DateTimeFormatter #716
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
"frameworks": { | ||
"netstandard1.1": { | ||
"dependencies": { | ||
"Microsoft.Extensions.Primitives": "1.1.0-*", | ||
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. This goes under the root dependencies, it's not framework specific. 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. What about 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. Move them all 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. Shouldn't 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. Why? 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. Doesn't it pull down lots of stuff you don't need when you target 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. Sure, once 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. But does it hurt to move it under |
||
"System.Buffers": "4.3.0-*", | ||
"System.Diagnostics.Contracts": "4.3.0-*" | ||
} | ||
|
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; | ||
} | ||
} | ||
} | ||
} |
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
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.