Example #1
0
        public string ToString(string format, IFormatProvider provider)
        {
            var dtfi = DateTimeFormatInfo.GetInstance(provider);

            return(DateTimeExFormat.FormatCustomized(this, format, dtfi));
        }
Example #2
0
        public static DateTimeEx ParseCustomized(string value, string format, DateTimeFormatInfo dtfi)
        {
            var        lexer = new DateTimeLexer(value);
            DateTimeEx res   = new DateTimeEx();

            int  num2;
            bool isbc = false;

            for (int i = 0; i < format.Length; i += num2)
            {
                char patternChar = format[i];
                switch (patternChar)
                {
                case 'F':
                case 'f':
                {
                    num2 = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    if (num2 > 7)
                    {
                        throw new FormatException("DAE-00221 Bad format");
                    }
                    int parsed = lexer.ReadNumber(num2);
                    res.Nanosecond = (int)Math.Pow(10.0, (double)(7 - num2)) * 100 * parsed;
                    break;
                }

                case 'H':
                {
                    num2     = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    res.Hour = lexer.ReadNumber(num2);
                    break;
                }

                case ':':
                {
                    lexer.Skip(dtfi.TimeSeparator);
                    num2 = 1;
                    break;
                }

                case '/':
                {
                    lexer.Skip(dtfi.DateSeparator);
                    num2 = 1;
                    break;
                }

                case '\'':
                case '"':
                {
                    StringBuilder result = new StringBuilder();
                    num2 = DateTimeExFormat.ParseQuoteString(format, i, result);
                    lexer.Skip(result.ToString());
                    break;
                }

                case 'M':
                {
                    num2      = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    res.Month = lexer.ReadNumber(num2);
                    break;
                }

                case '\\':
                {
                    int ch = DateTimeExFormat.ParseNextChar(format, i);
                    if (ch < 0)
                    {
                        throw new FormatException("DAE-00222 Invalid format");
                    }
                    lexer.Skip((char)ch);
                    num2 = 2;
                    break;
                }

                case 'd':
                {
                    num2    = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    res.Day = lexer.ReadNumber(num2);
                    break;
                }

                case 'g':
                {
                    num2 = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    if (lexer.Skip("A.D"))
                    {
                    }
                    else if (lexer.Skip("B.C"))
                    {
                        isbc = true;
                    }
                    break;
                }

                case 'h':
                {
                    num2     = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    res.Hour = lexer.ReadNumber(num2);
                    break;
                }

                case 's':
                {
                    num2       = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    res.Second = lexer.ReadNumber(num2);
                    break;
                }

                case 'm':
                {
                    num2       = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    res.Minute = lexer.ReadNumber(num2);
                    break;
                }

                case 'y':
                {
                    num2 = DateTimeExFormat.ParseRepeatPattern(format, i, patternChar);
                    if (num2 == 2)
                    {
                        int num3 = lexer.ReadNumber(2);
                        if (num3 < 69)
                        {
                            res.Year = 2000 + num3;
                        }
                        else
                        {
                            res.Year = 1900 + num3;
                        }
                    }
                    if (num2 == 3)
                    {
                        int num3 = lexer.ReadNumber(2);
                        if (num3 < 500)
                        {
                            res.Year = 2000 + num3;
                        }
                        else
                        {
                            res.Year = 1000 + num3;
                        }
                    }
                    if (num2 >= 4)
                    {
                        res.Year = lexer.ReadNumber(num2);
                    }
                    break;
                }

                default:
                {
                    lexer.Skip(patternChar);
                    num2 = 1;
                    break;
                }
                }
            }
            if (isbc)
            {
                res.Year = -res.Year;
            }
            return(res);
        }