Beispiel #1
0
 // HMSF (no days)
 internal Boolean FullHMSFMatch(TimeSpanFormat.FormatLiterals pattern) {
     return SepCount                  == 5
         && NumCount                  == 4
         && pattern.Start             == literals[0]
         && pattern.HourMinuteSep     == literals[1]
         && pattern.MinuteSecondSep   == literals[2]
         && pattern.SecondFractionSep == literals[3]
         && pattern.End               == literals[4];
 }
Beispiel #2
0
 // D (no hours, minutes, seconds, or fractions)
 internal Boolean FullDMatch(TimeSpanFormat.FormatLiterals pattern) {
     return SepCount                  == 2
         && NumCount                  == 1
         && pattern.Start             == literals[0]
         && pattern.End               == literals[1];
 }
Beispiel #3
0
            // DHM (no seconds or fraction)
            internal Boolean FullDHMMatch(TimeSpanFormat.FormatLiterals pattern) {
                return SepCount                  == 4
                    && NumCount                  == 3
                    && pattern.Start             == literals[0]
                    && pattern.DayHourSep        == literals[1]
                    && pattern.HourMinuteSep     == literals[2]
                    && pattern.End               == literals[3];

            }
Beispiel #4
0
 internal Boolean PartialAppCompatMatch(TimeSpanFormat.FormatLiterals pattern) {
     return SepCount                  == 4
         && NumCount                  == 3
         && pattern.Start             == literals[0]
         && pattern.HourMinuteSep     == literals[1]
         && pattern.AppCompatLiteral  == literals[2]
         && pattern.End               == literals[3];
 }
Beispiel #5
0
 // DHMSF (all values matched)
 internal Boolean FullMatch(TimeSpanFormat.FormatLiterals pattern) {
     return SepCount                  == MaxLiteralTokens
         && NumCount                  == MaxNumericTokens
         && pattern.Start             == literals[0]
         && pattern.DayHourSep        == literals[1]
         && pattern.HourMinuteSep     == literals[2]
         && pattern.MinuteSecondSep   == literals[3]
         && pattern.SecondFractionSep == literals[4]
         && pattern.End               == literals[5];
 }
 internal bool FullHMSFMatch(TimeSpanFormat.FormatLiterals pattern)
 {
     return (((((this.SepCount == 5) && (this.NumCount == 4)) && ((pattern.Start == this.literals[0]) && (pattern.HourMinuteSep == this.literals[1]))) && ((pattern.MinuteSecondSep == this.literals[2]) && (pattern.SecondFractionSep == this.literals[3]))) && (pattern.End == this.literals[4]));
 }
Beispiel #7
0
        //
        //  FormatCustomized
        //
        //  Actions: Format the TimeSpan instance using the specified format.
        //
        internal static String FormatCustomized(TimeSpan value, String format, DateTimeFormatInfo dtfi)
        {
            Contract.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(Environment.GetResourceString("Format_InvalidString"));
                    }
                    DateTimeFormat.FormatDigits(result, hours, tokenLen);
                    break;

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

                case 's':
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 2)
                    {
                        throw new FormatException(Environment.GetResourceString("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(Environment.GetResourceString("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(Environment.GetResourceString("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(Environment.GetResourceString("Format_InvalidString"));
                    }
                    DateTimeFormat.FormatDigits(result, day, tokenLen, true);
                    break;

                case '\'':
                case '\"':
                    StringBuilder enquotedString = new StringBuilder();
                    tokenLen = DateTimeFormat.ParseQuoteString(format, i, enquotedString);
                    result.Append(enquotedString);
                    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(Environment.GetResourceString("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(Environment.GetResourceString("Format_InvalidString"));
                    }
                    break;

                default:
                    throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
                }
                i += tokenLen;
            }
            return(StringBuilderCache.GetStringAndRelease(result));
        }
 internal bool FullDHMMatch(TimeSpanFormat.FormatLiterals pattern)
 {
     return (((((this.SepCount == 4) && (this.NumCount == 3)) && ((pattern.Start == this.literals[0]) && (pattern.DayHourSep == this.literals[1]))) && (pattern.HourMinuteSep == this.literals[2])) && (pattern.End == this.literals[3]));
 }
 internal bool FullDMatch(TimeSpanFormat.FormatLiterals pattern)
 {
     return ((((this.SepCount == 2) && (this.NumCount == 1)) && (pattern.Start == this.literals[0])) && (pattern.End == this.literals[1]));
 }
 internal bool PartialAppCompatMatch(TimeSpanFormat.FormatLiterals pattern)
 {
     return (((((this.SepCount == 4) && (this.NumCount == 3)) && ((pattern.Start == this.literals[0]) && (pattern.HourMinuteSep == this.literals[1]))) && (pattern.AppCompatLiteral == this.literals[2])) && (pattern.End == this.literals[3]));
 }
Beispiel #11
0
        private static string FormatStandard(TimeSpan value, bool isInvariant, string format, TimeSpanFormat.Pattern pattern)
        {
            StringBuilder sb   = StringBuilderCache.Acquire(16);
            int           num1 = (int)(value._ticks / 864000000000L);
            long          num2 = value._ticks % 864000000000L;

            if (value._ticks < 0L)
            {
                num1 = -num1;
                num2 = -num2;
            }
            int n1 = (int)(num2 / 36000000000L % 24L);
            int n2 = (int)(num2 / 600000000L % 60L);
            int n3 = (int)(num2 / 10000000L % 60L);
            int n4 = (int)(num2 % 10000000L);

            TimeSpanFormat.FormatLiterals formatLiterals;
            if (isInvariant)
            {
                formatLiterals = value._ticks >= 0L ? TimeSpanFormat.PositiveInvariantFormatLiterals : TimeSpanFormat.NegativeInvariantFormatLiterals;
            }
            else
            {
                formatLiterals = new TimeSpanFormat.FormatLiterals();
                formatLiterals.Init(format, pattern == TimeSpanFormat.Pattern.Full);
            }
            if (n4 != 0)
            {
                n4 = (int)((long)n4 / (long)Math.Pow(10.0, (double)(7 - formatLiterals.ff)));
            }
            sb.Append(formatLiterals.Start);
            if (pattern == TimeSpanFormat.Pattern.Full || num1 != 0)
            {
                sb.Append(num1);
                sb.Append(formatLiterals.DayHourSep);
            }
            sb.Append(TimeSpanFormat.IntToString(n1, formatLiterals.hh));
            sb.Append(formatLiterals.HourMinuteSep);
            sb.Append(TimeSpanFormat.IntToString(n2, formatLiterals.mm));
            sb.Append(formatLiterals.MinuteSecondSep);
            sb.Append(TimeSpanFormat.IntToString(n3, formatLiterals.ss));
            if (!isInvariant && pattern == TimeSpanFormat.Pattern.Minimum)
            {
                int num3;
                for (num3 = formatLiterals.ff; num3 > 0 && n4 % 10 == 0; --num3)
                {
                    n4 /= 10;
                }
                if (num3 > 0)
                {
                    sb.Append(formatLiterals.SecondFractionSep);
                    sb.Append(n4.ToString(DateTimeFormat.fixedNumberFormats[num3 - 1], (IFormatProvider)CultureInfo.InvariantCulture));
                }
            }
            else if (pattern == TimeSpanFormat.Pattern.Full || n4 != 0)
            {
                sb.Append(formatLiterals.SecondFractionSep);
                sb.Append(TimeSpanFormat.IntToString(n4, formatLiterals.ff));
            }
            sb.Append(formatLiterals.End);
            return(StringBuilderCache.GetStringAndRelease(sb));
        }
Beispiel #12
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));
        }