File size: 5,167 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Globalization;

namespace VersOne.Epub.Internal
{
    internal static class SmilClockParser
    {
        public const Int64 MAX_TIMECOUNT = (Int64)Int32.MaxValue * MILLISECONDS_IN_HOUR;
        private const int MILLISECONDS_IN_SECOND = 1000;
        private const int MILLISECONDS_IN_MINUTE = 60 * MILLISECONDS_IN_SECOND;
        private const int MILLISECONDS_IN_HOUR = 60 * MILLISECONDS_IN_MINUTE;

        public static bool TryParse(string smilClock, out EpubNarrationTimestamp result)
        {
            if (string.IsNullOrWhiteSpace(smilClock))
            {
                result = default;
                return false;
            }
            smilClock = smilClock.Trim();
            if (smilClock.Contains(":"))
            {
                return TryParseClockValue(smilClock, out result);
            }
            else
            {
                return TryParseTimecountValue(smilClock, out result);
            }
        }

        private static bool TryParseClockValue(string clockValue, out EpubNarrationTimestamp result)
        {
            string[] parts = clockValue.Split(':');
            if (parts.Length > 3)
            {
                result = default;
                return false;
            }
            int hour;
            int currentPartIndex;
            if (parts.Length == 3)
            {
                if (!Int32.TryParse(parts[0], out hour) || hour < 0)
                {
                    result = default;
                    return false;
                }
                currentPartIndex = 1;
            }
            else
            {
                hour = 0;
                currentPartIndex = 0;
            }
            if (!Int32.TryParse(parts[currentPartIndex], out int minute) || minute < 0 || minute >= 60)
            {
                result = default;
                return false;
            }
            currentPartIndex++;
            if (!Decimal.TryParse(parts[currentPartIndex], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out decimal secondWithFraction) ||
                secondWithFraction < 0 || secondWithFraction >= 60)
            {
                result = default;
                return false;
            }
            int second = (int)Math.Truncate(secondWithFraction);
            int millisecond = (int)Math.Truncate((secondWithFraction - second) * 1000);
            result = new EpubNarrationTimestamp(hour, minute, second, millisecond);
            return true;
        }

        private static bool TryParseTimecountValue(string timecountValue, out EpubNarrationTimestamp result)
        {
            bool hasMetric = false;
            int metricStartIndex = -1;
            for (int i = 0; i < timecountValue.Length; i++)
            {
                char c = timecountValue[i];
                if (!Char.IsDigit(c) && c != '.')
                {
                    hasMetric = true;
                    metricStartIndex = i;
                    break;
                }
            }
            string timecountWithFractionString = hasMetric ? timecountValue.Substring(0, metricStartIndex) : timecountValue;
            if (!Decimal.TryParse(timecountWithFractionString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out decimal timecountWithFraction) ||
                timecountWithFraction > MAX_TIMECOUNT)
            {
                result = default;
                return false;
            }
            int metricMultiplier;
            if (hasMetric)
            {
                string metric = timecountValue.Substring(metricStartIndex);
                switch (metric)
                {
                    case "h":
                        metricMultiplier = MILLISECONDS_IN_HOUR;
                        break;
                    case "min":
                        metricMultiplier = MILLISECONDS_IN_MINUTE;
                        break;
                    case "s":
                        metricMultiplier = MILLISECONDS_IN_SECOND;
                        break;
                    case "ms":
                        metricMultiplier = 1;
                        break;
                    default:
                        result = default;
                        return false;
                }
            }
            else
            {
                metricMultiplier = MILLISECONDS_IN_SECOND;
            }
            decimal milliseconds = Math.Truncate(timecountWithFraction * metricMultiplier);
            result = ConvertMillisecondsToEpubNarrationTimestamp(milliseconds);
            return true;
        }

        private static EpubNarrationTimestamp ConvertMillisecondsToEpubNarrationTimestamp(decimal milliseconds)
        {
            int millisecond = (int)(milliseconds % MILLISECONDS_IN_SECOND);
            int second = (int)(Math.Truncate(milliseconds / MILLISECONDS_IN_SECOND) % 60);
            int minute = (int)(Math.Truncate(milliseconds / MILLISECONDS_IN_MINUTE) % 60);
            int hour = (int)Math.Truncate(milliseconds / MILLISECONDS_IN_HOUR);
            return new(hour, minute, second, millisecond);
        }
    }
}