summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/shared/System/Buffers/Text/Utf8Parser/Utf8Parser.TimeSpan.C.cs
blob: d0a28969be8d8cc6efe9682cb4e6576359e06ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Buffers.Text
{
    public static partial class Utf8Parser
    {
        private static bool TryParseTimeSpanC(ReadOnlySpan<byte> source, out TimeSpan value, out int bytesConsumed)
        {
            TimeSpanSplitter s = default;
            if (!s.TrySplitTimeSpan(source, periodUsedToSeparateDay: true, out bytesConsumed))
            {
                value = default;
                return false;
            }

            bool isNegative = s.IsNegative;

            bool success;
            switch (s.Separators)
            {
                case 0x00000000: // dd
                    success = TryCreateTimeSpan(isNegative: isNegative, days: s.V1, hours: 0, minutes: 0, seconds: 0, fraction: 0, out value);
                    break;

                case 0x01000000: // hh:mm
                    success = TryCreateTimeSpan(isNegative: isNegative, days: 0, hours: s.V1, minutes: s.V2, seconds: 0, fraction: 0, out value);
                    break;

                case 0x02010000: // dd.hh:mm
                    success = TryCreateTimeSpan(isNegative: isNegative, days: s.V1, hours: s.V2, minutes: s.V3, seconds: 0, fraction: 0, out value);
                    break;

                case 0x01010000: // hh:mm:ss
                    success = TryCreateTimeSpan(isNegative: isNegative, days: 0, hours: s.V1, minutes: s.V2, seconds: s.V3, fraction: 0, out value);
                    break;

                case 0x02010100: // dd.hh:mm:ss
                    success = TryCreateTimeSpan(isNegative: isNegative, days: s.V1, hours: s.V2, minutes: s.V3, seconds: s.V4, fraction: 0, out value);
                    break;

                case 0x01010200: // hh:mm:ss.fffffff
                    success = TryCreateTimeSpan(isNegative: isNegative, days: 0, hours: s.V1, minutes: s.V2, seconds: s.V3, fraction: s.V4, out value);
                    break;

                case 0x02010102: // dd.hh:mm:ss.fffffff
                    success = TryCreateTimeSpan(isNegative: isNegative, days: s.V1, hours: s.V2, minutes: s.V3, seconds: s.V4, fraction: s.V5, out value);
                    break;

                default:
                    value = default;
                    success = false;
                    break;
            }

            if (!success)
            {
                bytesConsumed = 0;
                return false;
            }

            return true;
        }
    }
}