public string ParsePattern(MonthCalendarDate date, string[] nativeDigits = null) { // replace date separator with '/' string format = this.pattern.Replace(provider.DateSeparator, "/"); StringBuilder sb = new StringBuilder(); Calendar c = provider.Calendar; int i = 0; int index = 0; while (i < format.Length) { int tokLen; char ch = format[i]; string currentString; switch (ch) { case 'd': { tokLen = CountChar(format, i, ch); if (tokLen <= 2) { currentString = DateMethods.GetNumberString(date.Day, nativeDigits, tokLen == 2); this.isDayNumber = true; this.dayString = currentString; this.dayPartIndex = index++; this.dayIndex = sb.Length; } else { currentString = tokLen == 3 ? provider.GetAbbreviatedDayName(c.GetDayOfWeek(date.Date)) : provider.GetDayName(c.GetDayOfWeek(date.Date)); this.dayNameString = currentString; } sb.Append(currentString); break; } case 'M': { tokLen = CountChar(format, i, ch); if (tokLen <= 2) { currentString = DateMethods.GetNumberString(date.Month, nativeDigits, tokLen == 2); this.isMonthNumber = true; } else { currentString = tokLen == 3 ? provider.GetAbbreviatedMonthName(date.Year, date.Month) : provider.GetMonthName(date.Year, date.Month); } this.monthPartIndex = index++; this.monthIndex = sb.Length; this.monthString = currentString; sb.Append(currentString); break; } case 'y': { tokLen = CountChar(format, i, ch); var year = tokLen <= 2 ? date.Year % 100 : date.Year; currentString = DateMethods.GetNumberString(year, nativeDigits, tokLen <= 2); this.yearString = currentString; this.yearPartIndex = index++; this.yearIndex = sb.Length; sb.Append(currentString); break; } case 'g': { tokLen = CountChar(format, i, ch); currentString = provider.GetEraName(c.GetEra(date.Date)); this.eraString = currentString; sb.Append(currentString); break; } case '/': { tokLen = CountChar(format, i, ch); sb.Append(provider.DateSeparator); break; } default: { tokLen = 1; sb.Append(ch.ToString(CultureInfo.CurrentUICulture)); break; } } i += tokLen; } return(sb.ToString()); }
/// <summary> /// Returns a string representation of this object. /// </summary> /// <param name="format">The format pattern.</param> /// <param name="dtfi">The <see cref="DateTimeFormatInfo"/> to use for formatting.</param> /// <param name="nameProvider">The day and month name provider.</param> /// <param name="nativeDigits">If not <c>null</c> and valid, uses for number representation the specified native digits.</param> /// <returns> /// A <see cref="System.String"/> that represents this instance. /// </returns> private string ToString(string format, DateTimeFormatInfo dtfi, ICustomFormatProvider nameProvider, string[] nativeDigits = null) { if (dtfi == null) { dtfi = CultureInfo.CurrentUICulture.DateTimeFormat; } if (string.IsNullOrEmpty(format)) { format = nameProvider != null ? nameProvider.ShortDatePattern : dtfi.ShortDatePattern; } else if (format.Length == 1) { switch (format[0]) { case 'D': { format = nameProvider != null ? nameProvider.LongDatePattern : dtfi.LongDatePattern; break; } case 'm': case 'M': { format = nameProvider != null ? nameProvider.MonthDayPattern : dtfi.MonthDayPattern; break; } case 'y': case 'Y': { format = nameProvider != null ? nameProvider.YearMonthPattern : dtfi.YearMonthPattern; break; } default: { format = nameProvider != null ? nameProvider.ShortDatePattern : dtfi.ShortDatePattern; break; } } } format = format.Replace(nameProvider != null ? nameProvider.DateSeparator : dtfi.DateSeparator, "/"); StringBuilder sb = new StringBuilder(); Calendar c = nameProvider != null ? nameProvider.Calendar : dtfi.Calendar; int i = 0; while (i < format.Length) { int tokLen; char ch = format[i]; switch (ch) { case 'd': { tokLen = CountChar(format, i, ch); sb.Append(tokLen <= 2 ? DateMethods.GetNumberString(this.Day, nativeDigits, tokLen == 2) : GetDayName(c.GetDayOfWeek(this.Date), dtfi, nameProvider, tokLen == 3)); break; } case 'M': { tokLen = CountChar(format, i, ch); sb.Append(tokLen <= 2 ? DateMethods.GetNumberString(this.Month, nativeDigits, tokLen == 2) : GetMonthName(this.Month, this.Year, dtfi, nameProvider, tokLen == 3)); break; } case 'y': { tokLen = CountChar(format, i, ch); sb.Append(tokLen <= 2 ? DateMethods.GetNumberString(this.Year % 100, nativeDigits, true) : DateMethods.GetNumberString(this.Year, nativeDigits, false)); break; } case 'g': { tokLen = CountChar(format, i, ch); sb.Append(nameProvider != null ? nameProvider.GetEraName(c.GetEra(this.Date)) : dtfi.GetEraName(c.GetEra(this.Date))); break; } case '/': { tokLen = CountChar(format, i, ch); sb.Append(nameProvider != null ? nameProvider.DateSeparator : dtfi.DateSeparator); break; } case '\'': { tokLen = 1; break; } default: { tokLen = 1; sb.Append(ch.ToString(CultureInfo.CurrentUICulture)); break; } } i += tokLen; } return(sb.ToString()); }