public List <KeyValuePair <string, string> > GetSuggestionList(DateTime dateTime, DateTimePart editablePart, Orc.Controls.DateTimeFormatInfo dateTimeFormatInfo) { switch (editablePart) { case DateTimePart.Day: return(CreateDays(dateTime)); case DateTimePart.Month: return(CreateMonths(dateTime)); case DateTimePart.Hour: return(CreateHours()); case DateTimePart.Hour12: return(CreateHours12()); case DateTimePart.Minute: return(CreateMinutes()); case DateTimePart.Second: return(CreateSeconds()); case DateTimePart.AmPmDesignator: var meridiems = new List <KeyValuePair <string, string> > { CreateItem(Meridiems.LongAM, Meridiems.GetAmForFormat(dateTimeFormatInfo)), CreateItem(Meridiems.LongPM, Meridiems.GetPmForFormat(dateTimeFormatInfo)) }; return(meridiems); default: throw new InvalidOperationException(); } }
public static bool CheckIsAmPmShortFormat(this DateTimeFormatInfo formatInfo, string partValue, out string errorMessage) { Argument.IsNotNull(() => formatInfo); Argument.IsNotNull(() => partValue); errorMessage = string.Empty; switch (formatInfo.IsAmPmShortFormat) { case true when !(Meridiems.IsShortAm(partValue) || Meridiems.IsShortPm(partValue)): { errorMessage = "Invalid AM/PM designator value"; return(false); } case false when !(Meridiems.IsLongAm(partValue) || Meridiems.IsLongPm(partValue)): { errorMessage = "Invalid AM/PM designator value"; return(false); } } return(true); }
private void ApplyFormat() { _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(Format, false); IsYearShortFormat = _formatInfo.IsYearShortFormat; NumericTBYear.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsYearShortFormat ? 0 : 1)); NumericTBYear.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsYearShortFormat ? 99 : 3000)); IsHour12Format = _formatInfo.IsHour12Format.Value; NumericTBHour.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsHour12Format.Value ? 1 : 0)); NumericTBHour.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsHour12Format.Value ? 12 : 23)); ToggleButtonH.SetCurrentValue(TagProperty, _formatInfo.IsHour12Format.Value ? DateTimePart.Hour12 : DateTimePart.Hour); IsAmPmShortFormat = _formatInfo.IsAmPmShortFormat.Value; EnableOrDisableYearConverterDependingOnFormat(); EnableOrDisableHourConverterDependingOnFormat(); EnableOrDisableAmPmConverterDependingOnFormat(); ListTBAmPm.SetCurrentValue(ListTextBox.ListOfValuesProperty, new List <string>() { Meridiems.GetAmForFormat(_formatInfo), Meridiems.GetPmForFormat(_formatInfo) }); NumericTBDay.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.DayFormat.Length)); NumericTBMonth.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MonthFormat.Length)); NumericTBYear.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.YearFormat.Length)); NumericTBHour.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.HourFormat.Length)); NumericTBMinute.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MinuteFormat.Length)); NumericTBSecond.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.SecondFormat.Length)); UnsubscribeNumericTextBoxes(); Grid.SetColumn(NumericTBDay, GetPosition(_formatInfo.DayPosition)); Grid.SetColumn(NumericTBMonth, GetPosition(_formatInfo.MonthPosition)); Grid.SetColumn(NumericTBYear, GetPosition(_formatInfo.YearPosition)); Grid.SetColumn(NumericTBHour, GetPosition(_formatInfo.HourPosition.Value)); Grid.SetColumn(NumericTBMinute, GetPosition(_formatInfo.MinutePosition.Value)); Grid.SetColumn(NumericTBSecond, GetPosition(_formatInfo.SecondPosition.Value)); Grid.SetColumn(ListTBAmPm, GetPosition(_formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value)); Grid.SetColumn(ToggleButtonD, GetPosition(_formatInfo.DayPosition) + 1); Grid.SetColumn(ToggleButtonMo, GetPosition(_formatInfo.MonthPosition) + 1); Grid.SetColumn(ToggleButtonY, GetPosition(_formatInfo.YearPosition) + 1); Grid.SetColumn(ToggleButtonH, GetPosition(_formatInfo.HourPosition.Value) + 1); Grid.SetColumn(ToggleButtonM, GetPosition(_formatInfo.MinutePosition.Value) + 1); Grid.SetColumn(ToggleButtonS, GetPosition(_formatInfo.SecondPosition.Value) + 1); Grid.SetColumn(ToggleButtonT, GetPosition(_formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value) + 1); // Fix positions which could be broken, because of AM/PM textblock. int dayPos = _formatInfo.DayPosition, monthPos = _formatInfo.MonthPosition, yearPos = _formatInfo.YearPosition, hourPos = _formatInfo.HourPosition.Value, minutePos = _formatInfo.MinutePosition.Value, secondPos = _formatInfo.SecondPosition.Value, amPmPos = _formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value; FixNumericTextBoxesPositions(ref dayPos, ref monthPos, ref yearPos, ref hourPos, ref minutePos, ref secondPos, ref amPmPos); _textBoxes[dayPos] = NumericTBDay; _textBoxes[monthPos] = NumericTBMonth; _textBoxes[yearPos] = NumericTBYear; _textBoxes[hourPos] = NumericTBHour; _textBoxes[minutePos] = NumericTBMinute; _textBoxes[secondPos] = NumericTBSecond; _textBoxes[amPmPos] = ListTBAmPm; // Fix tab order inside control. NumericTBDay.SetCurrentValue(TabIndexProperty, dayPos); NumericTBMonth.SetCurrentValue(TabIndexProperty, monthPos); NumericTBYear.SetCurrentValue(TabIndexProperty, yearPos); NumericTBHour.SetCurrentValue(TabIndexProperty, hourPos); NumericTBMinute.SetCurrentValue(TabIndexProperty, minutePos); NumericTBSecond.SetCurrentValue(TabIndexProperty, secondPos); ListTBAmPm.SetCurrentValue(TabIndexProperty, amPmPos); DatePickerIcon.SetCurrentValue(TabIndexProperty, Int32.MaxValue); SubscribeNumericTextBoxes(); Separator1.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator1); Separator2.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator2); Separator3.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator3); Separator4.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator4); Separator5.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator5); Separator6.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator6); Separator7.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator7); }
internal static string Format(DateTime dateTime, DateTimeFormatInfo formatInfo) { Argument.IsNotNull(() => formatInfo); var parts = new List <KeyValuePair <int, string> > { new KeyValuePair <int, string>(formatInfo.DayPosition, dateTime.Day.ToString(NumberFormatHelper.GetFormat(formatInfo.DayFormat.Length))), new KeyValuePair <int, string>(formatInfo.MonthPosition, dateTime.Month.ToString(NumberFormatHelper.GetFormat(formatInfo.MonthFormat.Length))) }; if (formatInfo.IsYearShortFormat) { var yearShort = dateTime.Year - (dateTime.Year - dateTime.Year % 100); parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, yearShort.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length)))); } else { parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, dateTime.Year.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length)))); } if (!formatInfo.IsDateOnly) { if (formatInfo.IsHour12Format == true) { var hour12 = dateTime.Hour % 12 > 0 ? dateTime.Hour % 12 : 12; parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, hour12.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length)))); } else { parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, dateTime.Hour.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length)))); } parts.Add(new KeyValuePair <int, string>(formatInfo.MinutePosition.Value, dateTime.Minute.ToString(NumberFormatHelper.GetFormat(formatInfo.MinuteFormat.Length)))); if (formatInfo.SecondPosition.HasValue) { parts.Add(new KeyValuePair <int, string>(formatInfo.SecondPosition.Value, dateTime.Second.ToString(NumberFormatHelper.GetFormat(formatInfo.SecondFormat.Length)))); } if (formatInfo.AmPmPosition.HasValue) { parts.Add(new KeyValuePair <int, string>(formatInfo.AmPmPosition.Value, dateTime.Hour >= 12 ? Meridiems.GetPmForFormat(formatInfo) : Meridiems.GetAmForFormat(formatInfo))); } } parts = parts.OrderBy(x => x.Key).ToList(); return(BuildFormatString(formatInfo, parts)); }
private static DateTime?Parse(string input, DateTimeFormatInfo formatInfo, bool throwOnError = true) { Argument.IsNotNull(() => formatInfo); var year = 0; var month = 0; var day = 0; var hour = 0; var minute = 0; var second = 0; var amPm = default(string); var separator = default(string); var i = 0; var partValue = default(string); for (i = 0; i < 7; i++) { separator = formatInfo.GetSeparator(i); if (separator != null && separator.Length > 0 && input.Length > 0) { if (!input.StartsWith(separator)) { return(ThrowOnError <FormatException>("Invalid value. Value does not match format", null, throwOnError)); } input = input.Substring(separator.Length); } if (separator == null && i == (formatInfo.MaxPosition + 1) && input.Length > 0) { // We have come to the end of format but there is still some characters left in the input. // They are probably AM/PM values even though the format is 24 hours. We will try and parse them correctly formatInfo.AmPmPosition = i; input = input.Trim(); if (input.Length == 2) { formatInfo.AmPmFormat = "tt"; } else if (input.Length == 1) { formatInfo.AmPmFormat = "t"; } } if (i == formatInfo.YearPosition) { partValue = new string(input.TakeWhile(x => char.IsDigit(x)).ToArray()); if (formatInfo.YearFormat.Length == 1 && (partValue.Length < 1 || partValue.Length > 2)) // 'y' { return(ThrowOnError <FormatException>("Invalid year value. Year must contain 1 or 2 digits", null, throwOnError)); } else if (formatInfo.YearFormat.Length == 2 && partValue.Length != 2) // 'yy' { return(ThrowOnError <FormatException>("Invalid year value. Year must contain 2 digits", null, throwOnError)); } else if (formatInfo.YearFormat.Length == 3 && (partValue.Length < 3 || partValue.Length > 5)) // 'yyy' { return(ThrowOnError <FormatException>("Invalid year value. Year must contain 3 or 4 or 5 digits", null, throwOnError)); } else if (formatInfo.YearFormat.Length == 4 && (partValue.Length < 4 || partValue.Length > 5)) // 'yyyy' { return(ThrowOnError <FormatException>("Invalid year value. Year must contain 4 or 5 digits", null, throwOnError)); } else if (formatInfo.YearFormat.Length == 5 && partValue.Length != 5) // 'yyyyy' { return(ThrowOnError <FormatException>("Invalid year value. Year must contain 5 digits", null, throwOnError)); } year = int.Parse(partValue); // There is no reason to fail. year += formatInfo.IsYearShortFormat == true ? 2000 : 0; input = input.Substring(partValue.Length); } else if (i == formatInfo.MonthPosition) { partValue = new string(input.TakeWhile(x => char.IsDigit(x)).ToArray()); if (formatInfo.MonthFormat.Length == 1 && (partValue.Length < 1 || partValue.Length > 2)) // 'M' { return(ThrowOnError <FormatException>("Invalid month value. Month must contain 1 or 2 digits", null, throwOnError)); } else if (formatInfo.MonthFormat.Length == 2 && partValue.Length != 2) // 'MM' { return(ThrowOnError <FormatException>("Invalid month value. Month must contain 2 digits", null, throwOnError)); } month = int.Parse(partValue); // There is no reason to fail. input = input.Substring(partValue.Length); } else if (i == formatInfo.DayPosition) { partValue = new string(input.TakeWhile(x => char.IsDigit(x)).ToArray()); if (formatInfo.DayFormat.Length == 1 && (partValue.Length < 1 || partValue.Length > 2)) // 'd' { return(ThrowOnError <FormatException>("Invalid day value. Day must contain 1 or 2 digits", null, throwOnError)); } else if (formatInfo.DayFormat.Length == 2 && partValue.Length != 2) // 'dd' { return(ThrowOnError <FormatException>("Invalid day value. Day must contain 2 digits", null, throwOnError)); } day = int.Parse(partValue); // There is no reason to fail. input = input.Substring(partValue.Length); } else if (i == formatInfo.HourPosition) { partValue = new string(input.TakeWhile(x => char.IsDigit(x)).ToArray()); if (formatInfo.HourFormat.Length == 1 && (partValue.Length < 1 || partValue.Length > 2)) // 'h' or 'H' { return(ThrowOnError <FormatException>("Invalid hour value. Hour must contain 1 or 2 digits", null, throwOnError)); } else if (formatInfo.HourFormat.Length == 2 && partValue.Length != 2) // 'hh' or 'HH' { return(ThrowOnError <FormatException>("Invalid hour value. Hour must contain 2 digits", null, throwOnError)); } hour = int.Parse(partValue); // There is no reason to fail. if (hour < 0 && hour >= 24) { return(ThrowOnError <FormatException>("Invalid hour value. Hour must be in range <0, 24> for short format", null, throwOnError)); } input = input.Substring(partValue.Length); } else if (i == formatInfo.MinutePosition) { partValue = new string(input.TakeWhile(x => char.IsDigit(x)).ToArray()); if (formatInfo.MinuteFormat.Length == 1 && (partValue.Length < 1 || partValue.Length > 2)) // 'm' { return(ThrowOnError <FormatException>("Invalid minute value. Minute must contain 1 or 2 digits", null, throwOnError)); } else if (formatInfo.MinuteFormat.Length == 2 && partValue.Length != 2) // 'mm' { return(ThrowOnError <FormatException>("Invalid minute value. Minute must contain 2 digits", null, throwOnError)); } minute = int.Parse(partValue); // There is no reason to fail. input = input.Substring(partValue.Length); } else if (i == formatInfo.SecondPosition) { partValue = new string(input.TakeWhile(x => char.IsDigit(x)).ToArray()); if (formatInfo.SecondFormat.Length == 1 && (partValue.Length < 1 || partValue.Length > 2)) { return(ThrowOnError <FormatException>("Invalid second value. Second must contain 1 or 2 digits", null, throwOnError)); // 's' } else if (formatInfo.SecondFormat.Length == 2 && partValue.Length != 2) // 'ss' { return(ThrowOnError <FormatException>("Invalid second value. Second must contain 2 digits", null, throwOnError)); } second = int.Parse(partValue); // There is no reason to fail. input = input.Substring(partValue.Length); } else if (i == formatInfo.AmPmPosition) { partValue = new string(input.Take(formatInfo.AmPmFormat.Length).ToArray()); if (partValue.Length <= 0) { // We allow the input string to not have AM/PM even if the format specifies them continue; } if (formatInfo.IsAmPmShortFormat == true && !(Meridiems.IsShortAm(partValue) || Meridiems.IsShortPm(partValue))) { return(ThrowOnError <FormatException>("Invalid AM/PM designator value", null, throwOnError)); } else if (formatInfo.IsAmPmShortFormat == false && !(Meridiems.IsLongAm(partValue) || Meridiems.IsLongPm(partValue))) { return(ThrowOnError <FormatException>("Invalid AM/PM designator value", null, throwOnError)); } amPm = partValue; // We need to make sure hour is less than 12, because we could accept values like: 05/02/2017 16:41:24 PM hour += (Meridiems.IsPm(amPm) && hour < 12) ? 12 : 0; input = input.Substring(partValue.Length); } } separator = formatInfo.GetSeparator(i); if (separator != null && separator.Length > 0) { if (!input.StartsWith(separator)) { return(ThrowOnError <FormatException>("Invalid value. Value does not match to format", null, throwOnError)); } } try { return(new DateTime(year, month, day, hour, minute, second)); } catch (Exception exc) { return(ThrowOnError <FormatException>("Invalid value. It's not possible to build new DateTime object", exc, throwOnError)); } }
public List <KeyValuePair <string, string> > GetSuggestionList(DateTime dateTime, DateTimePart editablePart, Orc.Controls.DateTimeFormatInfo dateTimeFormatInfo) { switch (editablePart) { case DateTimePart.Day: var days = new List <KeyValuePair <string, string> >(); var daysInMonth = DateTime.DaysInMonth(dateTime.Year, dateTime.Month); for (var i = 1; i <= daysInMonth; i++) { var day = new DateTime(dateTime.Year, dateTime.Month, i).ToString("d ddd", CultureInfo.InvariantCulture); days.Add(CreateItem(i.ToString(), day)); } return(days); case DateTimePart.Month: var months = new List <KeyValuePair <string, string> >(); for (var i = 1; i <= 12; i++) { var month = new DateTime(dateTime.Year, i, 1).ToString("M MMM", CultureInfo.InvariantCulture); months.Add(CreateItem(i.ToString(), month)); } return(months); case DateTimePart.Hour: var hours = new List <KeyValuePair <string, string> >(); for (var i = 0; i < 24; i++) { hours.Add(i < 10 ? CreateItem(i.ToString(), (" " + i)) : CreateItem(i.ToString(), i.ToString())); } return(hours); case DateTimePart.Hour12: var hours12 = new List <KeyValuePair <string, string> >(); for (var i = 1; i <= 12; i++) { hours12.Add(i < 10 ? CreateItem(i.ToString(), (" " + i)) : CreateItem(i.ToString(), i.ToString())); } return(hours12); case DateTimePart.Minute: var minutes = new List <KeyValuePair <string, string> >(); for (var i = 0; i < 60; i++) { minutes.Add(CreateItem(i.ToString("00"), i.ToString("00"))); } return(minutes); case DateTimePart.Second: var seconds = new List <KeyValuePair <string, string> >(); for (var i = 0; i < 60; i++) { seconds.Add(CreateItem(i.ToString("00"), i.ToString("00"))); } return(seconds); case DateTimePart.AmPmDesignator: var meridiems = new List <KeyValuePair <string, string> >(); meridiems.Add(CreateItem(Meridiems.LongAM, Meridiems.GetAmForFormat(dateTimeFormatInfo))); meridiems.Add(CreateItem(Meridiems.LongPM, Meridiems.GetPmForFormat(dateTimeFormatInfo))); return(meridiems); default: throw new InvalidOperationException(); } }
private static DateTime?Parse(string input, DateTimeFormatInfo formatInfo, bool throwOnError) { Argument.IsNotNull(() => formatInfo); var year = 0; var month = 0; var day = 0; var hour = 0; var minute = 0; var second = 0; string separator; int i; for (i = 0; i < 7; i++) { separator = formatInfo.GetSeparator(i); if (!string.IsNullOrEmpty(separator) && input.Length > 0) { if (!input.StartsWith(separator)) { return(ThrowOnError <FormatException>("Invalid value. Value does not match format", null, throwOnError)); } input = input.Substring(separator.Length); } if (separator is null && i == formatInfo.MaxPosition + 1 && input.Length > 0) { input = ParseAmPmFormat(input, formatInfo, i); } if (i == formatInfo.YearPosition) { var partValue = new string(input.TakeWhile(char.IsDigit).ToArray()); if (!formatInfo.CheckYearFormat(partValue, out var errorMessage)) { return(ThrowOnError <FormatException>(errorMessage, null, throwOnError)); } input = input.ChunkIntValue(partValue, out year); year += formatInfo.IsYearShortFormat ? 2000 : 0; continue; } if (i == formatInfo.MonthPosition) { var partValue = new string(input.TakeWhile(char.IsDigit).ToArray()); if (!formatInfo.CheckMonthFormat(partValue, out var errorMessage)) { return(ThrowOnError <FormatException>(errorMessage, null, throwOnError)); } input = input.ChunkIntValue(partValue, out month); continue; } if (i == formatInfo.DayPosition) { var partValue = new string(input.TakeWhile(char.IsDigit).ToArray()); if (!formatInfo.CheckDayFormat(partValue, out var errorMessage)) { return(ThrowOnError <FormatException>(errorMessage, null, throwOnError)); } input = input.ChunkIntValue(partValue, out day); continue; } if (i == formatInfo.HourPosition) { var partValue = new string(input.TakeWhile(char.IsDigit).ToArray()); if (!formatInfo.CheckHourFormat(partValue, out var errorMessage)) { return(ThrowOnError <FormatException>(errorMessage, null, throwOnError)); } input = input.ChunkIntValue(partValue, out hour); if (hour < 0 || hour >= 24) { return(ThrowOnError <FormatException>("Invalid hour value. Hour must be in range <0, 24> for short format", null, throwOnError)); } continue; } if (i == formatInfo.MinutePosition) { var partValue = new string(input.TakeWhile(char.IsDigit).ToArray()); if (!formatInfo.CheckMinuteFormat(partValue, out var errorMessage)) { return(ThrowOnError <FormatException>(errorMessage, null, throwOnError)); } input = input.ChunkIntValue(partValue, out minute); continue; } if (i == formatInfo.SecondPosition) { var partValue = new string(input.TakeWhile(char.IsDigit).ToArray()); if (!formatInfo.CheckSecondFormat(partValue, out var errorMessage)) { return(ThrowOnError <FormatException>(errorMessage, null, throwOnError)); } input = input.ChunkIntValue(partValue, out second); continue; } if (i == formatInfo.AmPmPosition) { var partValue = new string(input.Take(formatInfo.AmPmFormat.Length).ToArray()); if (partValue.Length <= 0) { // We allow the input string to not have AM/PM even if the format specifies them continue; } if (!formatInfo.CheckIsAmPmShortFormat(partValue, out var amPmErrorMessage)) { return(ThrowOnError <FormatException>(amPmErrorMessage, null, throwOnError)); } var amPm = partValue; // We need to make sure hour is less than 12, because we could accept values like: 05/02/2017 16:41:24 PM hour += Meridiems.IsPm(amPm) && hour < 12 ? 12 : 0; input = input.Substring(partValue.Length); } } separator = formatInfo.GetSeparator(i); if (!string.IsNullOrEmpty(separator) && !input.StartsWith(separator)) { return(ThrowOnError <FormatException>("Invalid value. Value does not match to format", null, throwOnError)); } try { return(new DateTime(year, month, day, hour, minute, second)); } catch (Exception exc) { return(ThrowOnError <FormatException>("Invalid value. It's not possible to build new DateTime object", exc, throwOnError)); } }
internal static string Format(DateTime dateTime, DateTimeFormatInfo formatInfo) { Argument.IsNotNull(() => formatInfo); var parts = new List <KeyValuePair <int, string> > { new KeyValuePair <int, string>(formatInfo.DayPosition, dateTime.Day.ToString(NumberFormatHelper.GetFormat(formatInfo.DayFormat.Length))), new KeyValuePair <int, string>(formatInfo.MonthPosition, dateTime.Month.ToString(NumberFormatHelper.GetFormat(formatInfo.MonthFormat.Length))) }; if (formatInfo.IsYearShortFormat) { var yearShort = dateTime.Year - (dateTime.Year - dateTime.Year % 100); parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, yearShort.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length)))); } else { parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, dateTime.Year.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length)))); } if (!formatInfo.IsDateOnly) { if (formatInfo.IsHour12Format == true) { var hour12 = dateTime.Hour % 12 > 0 ? dateTime.Hour % 12 : 12; parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, hour12.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length)))); } else { parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, dateTime.Hour.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length)))); } parts.Add(new KeyValuePair <int, string>(formatInfo.MinutePosition.Value, dateTime.Minute.ToString(NumberFormatHelper.GetFormat(formatInfo.MinuteFormat.Length)))); parts.Add(new KeyValuePair <int, string>(formatInfo.SecondPosition.Value, dateTime.Second.ToString(NumberFormatHelper.GetFormat(formatInfo.SecondFormat.Length)))); if (formatInfo.AmPmPosition.HasValue) { parts.Add(new KeyValuePair <int, string>(formatInfo.AmPmPosition.Value, dateTime.Hour >= 12 ? Meridiems.GetPmForFormat(formatInfo) : Meridiems.GetAmForFormat(formatInfo))); } } parts = parts.OrderBy(x => x.Key).ToList(); // Always contain year, month, day part. var builder = new StringBuilder(); builder.Append(formatInfo.Separator0); builder.Append(parts[0].Value); builder.Append(formatInfo.Separator1); builder.Append(parts[1].Value); builder.Append(formatInfo.Separator2); builder.Append(parts[2].Value); builder.Append(formatInfo.Separator3); if (parts.Count <= 3) { return(builder.ToString()); } builder.Append(parts[3].Value); builder.Append(formatInfo.Separator4); builder.Append(parts[4].Value); builder.Append(formatInfo.Separator5); builder.Append(parts[5].Value); builder.Append(formatInfo.Separator6); if (parts.Count <= 6) { return(builder.ToString()); } builder.Append(parts[6].Value); builder.Append(formatInfo.Separator7); return(builder.ToString()); }
private void ApplyFormat() { _applyingFormat = true; try { var format = Format; _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(format); var hasLongTimeFormat = !(_formatInfo.HourFormat is null || _formatInfo.MinuteFormat is null || _formatInfo.SecondFormat is null); var hasAnyTimeFormat = !(_formatInfo.HourFormat is null && _formatInfo.MinuteFormat is null && _formatInfo.SecondFormat is null); SetCurrentValue(HideTimeProperty, !hasAnyTimeFormat || _hideTime); if (!hasLongTimeFormat) { var timePattern = DateTimeFormatHelper.ExtractTimePatternFromFormat(format); if (!string.IsNullOrEmpty(timePattern)) { timePattern = DateTimeFormatHelper.FindMatchedLongTimePattern(CultureInfo.CurrentUICulture, timePattern); } if (string.IsNullOrEmpty(timePattern)) { timePattern = CultureInfo.CurrentUICulture.DateTimeFormat.LongTimePattern; } var datePattern = DateTimeFormatHelper.ExtractDatePatternFromFormat(format); format = $"{datePattern} {timePattern}"; _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(format); } if (!hasAnyTimeFormat) { } IsYearShortFormat = _formatInfo.IsYearShortFormat; NumericTBYear.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsYearShortFormat ? 0 : 1)); NumericTBYear.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsYearShortFormat ? 99 : 3000)); var isHour12Format = _formatInfo.IsHour12Format ?? true; IsHour12Format = isHour12Format; NumericTBHour.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(isHour12Format ? 1 : 0)); NumericTBHour.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(isHour12Format ? 12 : 23)); ToggleButtonH.SetCurrentValue(TagProperty, isHour12Format ? DateTimePart.Hour12 : DateTimePart.Hour); IsAmPmShortFormat = _formatInfo.IsAmPmShortFormat.Value; EnableOrDisableYearConverterDependingOnFormat(); EnableOrDisableHourConverterDependingOnFormat(); EnableOrDisableAmPmConverterDependingOnFormat(); ListTBAmPm.SetCurrentValue(ListTextBox.ListOfValuesProperty, new List <string>() { Meridiems.GetAmForFormat(_formatInfo), Meridiems.GetPmForFormat(_formatInfo) }); NumericTBDay.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.DayFormat.Length)); NumericTBMonth.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MonthFormat.Length)); NumericTBYear.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.YearFormat.Length)); NumericTBHour.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.HourFormat.Length)); NumericTBMinute.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MinuteFormat.Length)); NumericTBSecond.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.SecondFormat?.Length ?? 0)); UnsubscribeNumericTextBoxes(); Grid.SetColumn(NumericTBDay, GetPosition(_formatInfo.DayPosition)); Grid.SetColumn(NumericTBMonth, GetPosition(_formatInfo.MonthPosition)); Grid.SetColumn(NumericTBYear, GetPosition(_formatInfo.YearPosition)); Grid.SetColumn(NumericTBHour, GetPosition(_formatInfo.HourPosition.Value)); Grid.SetColumn(NumericTBMinute, GetPosition(_formatInfo.MinutePosition.Value)); Grid.SetColumn(NumericTBSecond, GetPosition(_formatInfo.SecondPosition ?? _defaultSecondFormatPosition)); Grid.SetColumn(ListTBAmPm, GetPosition(_formatInfo.AmPmPosition ?? _defaultAmPmFormatPosition)); Grid.SetColumn(ToggleButtonD, GetPosition(_formatInfo.DayPosition) + 1); Grid.SetColumn(ToggleButtonMo, GetPosition(_formatInfo.MonthPosition) + 1); Grid.SetColumn(ToggleButtonY, GetPosition(_formatInfo.YearPosition) + 1); Grid.SetColumn(ToggleButtonH, GetPosition(_formatInfo.HourPosition.Value) + 1); Grid.SetColumn(ToggleButtonM, GetPosition(_formatInfo.MinutePosition.Value) + 1); Grid.SetColumn(ToggleButtonS, GetPosition(_formatInfo.SecondPosition ?? _defaultSecondFormatPosition) + 1); //hide parts according to Hide options and format bool isHiddenManual = HideSeconds || HideTime; NumericTBSecond.SetCurrentValue(NumericTextBox.VisibilityProperty, _formatInfo.SecondFormat is null || isHiddenManual ? Visibility.Collapsed : Visibility.Visible); ToggleButtonS.SetCurrentValue(NumericTextBox.VisibilityProperty, _formatInfo.SecondFormat is null || isHiddenManual ? Visibility.Collapsed : Visibility.Visible); Grid.SetColumn(ToggleButtonT, GetPosition((_formatInfo.AmPmPosition ?? _defaultAmPmFormatPosition) + 1)); // Fix positions which could be broken, because of AM/PM textblock. // Fix for seconds in a same way int dayPos = _formatInfo.DayPosition, monthPos = _formatInfo.MonthPosition, yearPos = _formatInfo.YearPosition, hourPos = _formatInfo.HourPosition.Value, minutePos = _formatInfo.MinutePosition.Value, secondPos = _formatInfo.SecondPosition ?? 5, amPmPos = _formatInfo.AmPmPosition ?? 6; FixNumericTextBoxesPositions(ref dayPos, ref monthPos, ref yearPos, ref hourPos, ref minutePos, ref secondPos, ref amPmPos); _textBoxes[dayPos] = NumericTBDay; _textBoxes[monthPos] = NumericTBMonth; _textBoxes[yearPos] = NumericTBYear; _textBoxes[hourPos] = NumericTBHour; _textBoxes[minutePos] = NumericTBMinute; _textBoxes[secondPos] = NumericTBSecond; _textBoxes[amPmPos] = ListTBAmPm; // Fix tab order inside control. NumericTBDay.SetCurrentValue(TabIndexProperty, dayPos); NumericTBMonth.SetCurrentValue(TabIndexProperty, monthPos); NumericTBYear.SetCurrentValue(TabIndexProperty, yearPos); NumericTBHour.SetCurrentValue(TabIndexProperty, hourPos); NumericTBMinute.SetCurrentValue(TabIndexProperty, minutePos); NumericTBSecond.SetCurrentValue(TabIndexProperty, secondPos); ListTBAmPm.SetCurrentValue(TabIndexProperty, amPmPos); DatePickerIcon.SetCurrentValue(TabIndexProperty, Int32.MaxValue); SubscribeNumericTextBoxes(); Separator1.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator1); Separator2.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator2); Separator3.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator3); Separator4.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator4); Separator5.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator5); Separator6.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator6); Separator7.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator7); } finally { _applyingFormat = false; } }