|
| 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 System.Runtime.CompilerServices; |
| 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 string[] MonthNames = FormatInfo.AbbreviatedMonthNames; |
| 15 | + private static readonly string[] DayNames = FormatInfo.AbbreviatedDayNames; |
| 16 | + |
| 17 | + private static readonly int Rfc1123DateLength = "ddd, dd MMM yyyy HH:mm:ss GMT".Length; |
| 18 | + |
| 19 | + // ASCII numbers are in the range 48 - 57. |
| 20 | + private const int AsciiNumberOffset = 0x30; |
| 21 | + |
| 22 | + private const string Gmt = "GMT"; |
| 23 | + private const char Comma = ','; |
| 24 | + private const char Space = ' '; |
| 25 | + private const char Colon = ':'; |
| 26 | + |
| 27 | + public static unsafe string ToRfc1123String(this DateTimeOffset dateTime) |
| 28 | + { |
| 29 | + var offset = 0; |
| 30 | + var universal = dateTime.UtcDateTime; |
| 31 | + char* target = stackalloc char[Rfc1123DateLength]; |
| 32 | + |
| 33 | + FormatDayOfWeek(universal.DayOfWeek, target, ref offset); |
| 34 | + Format(Comma, target, ref offset); |
| 35 | + Format(Space, target, ref offset); |
| 36 | + FormatNumber(universal.Day, target, ref offset); |
| 37 | + Format(Space, target, ref offset); |
| 38 | + FormatMonth(universal.Month, target, ref offset); |
| 39 | + Format(Space, target, ref offset); |
| 40 | + FormatYear(universal.Year, target, ref offset); |
| 41 | + Format(Space, target, ref offset); |
| 42 | + FormatTimeOfDay(universal.TimeOfDay, target, ref offset); |
| 43 | + Format(Space, target, ref offset); |
| 44 | + Format(Gmt, target, ref offset); |
| 45 | + |
| 46 | + return new string(target, 0, offset); |
| 47 | + } |
| 48 | + |
| 49 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 50 | + private static unsafe void FormatDayOfWeek(DayOfWeek dayOfWeek, char* target, ref int offset) |
| 51 | + { |
| 52 | + Format(DayNames[(int)dayOfWeek], target, ref offset); |
| 53 | + } |
| 54 | + |
| 55 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 56 | + private static unsafe void FormatMonth(int month, char* target, ref int offset) |
| 57 | + { |
| 58 | + Format(MonthNames[month - 1], target, ref offset); |
| 59 | + } |
| 60 | + |
| 61 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 62 | + private static unsafe void FormatYear(int year, char* target, ref int offset) |
| 63 | + { |
| 64 | + Format(GetAsciiChar(year / 1000), target, ref offset); |
| 65 | + Format(GetAsciiChar(year % 1000 / 100), target, ref offset); |
| 66 | + Format(GetAsciiChar(year % 100 / 10), target, ref offset); |
| 67 | + Format(GetAsciiChar(year % 10), target, ref offset); |
| 68 | + } |
| 69 | + |
| 70 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 71 | + private static unsafe void FormatTimeOfDay(TimeSpan timeOfDay, char* target, ref int offset) |
| 72 | + { |
| 73 | + FormatNumber(timeOfDay.Hours, target, ref offset); |
| 74 | + Format(Colon, target, ref offset); |
| 75 | + FormatNumber(timeOfDay.Minutes, target, ref offset); |
| 76 | + Format(Colon, target, ref offset); |
| 77 | + FormatNumber(timeOfDay.Seconds, target, ref offset); |
| 78 | + } |
| 79 | + |
| 80 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 81 | + private static unsafe void FormatNumber(int number, char* target, ref int offset) |
| 82 | + { |
| 83 | + Format(GetAsciiChar(number / 10), target, ref offset); |
| 84 | + Format(GetAsciiChar(number % 10), target, ref offset); |
| 85 | + } |
| 86 | + |
| 87 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 88 | + private static unsafe void Format(string source, char* target, ref int offset) |
| 89 | + { |
| 90 | + for (var i = 0; i < source.Length; i++) |
| 91 | + { |
| 92 | + target[offset++] = source[i]; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 97 | + private static unsafe void Format(char value, char* target, ref int offset) |
| 98 | + { |
| 99 | + target[offset++] = value; |
| 100 | + } |
| 101 | + |
| 102 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 103 | + private static char GetAsciiChar(int value) |
| 104 | + { |
| 105 | + return (char)(AsciiNumberOffset + value); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments