예제 #1
0
        static bool TryGetMonth(DateToken token, byte[] text, out int month)
        {
            month = 0;

            if (!token.IsMonth || token.Length < 3)
            {
                return(false);
            }

            var name = Encoding.ASCII.GetString(text, token.StartIndex, token.Length);

            if (name.Length > 3)
            {
                name = name.Substring(0, 3);
            }

            for (int i = 0; i < Months.Length; i++)
            {
                if (Months[i].Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    month = i + 1;
                    return(true);
                }
            }

            return(false);
        }
예제 #2
0
        static bool TryGetWeekday(DateToken token, byte[] text, out DayOfWeek weekday)
        {
            weekday = DayOfWeek.Sunday;

            if (!token.IsWeekday || token.Length < 3)
            {
                return(false);
            }

            var name = Encoding.ASCII.GetString(text, token.StartIndex, token.Length);

            if (name.Length > 3)
            {
                name = name.Substring(0, 3);
            }

            for (int day = 0; day < WeekDays.Length; day++)
            {
                if (WeekDays[day].Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    weekday = (DayOfWeek)day;
                    return(true);
                }
            }

            return(false);
        }
예제 #3
0
        static bool TryGetTimeZone(DateToken token, byte[] text, out int tzone)
        {
            tzone = 0;

            if (token.IsNumericZone)
            {
                int endIndex = token.StartIndex + token.Length;
                int index    = token.StartIndex;
                int sign;

                if (text[index] == (byte)'-')
                {
                    sign = -1;
                }
                else if (text[index] == (byte)'+')
                {
                    sign = 1;
                }
                else
                {
                    return(false);
                }

                index++;

                if (!TryParseInt32(text, ref index, endIndex, out tzone) || index != endIndex)
                {
                    return(false);
                }

                tzone *= sign;
            }
            else if (token.IsAlphaZone)
            {
                if (token.Length > 3)
                {
                    return(false);
                }

                var name = Encoding.ASCII.GetString(text, token.StartIndex, token.Length);

                if (!timezones.TryGetValue(name, out tzone))
                {
                    return(false);
                }
            }
            else if (token.IsNumeric)
            {
                int endIndex = token.StartIndex + token.Length;
                int index    = token.StartIndex;

                if (!ParseUtils.TryParseInt32(text, ref index, endIndex, out tzone) || index != endIndex)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #4
0
        static bool TryGetTimeOfDay(DateToken token, byte[] text, out int hour, out int minute, out int second)
        {
            int endIndex = token.StartIndex + token.Length;
            int index    = token.StartIndex;

            hour = minute = second = 0;

            if (!token.IsTimeOfDay)
            {
                return(false);
            }

            if (!TryParseInt32(text, ref index, endIndex, out hour) || hour > 23)
            {
                return(false);
            }

            if (index >= endIndex || text[index++] != (byte)':')
            {
                return(false);
            }

            if (!TryParseInt32(text, ref index, endIndex, out minute) || minute > 59)
            {
                return(false);
            }

            // Allow just hh:mm (i.e. w/o the :ss?)
            if (index >= endIndex || text[index++] != (byte)':')
            {
                return(true);
            }

            if (!TryParseInt32(text, ref index, endIndex, out second) || second > 59)
            {
                return(false);
            }

            return(index == endIndex);
        }
예제 #5
0
        static bool TryGetYear(DateToken token, byte[] text, out int year)
        {
            int endIndex = token.StartIndex + token.Length;
            int index    = token.StartIndex;

            year = 0;

            if (!token.IsNumeric)
            {
                return(false);
            }

            if (!TryParseInt32(text, ref index, endIndex, out year))
            {
                return(false);
            }

            if (year < 100)
            {
                year += (year < 70) ? 2000 : 1900;
            }

            return(year >= 1969);
        }
예제 #6
0
        static bool TryGetDayOfMonth(DateToken token, byte[] text, out int day)
        {
            int endIndex = token.StartIndex + token.Length;
            int index    = token.StartIndex;

            day = 0;

            if (!token.IsNumeric)
            {
                return(false);
            }

            if (!TryParseInt32(text, ref index, endIndex, out day))
            {
                return(false);
            }

            if (day <= 0 || day > 31)
            {
                return(false);
            }

            return(true);
        }