internal static String Format(DynamicDate dDate, string format, DynamicDateFormatInfo ddfi) { if (dDate.IsOutOfTime) { return("OutATime"); } if (format.Length == 1) { format = ExpandPredefinedFormat(format, ref ddfi); } if (format.StartsWith("_") && format.Length < 5) { format = DynamicDateFormatInfo.ExpandDynamicPredefinedFormat(format, ddfi); } return(FormatCustomized(dDate, format, ddfi)); }
private static String FormatCustomized(DynamicDate dDate, string format, DynamicDateFormatInfo ddfi) { StringBuilder result = new StringBuilder(); int i = 0; int tokenLen; while (i < format.Length) { char ch = format[i]; int nextChar; switch (ch) { /*case 'g': * tokenLen = ParseRepeatPattern(format, i, ch); * result.Append(ddfi.GetEraName(cal.GetEra(dateTime))); * break;*/ case 'd': // // tokenLen == 1 : Day of month as digits with no leading zero. // tokenLen == 2 : Day of month as digits with leading zero for single-digit months. // tokenLen == 3 : Day of week as a three-leter abbreviation. // tokenLen >= 4 : Day of week as its full name. // tokenLen = ParseRepeatPattern(format, i, ch); if (tokenLen <= 2) { FormatDigits(result, dDate.DayNumber, tokenLen); } else { if (tokenLen == 3) { result.Append(dDate.WeekDayNameShort); } else { result.Append(dDate.WeekDayNameFull); } } break; case 'D': // // tokenLen == 1 : Day of month as digits with no leading zero. // tokenLen == 2 : Day of month as digits with leading zero for single-digit months. // tokenLen == 3 : Day of month as its short name. // tokenLen >= 4 : Day of week as its full name. // tokenLen = ParseRepeatPattern(format, i, ch); if (tokenLen <= 2) { FormatDigits(result, dDate.DayNumber, tokenLen); } else { if (tokenLen == 3) { result.Append(dDate.MonthDayNameShort); } else { result.Append(dDate.MonthDayNameFull); } } break; case 'M': // // tokenLen == 1 : Month as digits with no leading zero. // tokenLen == 2 : Month as digits with leading zero for single-digit months. // tokenLen == 3 : Month as a three-letter abbreviation. // tokenLen >= 4 : Month as its full name. // tokenLen = ParseRepeatPattern(format, i, ch); if (tokenLen <= 2) { FormatDigits(result, dDate.MonthNumber, tokenLen); } else { if (tokenLen == 3) { result.Append(dDate.MonthNameShort); } else { result.Append(dDate.MonthNameFull); } } break; case 'y': //Info aus der DateTyme-Klasse und die letzte Zeile klingt falsch.. // Notes about OS behavior: // y: Always print (year % 100). No leading zero. // yy: Always print (year % 100) with leading zero. // yyy: Always print (year % 1000) with leading zero. // yyyy/yyyyy/... : Print year value. No leading zero. int year = dDate.YearNumber; tokenLen = ParseRepeatPattern(format, i, ch); int iX = ddfi.OverrideMaxYearDigits > 0 ? Math.Min(tokenLen, ddfi.OverrideMaxYearDigits) : tokenLen; if (iX <= 2) { FormatDigits(result, year % 100, iX); } else if (iX == 3) { FormatDigits(result, year % 1000, iX); } else { String fmtPattern = "D" + iX; result.Append(year.ToString(fmtPattern, CultureInfo.InvariantCulture)); } break; case ':': //result.Append(ddfi.TimeSeparator); tokenLen = 1; break; case '/': result.Append(ddfi.DateSeparator); tokenLen = 1; break; /*case '|': * tokenLen = 1; * nextChar = ParseNextChar(format, i); * if (nextChar == ch) * { * tokenLen = 2; * result.Append(DynamicDate.FormatLBMode ? "\n" : ", "); * } else * result.Append(DynamicDate.FormatLBMode ? '\n' : ' '); * break;*/ case '\'': case '\"': StringBuilder enquotedString = new StringBuilder(); tokenLen = ParseQuoteString(format, i, enquotedString); result.Append(enquotedString); break; case '&': enquotedString = new StringBuilder(); tokenLen = ParseQuoteString(format, i, enquotedString); string c = "???"; switch (enquotedString.ToString()) { case "MayaDate": c = MayaCalc.Gregorian2Maya(dDate.UtcDate, dDate.Model.MayaCorrelation); break; case "MayaHaabKin": c = MayaCalc.Gregorian2MayaHaabKin(dDate.UtcDate, dDate.Model.MayaCorrelation).ToString(); break; case "MayaDateHaab": c = MayaCalc.Gregorian2MayaHaab(dDate.UtcDate, dDate.Model.MayaCorrelation); break; case "MayaDateTzolkin": c = MayaCalc.Gregorian2MayaTzolkin(dDate.UtcDate, dDate.Model.MayaCorrelation); break; } result.Append(c); break; case '%': // Optional format character. // For example, format string "%d" will print day of month // without leading zero. Most of the cases, "%" can be ignored. nextChar = 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(FormatCustomized(dDate, ((char)nextChar).ToString(), ddfi)); tokenLen = 2; } else { // // This means that '%' is at the end of the format string or // "%%" appears in the format string. // throw new FormatException("Format_InvalidString"); } break; case '\\': // Escaped character. Can be used to insert character into the format string. // For exmple, "\d" will insert the character 'd' into the string. // // NOTENOTE : we can remove this format character if we enforce the enforced quote // character rule. // That is, we ask everyone to use single quote or double quote to insert characters, // then we can remove this character. // nextChar = 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("Format_InvalidString"); } break; default: // NOTENOTE : we can remove this rule if we enforce the enforced quote // character rule. // That is, if we ask everyone to use single quote or double quote to insert characters, // then we can remove this default block. result.Append(ch); tokenLen = 1; break; } i += tokenLen; } return(result.ToString()); }
internal static String Format(DynamicDate dDate, string format, IFormatProvider provider) { DynamicDateFormatInfo ddfi = DynamicDateFormatInfo.GetInstance(dDate.ModelID, provider); return(Format(dDate, format, ddfi)); }