|
| 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 | +} |
0 commit comments