Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e20df05

Browse files
committedSep 30, 2016
Added DateTimeFormatter
1 parent 067eb9c commit e20df05

File tree

7 files changed

+210
-9
lines changed

7 files changed

+210
-9
lines changed
 

‎src/Microsoft.Net.Http.Headers/ContentDispositionHeaderValue.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ private void SetDate(string parameter, DateTimeOffset? date)
321321
else
322322
{
323323
// Must always be quoted
324-
var dateString = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", HttpRuleParser.DateToString(date.Value));
324+
var dateString = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", HeaderUtilities.FormatDate(date.Value));
325325
if (dateParameter != null)
326326
{
327327
dateParameter.Value = dateString;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Globalization;
6+
using static System.Text.Encoding;
7+
8+
namespace Microsoft.Net.Http.Headers
9+
{
10+
internal static class DateTimeFormatter
11+
{
12+
private static readonly DateTimeFormatInfo FormatInfo = CultureInfo.InvariantCulture.DateTimeFormat;
13+
14+
private static readonly byte[] MonBytes = UTF8.GetBytes(GetDayName(DayOfWeek.Monday));
15+
private static readonly byte[] TueBytes = UTF8.GetBytes(GetDayName(DayOfWeek.Tuesday));
16+
private static readonly byte[] WedBytes = UTF8.GetBytes(GetDayName(DayOfWeek.Wednesday));
17+
private static readonly byte[] ThuBytes = UTF8.GetBytes(GetDayName(DayOfWeek.Thursday));
18+
private static readonly byte[] FriBytes = UTF8.GetBytes(GetDayName(DayOfWeek.Friday));
19+
private static readonly byte[] SatBytes = UTF8.GetBytes(GetDayName(DayOfWeek.Saturday));
20+
private static readonly byte[] SunBytes = UTF8.GetBytes(GetDayName(DayOfWeek.Sunday));
21+
22+
private static readonly byte[] JanBytes = UTF8.GetBytes(GetMonthName(1));
23+
private static readonly byte[] FebBytes = UTF8.GetBytes(GetMonthName(2));
24+
private static readonly byte[] MarBytes = UTF8.GetBytes(GetMonthName(3));
25+
private static readonly byte[] AprBytes = UTF8.GetBytes(GetMonthName(4));
26+
private static readonly byte[] MayBytes = UTF8.GetBytes(GetMonthName(5));
27+
private static readonly byte[] JunBytes = UTF8.GetBytes(GetMonthName(6));
28+
private static readonly byte[] JulBytes = UTF8.GetBytes(GetMonthName(7));
29+
private static readonly byte[] AugBytes = UTF8.GetBytes(GetMonthName(8));
30+
private static readonly byte[] SepBytes = UTF8.GetBytes(GetMonthName(9));
31+
private static readonly byte[] OctBytes = UTF8.GetBytes(GetMonthName(10));
32+
private static readonly byte[] NovBytes = UTF8.GetBytes(GetMonthName(11));
33+
private static readonly byte[] DecBytes = UTF8.GetBytes(GetMonthName(12));
34+
35+
private static readonly byte[] GmtBytes = UTF8.GetBytes("GMT");
36+
37+
// The format is "ddd, dd MMM yyyy HH:mm:ss GMT".
38+
private const int Rfc1123DateLength = 29;
39+
40+
// ASCII numbers are in the range 48 - 57.
41+
private const int AsciiNumberOffset = 0x30;
42+
43+
private const char Comma = ',';
44+
private const char Space = ' ';
45+
private const char Colon = ':';
46+
47+
public static unsafe string ToRfc1123String(this DateTimeOffset dateTime)
48+
{
49+
var offset = 0;
50+
char* target = stackalloc char[Rfc1123DateLength];
51+
var universalDateTime = dateTime.ToUniversalTime();
52+
53+
offset = FormatDayOfWeek(universalDateTime.DayOfWeek, target, offset);
54+
offset = Format(Comma, target, offset);
55+
offset = Format(Space, target, offset);
56+
offset = FormatNumber(universalDateTime.Day, target, offset);
57+
offset = Format(Space, target, offset);
58+
offset = FormatMonth(universalDateTime.Month, target, offset);
59+
offset = Format(Space, target, offset);
60+
offset = FormatYear(universalDateTime.Year, target, offset);
61+
offset = Format(Space, target, offset);
62+
offset = FormatTimeOfDay(universalDateTime.TimeOfDay, target, offset);
63+
offset = Format(Space, target, offset);
64+
offset = Format(GmtBytes, target, offset);
65+
66+
return new string(target, 0, offset);
67+
}
68+
69+
private static unsafe int FormatDayOfWeek(DayOfWeek dayOfWeek, char* target, int offset)
70+
{
71+
switch (dayOfWeek)
72+
{
73+
case DayOfWeek.Sunday: return Format(SunBytes, target, offset);
74+
case DayOfWeek.Monday: return Format(MonBytes, target, offset);
75+
case DayOfWeek.Tuesday: return Format(TueBytes, target, offset);
76+
case DayOfWeek.Wednesday: return Format(WedBytes, target, offset);
77+
case DayOfWeek.Thursday: return Format(ThuBytes, target, offset);
78+
case DayOfWeek.Friday: return Format(FriBytes, target, offset);
79+
case DayOfWeek.Saturday: return Format(SatBytes, target, offset);
80+
default: return offset;
81+
}
82+
}
83+
84+
private static unsafe int FormatMonth(int month, char* target, int offset)
85+
{
86+
switch (month)
87+
{
88+
case 1: return Format(JanBytes, target, offset);
89+
case 2: return Format(FebBytes, target, offset);
90+
case 3: return Format(MarBytes, target, offset);
91+
case 4: return Format(AprBytes, target, offset);
92+
case 5: return Format(MayBytes, target, offset);
93+
case 6: return Format(JunBytes, target, offset);
94+
case 7: return Format(JulBytes, target, offset);
95+
case 8: return Format(AugBytes, target, offset);
96+
case 9: return Format(SepBytes, target, offset);
97+
case 10: return Format(OctBytes, target, offset);
98+
case 11: return Format(NovBytes, target, offset);
99+
case 12: return Format(DecBytes, target, offset);
100+
default: return offset;
101+
}
102+
}
103+
104+
private static unsafe int FormatYear(int year, char* target, int offset)
105+
{
106+
offset = Format(GetAsciiChar(year / 1000), target, offset);
107+
offset = Format(GetAsciiChar(year % 1000 / 100), target, offset);
108+
offset = Format(GetAsciiChar(year % 100 / 10), target, offset);
109+
offset = Format(GetAsciiChar(year % 10), target, offset);
110+
return offset;
111+
}
112+
113+
private static unsafe int FormatTimeOfDay(TimeSpan timeOfDay, char* target, int offset)
114+
{
115+
offset = FormatNumber(timeOfDay.Hours, target, offset);
116+
offset = Format(Colon, target, offset);
117+
offset = FormatNumber(timeOfDay.Minutes, target, offset);
118+
offset = Format(Colon, target, offset);
119+
offset = FormatNumber(timeOfDay.Seconds, target, offset);
120+
return offset;
121+
}
122+
123+
private static unsafe int FormatNumber(int number, char* target, int offset)
124+
{
125+
offset = Format(GetAsciiChar(number / 10), target, offset);
126+
offset = Format(GetAsciiChar(number % 10), target, offset);
127+
return offset;
128+
}
129+
130+
private static unsafe int Format(byte[] source, char* target, int offset)
131+
{
132+
foreach (var b in source)
133+
{
134+
offset = Format((char) b, target, offset);
135+
}
136+
137+
return offset;
138+
}
139+
140+
private static unsafe int Format(char value, char* target, int offset)
141+
{
142+
target[offset++] = value;
143+
return offset;
144+
}
145+
146+
private static char GetAsciiChar(int value)
147+
{
148+
return (char)(AsciiNumberOffset + value);
149+
}
150+
151+
private static string GetDayName(DayOfWeek dayOfWeek)
152+
{
153+
return FormatInfo.GetAbbreviatedDayName(dayOfWeek);
154+
}
155+
156+
private static string GetMonthName(int month)
157+
{
158+
return FormatInfo.GetAbbreviatedMonthName(month);
159+
}
160+
}
161+
}

‎src/Microsoft.Net.Http.Headers/HeaderUtilities.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public static bool TryParseDate(string input, out DateTimeOffset result)
220220

221221
public static string FormatDate(DateTimeOffset dateTime)
222222
{
223-
return HttpRuleParser.DateToString(dateTime);
223+
return dateTime.ToRfc1123String();
224224
}
225225

226226
public static string RemoveQuotes(string input)

‎src/Microsoft.Net.Http.Headers/HttpRuleParser.cs

-6
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,6 @@ internal static HttpParseResult GetQuotedPairLength(string input, int startIndex
233233
return HttpParseResult.Parsed;
234234
}
235235

236-
internal static string DateToString(DateTimeOffset dateTime)
237-
{
238-
// Format according to RFC1123; 'r' uses invariant info (DateTimeFormatInfo.InvariantInfo)
239-
return dateTime.ToUniversalTime().ToString("r", CultureInfo.InvariantCulture);
240-
}
241-
242236
internal static bool TryStringToDate(string input, out DateTimeOffset result)
243237
{
244238
// Try the various date formats in the order listed above.

‎src/Microsoft.Net.Http.Headers/RangeConditionHeaderValue.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override string ToString()
5353
{
5454
if (_entityTag == null)
5555
{
56-
return HttpRuleParser.DateToString(_lastModified.Value);
56+
return HeaderUtilities.FormatDate(_lastModified.Value);
5757
}
5858
return _entityTag.ToString();
5959
}

‎src/Microsoft.Net.Http.Headers/project.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"buildOptions": {
1414
"warningsAsErrors": true,
15+
"allowUnsafe": true,
1516
"keyFile": "../../tools/Key.snk",
1617
"nowarn": [
1718
"CS1591"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Globalization;
6+
using Xunit;
7+
8+
namespace Microsoft.Net.Http.Headers
9+
{
10+
public static class HeaderUtilitiesTest
11+
{
12+
private const string Rfc1123Format = "r";
13+
14+
[Theory]
15+
[MemberData(nameof(TestValues))]
16+
public static void ReturnsSameResultAsRfc1123String(DateTimeOffset dateTime)
17+
{
18+
var expected = dateTime.ToString(Rfc1123Format);
19+
var actual = HeaderUtilities.FormatDate(dateTime);
20+
21+
Assert.Equal(expected, actual);
22+
}
23+
24+
public static TheoryData<DateTimeOffset> TestValues
25+
{
26+
get
27+
{
28+
var data = new TheoryData<DateTimeOffset>();
29+
30+
var now = DateTimeOffset.Now;
31+
32+
for (var i = 0; i < 60; i++)
33+
{
34+
data.Add(now.AddSeconds(i));
35+
data.Add(now.AddMinutes(i));
36+
data.Add(now.AddDays(i));
37+
data.Add(now.AddMonths(i));
38+
data.Add(now.AddYears(i));
39+
}
40+
41+
return data;
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)
This repository has been archived.