| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Globalization; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.Headers |
| | | 8 | | { |
| | | 9 | | internal sealed class TimeSpanHeaderParser : BaseHeaderParser |
| | | 10 | | { |
| | 1 | 11 | | internal static readonly TimeSpanHeaderParser Parser = new TimeSpanHeaderParser(); |
| | | 12 | | |
| | | 13 | | private TimeSpanHeaderParser() |
| | 1 | 14 | | : base(false) |
| | 1 | 15 | | { |
| | 1 | 16 | | } |
| | | 17 | | |
| | | 18 | | public override string ToString(object value) |
| | 12 | 19 | | { |
| | 12 | 20 | | Debug.Assert(value is TimeSpan); |
| | | 21 | | |
| | 12 | 22 | | return ((int)((TimeSpan)value).TotalSeconds).ToString(NumberFormatInfo.InvariantInfo); |
| | 12 | 23 | | } |
| | | 24 | | |
| | | 25 | | protected override int GetParsedValueLength(string value, int startIndex, object? storeValue, |
| | | 26 | | out object? parsedValue) |
| | 16 | 27 | | { |
| | 16 | 28 | | parsedValue = null; |
| | | 29 | | |
| | 16 | 30 | | int numberLength = HttpRuleParser.GetNumberLength(value, startIndex, false); |
| | | 31 | | |
| | 16 | 32 | | if ((numberLength == 0) || (numberLength > HttpRuleParser.MaxInt32Digits)) |
| | 2 | 33 | | { |
| | 2 | 34 | | return 0; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | int result; |
| | 14 | 38 | | if (!HeaderUtilities.TryParseInt32(value, startIndex, numberLength, out result)) |
| | 2 | 39 | | { |
| | 2 | 40 | | return 0; |
| | | 41 | | } |
| | | 42 | | |
| | 12 | 43 | | parsedValue = new TimeSpan(0, 0, result); |
| | 12 | 44 | | return numberLength; |
| | 16 | 45 | | } |
| | | 46 | | } |
| | | 47 | | } |