File: System\Net\Http\Headers\TimeSpanHeaderParser.cs
Web Access
Project: src\src\libraries\System.Net.Http\src\System.Net.Http.csproj (System.Net.Http)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Diagnostics;
using System.Globalization;
 
namespace System.Net.Http.Headers
{
    internal sealed class TimeSpanHeaderParser : BaseHeaderParser
    {
        internal static readonly TimeSpanHeaderParser Parser = new TimeSpanHeaderParser();
 
        private TimeSpanHeaderParser()
            : base(false)
        {
        }
 
        public override string ToString(object value)
        {
            Debug.Assert(value is TimeSpan);
 
            return ((int)((TimeSpan)value).TotalSeconds).ToString(NumberFormatInfo.InvariantInfo);
        }
 
        protected override int GetParsedValueLength(string value, int startIndex, object? storeValue,
            out object? parsedValue)
        {
            parsedValue = null;
 
            int numberLength = HttpRuleParser.GetNumberLength(value, startIndex, false);
 
            if ((numberLength == 0) || (numberLength > HttpRuleParser.MaxInt32Digits))
            {
                return 0;
            }
 
            int result;
            if (!HeaderUtilities.TryParseInt32(value, startIndex, numberLength, out result))
            {
                return 0;
            }
 
            parsedValue = new TimeSpan(0, 0, result);
            return numberLength;
        }
    }
}