public JsonWriterDelegator(XmlWriter writer, DateTimeFormat dateTimeFormat) : this(writer) { _dateTimeFormat = dateTimeFormat; }
/// <summary>Format the TimeSpan instance using the specified format.</summary> private static StringBuilder FormatCustomized(TimeSpan value, ReadOnlySpan <char> format, DateTimeFormatInfo dtfi, StringBuilder result) { 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.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) { 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.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) { 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; ReadOnlySpan <char> nextCharSpan; #if MONO // Remove once Mono switches to Fast Spans unsafe { nextCharSpan = new ReadOnlySpan <char>(&nextCharChar, 1); } #else nextCharSpan = MemoryMarshal.CreateReadOnlySpan <char>(ref nextCharChar, 1); #endif StringBuilder origStringBuilder = FormatCustomized(value, nextCharSpan, 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); }
public static String FormatDateTime(DateTime value, String format, IFormatProvider provider, TimeSpan offSet) { return(DateTimeFormat.Format(value, format, DateTimeFormatInfo.GetInstance(provider), offSet)); }
// // FormatCustomized // // Actions: Format the TimeSpan instance using the specified format. // internal static String FormatCustomized(TimeSpan value, String format, DateTimeFormatInfo dtfi) { 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(InternalGloablizationHelper.StringBuilderDefaultCapacity); 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.Append(format, i + 1, tokenLen - 2); 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)); }
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)); }