Beispiel #1
0
        internal static string Format(TimeSpan value, string format, IFormatProvider formatProvider)
        {
            if (format == null || format.Length == 0)
            {
                format = "c";
            }
            if (format.Length != 1)
            {
                return(TimeSpanFormat.FormatCustomized(value, format, DateTimeFormatInfo.GetInstance(formatProvider)));
            }
            char ch = format[0];

            switch (ch)
            {
            case 'c':
            case 't':
            case 'T':
                return(TimeSpanFormat.FormatStandard(value, true, format, TimeSpanFormat.Pattern.Minimum));

            case 'g':
            case 'G':
                DateTimeFormatInfo instance = DateTimeFormatInfo.GetInstance(formatProvider);
                format = value._ticks >= 0L ? instance.FullTimeSpanPositivePattern : instance.FullTimeSpanNegativePattern;
                TimeSpanFormat.Pattern pattern = (int)ch != 103 ? TimeSpanFormat.Pattern.Full : TimeSpanFormat.Pattern.Minimum;
                return(TimeSpanFormat.FormatStandard(value, false, format, pattern));

            default:
                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
            }
        }
Beispiel #2
0
        // Token: 0x060030CD RID: 12493 RVA: 0x000BA424 File Offset: 0x000B8624
        internal static string Format(TimeSpan value, string format, IFormatProvider formatProvider)
        {
            if (format == null || format.Length == 0)
            {
                format = "c";
            }
            if (format.Length != 1)
            {
                return(TimeSpanFormat.FormatCustomized(value, format, DateTimeFormatInfo.GetInstance(formatProvider)));
            }
            char c = format[0];

            if (c == 'c' || c == 't' || c == 'T')
            {
                return(TimeSpanFormat.FormatStandard(value, true, format, TimeSpanFormat.Pattern.Minimum));
            }
            if (c == 'g' || c == 'G')
            {
                DateTimeFormatInfo instance = DateTimeFormatInfo.GetInstance(formatProvider);
                if (value._ticks < 0L)
                {
                    format = instance.FullTimeSpanNegativePattern;
                }
                else
                {
                    format = instance.FullTimeSpanPositivePattern;
                }
                TimeSpanFormat.Pattern pattern;
                if (c == 'g')
                {
                    pattern = TimeSpanFormat.Pattern.Minimum;
                }
                else
                {
                    pattern = TimeSpanFormat.Pattern.Full;
                }
                return(TimeSpanFormat.FormatStandard(value, false, format, pattern));
            }
            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
        }
        //
        //  FormatCustomized
        //
        //  Actions: Format the TimeSpan instance using the specified format.
        //
        internal static String FormatCustomized(TimeSpan value, String format, DateTimeFormatInfo dtfi)
        {
            Debug.Assert(dtfi != null, "dtfi == null");

            int  day  = (int)(value._ticks / TimeSpan.TicksPerDay);
            long time = value._ticks % TimeSpan.TicksPerDay;

            if (value._ticks < 0)
            {
                day  = -day;
                time = -time;
            }
            int hours    = (int)(time / TimeSpan.TicksPerHour % 24);
            int minutes  = (int)(time / TimeSpan.TicksPerMinute % 60);
            int seconds  = (int)(time / TimeSpan.TicksPerSecond % 60);
            int fraction = (int)(time % TimeSpan.TicksPerSecond);

            long          tmp = 0;
            int           i   = 0;
            int           tokenLen;
            StringBuilder result = StringBuilderCache.Acquire();

            while (i < format.Length)
            {
                char ch = format[i];
                int  nextChar;
                switch (ch)
                {
                case 'h':
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 2)
                    {
                        throw new FormatException(SR.Format_InvalidString);
                    }
                    DateTimeFormat.FormatDigits(result, hours, tokenLen);
                    break;

                case 'm':
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 2)
                    {
                        throw new FormatException(SR.Format_InvalidString);
                    }
                    DateTimeFormat.FormatDigits(result, minutes, tokenLen);
                    break;

                case 's':
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 2)
                    {
                        throw new FormatException(SR.Format_InvalidString);
                    }
                    DateTimeFormat.FormatDigits(result, seconds, tokenLen);
                    break;

                case 'f':
                    //
                    // The fraction of a second in single-digit precision. The remaining digits are truncated.
                    //
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits)
                    {
                        throw new FormatException(SR.Format_InvalidString);
                    }

                    tmp  = (long)fraction;
                    tmp /= (long)Math.Pow(10, DateTimeFormat.MaxSecondsFractionDigits - tokenLen);
                    result.Append((tmp).ToString(DateTimeFormat.fixedNumberFormats[tokenLen - 1], CultureInfo.InvariantCulture));
                    break;

                case 'F':
                    //
                    // Displays the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero.
                    //
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits)
                    {
                        throw new FormatException(SR.Format_InvalidString);
                    }

                    tmp  = (long)fraction;
                    tmp /= (long)Math.Pow(10, DateTimeFormat.MaxSecondsFractionDigits - tokenLen);
                    int effectiveDigits = tokenLen;
                    while (effectiveDigits > 0)
                    {
                        if (tmp % 10 == 0)
                        {
                            tmp = tmp / 10;
                            effectiveDigits--;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (effectiveDigits > 0)
                    {
                        result.Append((tmp).ToString(DateTimeFormat.fixedNumberFormats[effectiveDigits - 1], CultureInfo.InvariantCulture));
                    }
                    break;

                case 'd':
                    //
                    // tokenLen == 1 : Day as digits with no leading zero.
                    // tokenLen == 2+: Day as digits with leading zero for single-digit days.
                    //
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 8)
                    {
                        throw new FormatException(SR.Format_InvalidString);
                    }
                    DateTimeFormat.FormatDigits(result, day, tokenLen, true);
                    break;

                case '\'':
                case '\"':
                    tokenLen = DateTimeFormat.ParseQuoteString(format, i, result);
                    break;

                case '%':
                    // Optional format character.
                    // For example, format string "%d" will print day
                    // Most of the cases, "%" can be ignored.
                    nextChar = DateTimeFormat.ParseNextChar(format, i);
                    // nextChar will be -1 if we already reach the end of the format string.
                    // Besides, we will not allow "%%" appear in the pattern.
                    if (nextChar >= 0 && nextChar != (int)'%')
                    {
                        result.Append(TimeSpanFormat.FormatCustomized(value, ((char)nextChar).ToString(), dtfi));
                        tokenLen = 2;
                    }
                    else
                    {
                        //
                        // This means that '%' is at the end of the format string or
                        // "%%" appears in the format string.
                        //
                        throw new FormatException(SR.Format_InvalidString);
                    }
                    break;

                case '\\':
                    // Escaped character.  Can be used to insert character into the format string.
                    // For example, "\d" will insert the character 'd' into the string.
                    //
                    nextChar = DateTimeFormat.ParseNextChar(format, i);
                    if (nextChar >= 0)
                    {
                        result.Append(((char)nextChar));
                        tokenLen = 2;
                    }
                    else
                    {
                        //
                        // This means that '\' is at the end of the formatting string.
                        //
                        throw new FormatException(SR.Format_InvalidString);
                    }
                    break;

                default:
                    throw new FormatException(SR.Format_InvalidString);
                }
                i += tokenLen;
            }
            return(StringBuilderCache.GetStringAndRelease(result));
        }
Beispiel #4
0
        // Token: 0x060030CF RID: 12495 RVA: 0x000BA6C8 File Offset: 0x000B88C8
        internal static string FormatCustomized(TimeSpan value, string format, DateTimeFormatInfo dtfi)
        {
            int  num  = (int)(value._ticks / 864000000000L);
            long num2 = value._ticks % 864000000000L;

            if (value._ticks < 0L)
            {
                num  = -num;
                num2 = -num2;
            }
            int           value2        = (int)(num2 / 36000000000L % 24L);
            int           value3        = (int)(num2 / 600000000L % 60L);
            int           value4        = (int)(num2 / 10000000L % 60L);
            int           num3          = (int)(num2 % 10000000L);
            int           i             = 0;
            StringBuilder stringBuilder = StringBuilderCache.Acquire(16);

            while (i < format.Length)
            {
                char c = format[i];
                int  num5;
                if (c <= 'F')
                {
                    if (c <= '%')
                    {
                        if (c != '"')
                        {
                            if (c != '%')
                            {
                                goto IL_34D;
                            }
                            int num4 = DateTimeFormat.ParseNextChar(format, i);
                            if (num4 >= 0 && num4 != 37)
                            {
                                stringBuilder.Append(TimeSpanFormat.FormatCustomized(value, ((char)num4).ToString(), dtfi));
                                num5 = 2;
                                goto IL_35D;
                            }
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                        }
                    }
                    else if (c != '\'')
                    {
                        if (c != 'F')
                        {
                            goto IL_34D;
                        }
                        num5 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                        if (num5 > 7)
                        {
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                        }
                        long num6 = (long)num3;
                        num6 /= (long)Math.Pow(10.0, (double)(7 - num5));
                        int num7 = num5;
                        while (num7 > 0 && num6 % 10L == 0L)
                        {
                            num6 /= 10L;
                            num7--;
                        }
                        if (num7 > 0)
                        {
                            stringBuilder.Append(num6.ToString(DateTimeFormat.fixedNumberFormats[num7 - 1], CultureInfo.InvariantCulture));
                            goto IL_35D;
                        }
                        goto IL_35D;
                    }
                    StringBuilder stringBuilder2 = new StringBuilder();
                    num5 = DateTimeFormat.ParseQuoteString(format, i, stringBuilder2);
                    stringBuilder.Append(stringBuilder2);
                }
                else if (c <= 'h')
                {
                    if (c != '\\')
                    {
                        switch (c)
                        {
                        case 'd':
                            num5 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                            if (num5 > 8)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                            }
                            DateTimeFormat.FormatDigits(stringBuilder, num, num5, true);
                            break;

                        case 'e':
                        case 'g':
                            goto IL_34D;

                        case 'f':
                        {
                            num5 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                            if (num5 > 7)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                            }
                            long num6 = (long)num3;
                            stringBuilder.Append((num6 / (long)Math.Pow(10.0, (double)(7 - num5))).ToString(DateTimeFormat.fixedNumberFormats[num5 - 1], CultureInfo.InvariantCulture));
                            break;
                        }

                        case 'h':
                            num5 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                            if (num5 > 2)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                            }
                            DateTimeFormat.FormatDigits(stringBuilder, value2, num5);
                            break;

                        default:
                            goto IL_34D;
                        }
                    }
                    else
                    {
                        int num4 = DateTimeFormat.ParseNextChar(format, i);
                        if (num4 < 0)
                        {
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                        }
                        stringBuilder.Append((char)num4);
                        num5 = 2;
                    }
                }
                else if (c != 'm')
                {
                    if (c != 's')
                    {
                        goto IL_34D;
                    }
                    num5 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                    if (num5 > 2)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                    }
                    DateTimeFormat.FormatDigits(stringBuilder, value4, num5);
                }
                else
                {
                    num5 = DateTimeFormat.ParseRepeatPattern(format, i, c);
                    if (num5 > 2)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                    }
                    DateTimeFormat.FormatDigits(stringBuilder, value3, num5);
                }
IL_35D:
                i += num5;
                continue;
IL_34D:
                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
            }
            return(StringBuilderCache.GetStringAndRelease(stringBuilder));
        }
Beispiel #5
0
        internal static string FormatCustomized(TimeSpan value, string format, DateTimeFormatInfo dtfi)
        {
            int  num1 = (int)(value._ticks / 864000000000L);
            long num2 = value._ticks % 864000000000L;

            if (value._ticks < 0L)
            {
                num1 = -num1;
                num2 = -num2;
            }
            int           num3          = (int)(num2 / 36000000000L % 24L);
            int           num4          = (int)(num2 / 600000000L % 60L);
            int           num5          = (int)(num2 / 10000000L % 60L);
            int           num6          = (int)(num2 % 10000000L);
            int           pos           = 0;
            StringBuilder stringBuilder = StringBuilderCache.Acquire(16);

            while (pos < format.Length)
            {
                char patternChar = format[pos];
                int  len;
                if ((uint)patternChar <= 70U)
                {
                    if ((uint)patternChar <= 37U)
                    {
                        if ((int)patternChar != 34)
                        {
                            if ((int)patternChar == 37)
                            {
                                int nextChar = DateTimeFormat.ParseNextChar(format, pos);
                                if (nextChar < 0 || nextChar == 37)
                                {
                                    throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                                }
                                stringBuilder.Append(TimeSpanFormat.FormatCustomized(value, ((char)nextChar).ToString(), dtfi));
                                len = 2;
                                goto label_43;
                            }
                            else
                            {
                                goto label_42;
                            }
                        }
                    }
                    else if ((int)patternChar != 39)
                    {
                        if ((int)patternChar == 70)
                        {
                            len = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar);
                            if (len > 7)
                            {
                                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                            }
                            long num7 = (long)num6 / (long)Math.Pow(10.0, (double)(7 - len));
                            int  num8;
                            for (num8 = len; num8 > 0 && num7 % 10L == 0L; --num8)
                            {
                                num7 /= 10L;
                            }
                            if (num8 > 0)
                            {
                                stringBuilder.Append(num7.ToString(DateTimeFormat.fixedNumberFormats[num8 - 1], (IFormatProvider)CultureInfo.InvariantCulture));
                                goto label_43;
                            }
                            else
                            {
                                goto label_43;
                            }
                        }
                        else
                        {
                            goto label_42;
                        }
                    }
                    StringBuilder result = new StringBuilder();
                    len = DateTimeFormat.ParseQuoteString(format, pos, result);
                    stringBuilder.Append((object)result);
                    goto label_43;
                }
                else if ((uint)patternChar <= 104U)
                {
                    switch (patternChar)
                    {
                    case '\\':
                        int nextChar1 = DateTimeFormat.ParseNextChar(format, pos);
                        if (nextChar1 < 0)
                        {
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                        }
                        stringBuilder.Append((char)nextChar1);
                        len = 2;
                        goto label_43;

                    case 'd':
                        len = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar);
                        if (len > 8)
                        {
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                        }
                        DateTimeFormat.FormatDigits(stringBuilder, num1, len, true);
                        goto label_43;

                    case 'f':
                        len = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar);
                        if (len > 7)
                        {
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                        }
                        long num9 = (long)num6 / (long)Math.Pow(10.0, (double)(7 - len));
                        stringBuilder.Append(num9.ToString(DateTimeFormat.fixedNumberFormats[len - 1], (IFormatProvider)CultureInfo.InvariantCulture));
                        goto label_43;

                    case 'h':
                        len = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar);
                        if (len > 2)
                        {
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                        }
                        DateTimeFormat.FormatDigits(stringBuilder, num3, len);
                        goto label_43;
                    }
                }
                else if ((int)patternChar != 109)
                {
                    if ((int)patternChar == 115)
                    {
                        len = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar);
                        if (len > 2)
                        {
                            throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                        }
                        DateTimeFormat.FormatDigits(stringBuilder, num5, len);
                        goto label_43;
                    }
                }
                else
                {
                    len = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar);
                    if (len > 2)
                    {
                        throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                    }
                    DateTimeFormat.FormatDigits(stringBuilder, num4, len);
                    goto label_43;
                }
label_42:
                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
label_43:
                pos += len;
            }
            return(StringBuilderCache.GetStringAndRelease(stringBuilder));
        }