// // 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)); }
// 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)); }
/// <summary>Format the TimeSpan instance using the specified format.</summary> private static StringBuilder FormatCustomized(TimeSpan value, ReadOnlySpan <char> format, DateTimeFormatInfo dtfi, StringBuilder result = null) { Debug.Assert(dtfi != null); bool resultBuilderIsPooled = false; if (result == null) { result = StringBuilderCache.Acquire(InternalGlobalizationHelper.StringBuilderDefaultCapacity); resultBuilderIsPooled = true; } 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; while (i < format.Length) { char ch = format[i]; int nextChar; switch (ch) { case 'h': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); if (tokenLen > 2) { goto default; // to release the builder and throw } DateTimeFormat.FormatDigits(result, hours, tokenLen); break; case 'm': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); if (tokenLen > 2) { goto default; // to release the builder and throw } DateTimeFormat.FormatDigits(result, minutes, tokenLen); break; case 's': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); if (tokenLen > 2) { goto default; // to release the builder and throw } 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) { goto default; // to release the builder and throw } tmp = fraction; tmp /= TimeSpanParse.Pow10(DateTimeFormat.MaxSecondsFractionDigits - tokenLen); result.AppendSpanFormattable(tmp, 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) { goto default; // to release the builder and throw } tmp = fraction; tmp /= TimeSpanParse.Pow10(DateTimeFormat.MaxSecondsFractionDigits - tokenLen); int effectiveDigits = tokenLen; while (effectiveDigits > 0) { if (tmp % 10 == 0) { tmp = tmp / 10; effectiveDigits--; } else { break; } } if (effectiveDigits > 0) { result.AppendSpanFormattable(tmp, 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) { goto default; // to release the builder and throw } 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)'%') { char nextCharChar = (char)nextChar; StringBuilder origStringBuilder = FormatCustomized(value, MemoryMarshal.CreateReadOnlySpan <char>(ref nextCharChar, 1), dtfi, result); Debug.Assert(ReferenceEquals(origStringBuilder, result)); tokenLen = 2; } else { // // This means that '%' is at the end of the format string or // "%%" appears in the format string. // goto default; // to release the builder and throw } 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. // goto default; // to release the builder and throw } break; default: // Invalid format string if (resultBuilderIsPooled) { StringBuilderCache.Release(result); } throw new FormatException(SR.Format_InvalidString); } i += tokenLen; } return(result); }
internal static string FormatCustomized(TimeSpan value, string format, DateTimeFormatInfo dtfi) { int num = (int)(value._ticks / 0xc92a69c000L); long num2 = value._ticks % 0xc92a69c000L; if (value._ticks < 0L) { num = -num; num2 = -num2; } int num3 = (int)((num2 / 0x861c46800L) % 0x18L); int num4 = (int)((num2 / 0x23c34600L) % 60L); int num5 = (int)((num2 / 0x989680L) % 60L); int num6 = (int)(num2 % 0x989680L); long num7 = 0L; int pos = 0; StringBuilder outputBuffer = new StringBuilder(); while (pos < format.Length) { int num9; int num10; char patternChar = format[pos]; switch (patternChar) { case '%': { num10 = DateTimeFormat.ParseNextChar(format, pos); if ((num10 < 0) || (num10 == 0x25)) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } char ch3 = (char)num10; outputBuffer.Append(FormatCustomized(value, ch3.ToString(), dtfi)); num9 = 2; goto Label_035D; } case '\'': case '"': { StringBuilder result = new StringBuilder(); num9 = DateTimeFormat.ParseQuoteString(format, pos, result); outputBuffer.Append(result); goto Label_035D; } case 'F': { num9 = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar); if (num9 > 7) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } num7 = num6; num7 /= (long)Math.Pow(10.0, (double)(7 - num9)); int num11 = num9; while (num11 > 0) { if ((num7 % 10L) != 0L) { break; } num7 /= 10L; num11--; } if (num11 > 0) { outputBuffer.Append(num7.ToString(DateTimeFormat.fixedNumberFormats[num11 - 1], CultureInfo.InvariantCulture)); } goto Label_035D; } case 'm': num9 = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar); if (num9 > 2) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } DateTimeFormat.FormatDigits(outputBuffer, num4, num9); goto Label_035D; case 's': num9 = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar); if (num9 > 2) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } DateTimeFormat.FormatDigits(outputBuffer, num5, num9); goto Label_035D; case 'd': num9 = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar); if (num9 > 8) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } goto Label_02A7; case 'f': num9 = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar); if (num9 > 7) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } goto Label_01B8; case 'h': num9 = DateTimeFormat.ParseRepeatPattern(format, pos, patternChar); if (num9 > 2) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } break; case '\\': num10 = DateTimeFormat.ParseNextChar(format, pos); if (num10 < 0) { throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } outputBuffer.Append((char)num10); num9 = 2; goto Label_035D; default: throw new FormatException(Environment.GetResourceString("Format_InvalidString")); } DateTimeFormat.FormatDigits(outputBuffer, num3, num9); goto Label_035D; Label_01B8: num7 = num6; outputBuffer.Append((num7 / ((long)Math.Pow(10.0, (double)(7 - num9)))).ToString(DateTimeFormat.fixedNumberFormats[num9 - 1], CultureInfo.InvariantCulture)); goto Label_035D; Label_02A7: DateTimeFormat.FormatDigits(outputBuffer, num, num9, true); Label_035D: pos += num9; } return(outputBuffer.ToString()); }
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)); }