public static string GetStaticDisplayText(SpecifierInfo specifier, DateTimeFormatInfo dateTimeFormat, Calendar calendar, DateTime?value) { if (specifier.ValueType != ValueType.Static) { throw new ArgumentException("specifier"); } switch (specifier.Type) { case SpecifierType.g: if (value.HasValue) { int era = calendar.GetEra(value.Value); return(dateTimeFormat.GetEraName(era)); } return(string.Empty); case SpecifierType.DateSeparator: return(dateTimeFormat.DateSeparator); case SpecifierType.TimeSeparator: return(dateTimeFormat.TimeSeparator); default: throw new ArgumentOutOfRangeException("specifier.Type"); } }
public bool IncreaseComponentValue(SpecifierInfo specifier) { switch (specifier.Type) { case SpecifierType.s: case SpecifierType.ss: if (this.second.HasValue && this.second.Value < 59) { this.second++; } else { this.second = 1; } return(true); case SpecifierType.m: case SpecifierType.mm: if (this.minute.HasValue && this.minute.Value < 59) { this.minute++; } else { this.minute = 1; } return(true); case SpecifierType.h: case SpecifierType.hh: case SpecifierType.H: case SpecifierType.HH: if (this.hour.HasValue && this.hour.Value < 23) { this.hour++; } else { this.hour = 0; } this.am = hour < 12; return(true); case SpecifierType.d: case SpecifierType.dd: if (this.day.HasValue) { if (this.year.HasValue && this.month.HasValue) { int daysInMonth = this.Calendar.GetDaysInMonth(this.year.Value, this.month.Value); if (this.day.Value < daysInMonth) { this.day++; return(true); } } } this.day = 1; return(true); case SpecifierType.M: case SpecifierType.MM: case SpecifierType.MMM: case SpecifierType.MMMM: if (this.month.HasValue) { if (this.year.HasValue) { int monthsInYear = this.Calendar.GetMonthsInYear(this.year.Value); if (this.month.Value < monthsInYear) { this.month++; return(true); } } else { if (this.month.Value < 12) /**********/ { this.month++; return(true); } } } this.month = 1; return(true); case SpecifierType.y: case SpecifierType.yy: case SpecifierType.yyy: case SpecifierType.yyyy: if (this.year.HasValue) { int maxYear = this.Calendar.GetYear(this.Calendar.MaxSupportedDateTime); if (this.year.Value < maxYear) { this.year++; return(true); } else { return(false); } } this.year = this.Calendar.GetYear(DateTime.Now); return(true); case SpecifierType.t: case SpecifierType.tt: if (this.hour.HasValue) { if (this.hour < 12) { this.hour += 12; this.am = false; } else { this.hour -= 12; this.am = true; } return(true); } return(false); } return(false); }
public static SpecifierInfo[] GetSpecifiers(string format) { if (format == null) { throw new ArgumentNullException("format"); } StringBuilder stringLiteral = new StringBuilder(); var matchedSpecifiers = new List <SpecifierInfo>(); int length = format.Length; for (int index = 0; index < length;) { char symbol = format[index]; if (symbol.Equals(escapeCharacter)) { if (index < length - 1) { index++; // Move to the next character stringLiteral.Append(format[index]); index++; } continue; } var specsOfSymbol = GetSpecsOfSymbol(symbol); int lastMatchedRepeatCount = 1; SpecifierInfo matchedSpecifier = new SpecifierInfo(); bool specifierFound = false; int repeatCount = 0; if (specsOfSymbol != null) { do { repeatCount++; foreach (var specifier in specsOfSymbol) { int specSymbolCount = specifier.Symbol.Length; if (specSymbolCount == repeatCount || (specSymbolCount < repeatCount && specifier.MatchesExtraSymbols)) { matchedSpecifier = specifier; lastMatchedRepeatCount = repeatCount; specifierFound = true; } } } while ((index + repeatCount) < length && format[index + repeatCount].Equals(symbol)); } else { repeatCount = 1; } if (specifierFound) { if (stringLiteral.Length > 0) { var slSpecifier = SpecifierInfo.CreateStringLiteralSpecifier(stringLiteral.ToString()); matchedSpecifiers.Add(slSpecifier); stringLiteral = new StringBuilder(); } matchedSpecifiers.Add(matchedSpecifier); } else { stringLiteral.Append(new string(symbol, repeatCount)); } index += lastMatchedRepeatCount; } if (stringLiteral.Length > 0) { var slSpecifier = SpecifierInfo.CreateStringLiteralSpecifier(stringLiteral.ToString()); matchedSpecifiers.Add(slSpecifier); } return(matchedSpecifiers.ToArray()); }
public string GetDisplayText(SpecifierInfo specifier) { if (specifier.ValueType == ValueType.StringLiteral) { return(specifier.Symbol); } string displayText = null; switch (specifier.Type) { case SpecifierType.s: if (this.second.HasValue) { displayText = this.second.Value.ToString(); } break; case SpecifierType.ss: if (this.second.HasValue) { displayText = this.second.Value.ToString("00"); } break; case SpecifierType.m: if (this.minute.HasValue) { displayText = this.minute.Value.ToString(); } break; case SpecifierType.mm: if (this.minute.HasValue) { displayText = this.minute.Value.ToString("00"); } break; case SpecifierType.h: if (this.hour.HasValue) { int hour = this.hour.Value; if (hour > 12) { hour -= 12; } displayText = hour.ToString(); } break; case SpecifierType.hh: if (this.hour.HasValue) { int hour = this.hour.Value; if (hour > 12) { hour -= 12; } displayText = hour.ToString("00"); } break; case SpecifierType.H: if (this.hour.HasValue) { displayText = this.hour.ToString(); } break; case SpecifierType.HH: if (this.hour.HasValue) { displayText = this.hour.Value.ToString("00"); } break; case SpecifierType.t: if (this.hour.HasValue) { int hour = this.hour.Value; string designator; if (hour < 12) { designator = this.DateTimeFormat.AMDesignator; } else { designator = this.DateTimeFormat.PMDesignator; } if (!string.IsNullOrEmpty(designator)) { displayText = designator.Substring(0, 1); } else { displayText = string.Empty; } } break; case SpecifierType.tt: if (this.hour.HasValue) { int hour = this.hour.Value; if (hour < 12) { displayText = this.DateTimeFormat.AMDesignator; } else { displayText = this.DateTimeFormat.PMDesignator; } } break; case SpecifierType.d: if (this.day.HasValue) { displayText = this.day.ToString(); } break; case SpecifierType.dd: if (this.day.HasValue) { displayText = this.day.Value.ToString("00"); } break; case SpecifierType.ddd: if (this.value.HasValue) { var dayOfWeek = this.Calendar.GetDayOfWeek(this.value.Value); displayText = this.DateTimeFormat.GetAbbreviatedDayName(dayOfWeek); } break; case SpecifierType.dddd: if (this.value.HasValue) { var dayOfWeek = this.Calendar.GetDayOfWeek(this.value.Value); displayText = this.DateTimeFormat.GetDayName(dayOfWeek); } break; case SpecifierType.M: if (this.month.HasValue) { displayText = this.month.ToString(); } break; case SpecifierType.MM: if (this.month.HasValue) { displayText = this.month.Value.ToString("00"); } break; case SpecifierType.MMM: if (this.month.HasValue) { displayText = this.DateTimeFormat.GetAbbreviatedMonthName(this.month.Value); } break; case SpecifierType.MMMM: if (this.month.HasValue) { displayText = this.DateTimeFormat.GetMonthName(this.month.Value); } break; case SpecifierType.y: if (this.year.HasValue) { displayText = (this.year.Value % 100).ToString(); } break; case SpecifierType.yy: if (this.year.HasValue) { displayText = (this.year.Value % 100).ToString("00"); } break; case SpecifierType.yyy: if (this.year.HasValue) { displayText = this.year.Value.ToString("000"); } break; case SpecifierType.yyyy: if (this.year.HasValue) { displayText = this.year.Value.ToString("0000"); } break; case SpecifierType.g: case SpecifierType.DateSeparator: case SpecifierType.TimeSeparator: return(GetStaticDisplayText(specifier, this.DateTimeFormat, this.Calendar, this.Value)); default: throw new ArgumentOutOfRangeException("specifier.Type"); } return(displayText); }
public bool EditComponentValue(SpecifierInfo specifier, int componentValue, bool commit, int length) { switch (specifier.Type) { case SpecifierType.s: case SpecifierType.ss: if (componentValue < 0) { return(false); } if (componentValue < 60) { if (commit) { this.second = componentValue; this.UpdateDateTimeComponets(); } return(true); } return(false); case SpecifierType.m: case SpecifierType.mm: if (componentValue < 0) { return(false); } if (componentValue < 60) { if (commit) { this.minute = componentValue; this.UpdateDateTimeComponets(); } return(true); } return(false); case SpecifierType.h: case SpecifierType.hh: if (componentValue < 0) { return(false); } if (!commit) { if (componentValue < 24) { return(true); } return(false); } if (componentValue == 0) { this.hour = 0; } else if (componentValue < 12) { this.hour = this.am ? componentValue : componentValue + 12; } else if (componentValue < 24) { this.hour = componentValue; } else { return(false); } this.UpdateDateTimeComponets(); return(true); case SpecifierType.H: case SpecifierType.HH: if (componentValue < 0) { return(false); } if (componentValue < 24) { if (commit) { this.hour = componentValue; this.UpdateDateTimeComponets(); } return(true); } return(false); case SpecifierType.d: case SpecifierType.dd: if (componentValue <= 0) { return(false); } if (this.year.HasValue && this.month.HasValue) { int daysInMonth = this.Calendar.GetDaysInMonth(this.year.Value, this.month.Value); if (componentValue > daysInMonth) { return(false); } } if (commit) { this.day = componentValue; this.UpdateDateTimeComponets(); } return(true); case SpecifierType.M: case SpecifierType.MM: if (componentValue <= 0) { return(false); } if (this.year.HasValue) { int monthsInYear = this.Calendar.GetMonthsInYear(this.year.Value); if (componentValue > monthsInYear) { return(false); } } if (commit) { this.month = componentValue; this.UpdateDateTimeComponets(); } return(true); case SpecifierType.y: case SpecifierType.yy: if (componentValue <= 0) { return(false); } if (componentValue <= 99) { if (commit) { this.year = this.Calendar.ToFourDigitYear(componentValue); this.UpdateDateTimeComponets(); } return(true); } return(false); case SpecifierType.yyy: case SpecifierType.yyyy: if (componentValue <= 0) { return(false); } if (length <= 2 && componentValue <= 99) { if (commit) { this.year = this.Calendar.ToFourDigitYear(componentValue); this.UpdateDateTimeComponets(); } return(true); } else { int maxYear = this.Calendar.GetYear(this.Calendar.MaxSupportedDateTime); int minYear = this.Calendar.GetYear(this.Calendar.MinSupportedDateTime); if (componentValue >= minYear && componentValue <= maxYear) { if (commit) { this.year = componentValue; this.UpdateDateTimeComponets(); } return(true); } else if (componentValue > maxYear) { if (commit) { this.UpdateDateTimeComponets(); return(true); } return(false); } else if (componentValue < minYear) { if (commit) { this.UpdateDateTimeComponets(); } return(true); } } return(false); } return(false); }
public bool DecreaseComponentValue(SpecifierInfo specifier) { switch (specifier.Type) { case SpecifierType.s: case SpecifierType.ss: if (this.second.HasValue && this.second.Value > 0) { this.second--; } else { this.second = 59; } return(true); case SpecifierType.m: case SpecifierType.mm: if (this.minute.HasValue && this.minute.Value > 0) { this.minute--; } else { this.minute = 59; } return(true); case SpecifierType.h: case SpecifierType.hh: case SpecifierType.H: case SpecifierType.HH: if (this.hour.HasValue && this.hour.Value > 0) { this.hour--; } else { this.hour = 23; } this.am = hour < 12; return(true); case SpecifierType.d: case SpecifierType.dd: if (this.day.HasValue && this.day.Value > 1) { this.day--; } else { if (this.year.HasValue && this.month.HasValue) { int daysInMonth = this.Calendar.GetDaysInMonth(this.year.Value, this.month.Value); this.day = daysInMonth; return(true); } this.day = 31; /********/ } return(true); case SpecifierType.M: case SpecifierType.MM: case SpecifierType.MMM: case SpecifierType.MMMM: if (this.month.HasValue && this.month.Value > 1) { this.month--; } else { if (this.year.HasValue) { int monthsInYear = this.Calendar.GetMonthsInYear(this.year.Value); this.month = monthsInYear; return(true); } this.month = 12; /**********/ } return(true); case SpecifierType.y: case SpecifierType.yy: case SpecifierType.yyy: case SpecifierType.yyyy: int minYear = this.Calendar.GetYear(this.Calendar.MinSupportedDateTime); if (this.year.HasValue && this.year.Value > minYear) { this.year--; } else { this.year = this.Calendar.GetYear(DateTime.Now); } return(true); case SpecifierType.t: case SpecifierType.tt: if (this.hour.HasValue) { if (this.hour < 12) { this.hour += 12; this.am = false; } else { this.hour -= 12; this.am = true; } return(true); } return(false); } return(false); }
public void ClearComponentValue(SpecifierInfo specifier) { switch (specifier.Type) { case SpecifierType.s: case SpecifierType.ss: if (this.second != null) { this.second = null; this.UpdateDateTimeComponets(); } break; case SpecifierType.m: case SpecifierType.mm: if (this.minute != null) { this.minute = null; this.UpdateDateTimeComponets(); } break; case SpecifierType.h: case SpecifierType.hh: if (this.hour != null) { this.hour = null; this.UpdateDateTimeComponets(); } break; case SpecifierType.d: case SpecifierType.dd: if (this.day != null) { this.day = null; this.UpdateDateTimeComponets(); } break; case SpecifierType.M: case SpecifierType.MM: if (this.month != null) { this.month = null; this.UpdateDateTimeComponets(); } break; case SpecifierType.y: case SpecifierType.yy: case SpecifierType.yyy: case SpecifierType.yyyy: if (this.year != null) { this.year = null; this.UpdateDateTimeComponets(); } break; } }
public bool ShouldCommit(SpecifierInfo specifier) { if (specifier.ValueType == ValueType.StringLiteral) { return(false); } int?dateTimeValue = null; switch (specifier.Type) { case SpecifierType.s: return(ShouldCommit(this.value.HasValue ? (int?)this.value.Value.Second : null, this.second)); case SpecifierType.m: case SpecifierType.mm: return(ShouldCommit(this.value.HasValue ? (int?)this.value.Value.Minute : null, this.minute)); case SpecifierType.h: case SpecifierType.hh: case SpecifierType.H: case SpecifierType.HH: case SpecifierType.t: // Correct?? case SpecifierType.tt: // Correct?? return(ShouldCommit(this.value.HasValue ? (int?)this.value.Value.Hour : null, this.hour)); case SpecifierType.d: case SpecifierType.dd: case SpecifierType.ddd: // Correct?? case SpecifierType.dddd: // Correct?? if (this.value.HasValue) { dateTimeValue = this.Calendar.GetDayOfMonth(this.value.Value); } return(ShouldCommit(dateTimeValue, this.day)); case SpecifierType.M: case SpecifierType.MM: case SpecifierType.MMM: case SpecifierType.MMMM: if (this.value.HasValue) { dateTimeValue = this.Calendar.GetMonth(this.value.Value); } return(ShouldCommit(dateTimeValue, this.month)); case SpecifierType.y: case SpecifierType.yy: case SpecifierType.yyy: case SpecifierType.yyyy: if (this.value.HasValue) { dateTimeValue = this.Calendar.GetYear(this.value.Value); } return(ShouldCommit(dateTimeValue, this.year)); case SpecifierType.g: case SpecifierType.DateSeparator: case SpecifierType.TimeSeparator: return(false); default: throw new ArgumentOutOfRangeException("specifier.Type"); } }