HandCoded.Finance.Date.Parse C# (CSharp) Method

Parse() public static method

Parses a DateTime instance from a character string in the ISO date format (as produced by ToString).
If the character string is not in the /// correct format.
public static Parse ( String text ) : Date
text String The text string to be parsed.
return Date
        public static Date Parse(String text)
        {
            int			limit = text.Length;
            int			index = 0;

            while (true) {
                // Extract date components
                if ((index >= limit) || !IsDigit (text [index])) break;
                int year = (text [index++] - '0') * 1000;
                if ((index >= limit) || !IsDigit (text [index])) break;
                year += (text [index++] - '0') * 100;
                if ((index >= limit) || !IsDigit (text [index])) break;
                year += (text [index++] - '0') *10;
                if ((index >= limit) || !IsDigit (text [index])) break;
                year += (text [index++] - '0');

                if ((index >= limit) || (text [index++] != '-')) break;

                if ((index >= limit) || !IsDigit (text [index])) break;
                int month = (text [index++] - '0') * 10;
                if ((index >= limit) || !IsDigit (text [index])) break;
                month += (text [index++] - '0');

                if ((index >= limit) || (text [index++] != '-')) break;

                if ((index >= limit) || !IsDigit (text [index])) break;
                int day = (text [index++] - '0') * 10;
                if ((index >= limit) || !IsDigit (text [index])) break;
                day += (text [index++] - '0');

                // Detect zulu time zone
                if ((index < limit)&& (text [index] == 'Z')) {
                    return (new Date (day, month, year, true));
                }

                // Detect time offsets
                if ((index < limit)&& (text [index] == '+')) {
                    ++index;
                    if ((index >= limit) && !IsDigit (text [index])) break;
                    int offset = (text [index++] - '0') * 600;
                    if ((index >= limit) && !IsDigit (text [index])) break;
                    offset += (text [index++] - '0') * 60;

                    if ((index >= limit) && (text [index++] != ':')) break;

                    if ((index >= limit) && !IsDigit (text [index])) break;
                    offset = (text [index++] - '0') * 10;
                    if ((index >= limit) && !IsDigit (text [index])) break;
                    offset += (text [index++] - '0');

                    return (new Date (day, month, year, offset));
                }

                if ((index < limit)&& (text [index] == '-')) {
                    ++index;
                    if ((index >= limit) && !IsDigit (text [index])) break;
                    int offset = (text [index++] - '0') * 600;
                    if ((index >= limit) && !IsDigit (text [index])) break;
                    offset += (text [index++] - '0') * 60;

                    if ((index >= limit) && (text [index++] != ':')) break;

                    if ((index >= limit) && !IsDigit (text [index])) break;
                    offset = (text [index++] - '0') * 10;
                    if ((index >= limit) && !IsDigit (text [index])) break;
                    offset += (text [index++] - '0');

                    return (new Date (day, month, year, -offset));
                }

                return (new Date (day, month, year, false));
            }

            throw new ArgumentException ("Value is not in ISO date format");
        }