//[TestCase("yyyy-MM-dd mm:ss")] //[TestCase("yyyy-MM-dd HH:ss")] //[TestCase("yyyy-MM-dd HH:mm")] public void ThrowsFormatExceptionForDateTimeFormatWhenMissingYearOrMonthOrDayOrHourOrMinuteOrSecondPart(string format) { ExceptionTester.CallMethodAndExpectException <FormatException>(() => DateTimeFormatHelper.GetDateTimeFormatInfo(format, false), x => { return(string.Equals(x.Message, "Format string is incorrect. Missing required fields: day, month, year")); }); }
public void ThrowsFormatExceptionForDateTimeFormatWhenAmPmPartFormatIsIncorrect(string format) { ExceptionTester.CallMethodAndExpectException <FormatException>(() => DateTimeFormatHelper.GetDateTimeFormatInfo(format, false), x => { return(string.Equals(x.Message, "Format string is incorrect. AM/PM designator field must be in one of formats: 't' or 'tt'")); }); }
public void ThrowsFormatExceptionForDateTimeFormatWhenDateFormatExpected(string format) { ExceptionTester.CallMethodAndExpectException <FormatException>(() => DateTimeFormatHelper.GetDateTimeFormatInfo(format, true), x => { return(string.Equals(x.Message, "Format string is incorrect. Time fields are not expected")); }); }
public void ThrowsFormatExceptionForDateTimeFormatWhenAmPmPartExistsMoreThanOnce(string format) { ExceptionTester.CallMethodAndExpectException <FormatException>(() => DateTimeFormatHelper.GetDateTimeFormatInfo(format, false), x => { return(string.Equals(x.Message, "Format string is incorrect. AM/PM designator field can not be specified more than once")); }); }
public void ThrowsFormatExceptionForDateTimeFormatWhenHour12PartAndHour24PartExistsBoth(string format) { ExceptionTester.CallMethodAndExpectException <FormatException>(() => DateTimeFormatHelper.GetDateTimeFormatInfo(format, false), x => { return(string.Equals(x.Message, "Format string is incorrect. Hour field must be 12 hour or 24 hour format, but no both")); }); }
public void ThrowsFormatExceptionForDateFormatWhenDayPartFormatIsIncorrect(string format) { ExceptionTester.CallMethodAndExpectException <FormatException>(() => DateTimeFormatHelper.GetDateTimeFormatInfo(format, true), x => { return(string.Equals(x.Message, "Format string is incorrect. Day field must be in one of formats: 'd' or 'dd' or 'ddd' or 'dddd'")); }); }
public void ThrowsFormatExceptionForDateFormatWhenMissingYearOrMonthOrDayPart(string format) { ExceptionTester.CallMethodAndExpectException <FormatException>(() => DateTimeFormatHelper.GetDateTimeFormatInfo(format, true), x => { return(string.Equals(x.Message, "Format string is incorrect. Day, month and year fields are mandatory")); }); }
private void ApplyFormat() { _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(Format, true); IsYearShortFormat = _formatInfo.IsYearShortFormat; NumericTBYear.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsYearShortFormat ? 0 : 1)); NumericTBYear.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsYearShortFormat ? 99 : 3000)); EnableOrDisableYearConverterDependingOnFormat(); 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)); UnsubscribeNumericTextBoxes(); Grid.SetColumn(NumericTBDay, GetPosition(_formatInfo.DayPosition)); Grid.SetColumn(NumericTBMonth, GetPosition(_formatInfo.MonthPosition)); Grid.SetColumn(NumericTBYear, GetPosition(_formatInfo.YearPosition)); Grid.SetColumn(ToggleButtonD, GetPosition(_formatInfo.DayPosition) + 1); Grid.SetColumn(ToggleButtonMo, GetPosition(_formatInfo.MonthPosition) + 1); Grid.SetColumn(ToggleButtonY, GetPosition(_formatInfo.YearPosition) + 1); _textBoxes[_formatInfo.DayPosition] = NumericTBDay; _textBoxes[_formatInfo.MonthPosition] = NumericTBMonth; _textBoxes[_formatInfo.YearPosition] = NumericTBYear; // Fix tab order inside control. NumericTBDay.SetCurrentValue(TabIndexProperty, _formatInfo.DayPosition); NumericTBMonth.SetCurrentValue(TabIndexProperty, _formatInfo.MonthPosition); NumericTBYear.SetCurrentValue(TabIndexProperty, _formatInfo.YearPosition); 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); }
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); }
public static string Format(DateTime dateTime, string format, bool isDateOnly = false) { return(Format(dateTime, DateTimeFormatHelper.GetDateTimeFormatInfo(format, isDateOnly))); }
public static bool TryParse(string input, string format, out DateTime dateTime, bool isDateOnly = false) { return(TryParse(input, DateTimeFormatHelper.GetDateTimeFormatInfo(format, isDateOnly), out dateTime)); }
public static DateTime Parse(string input, string format, bool isDateOnly = false) { return(Parse(input, DateTimeFormatHelper.GetDateTimeFormatInfo(format, isDateOnly))); }
public void CorrectlySplitsDateToParts(string format, char[] formatCharacters, string[] expectedResult) { var result = DateTimeFormatHelper.Split(format, formatCharacters); Assert.That(result, Is.EquivalentTo(expectedResult)); }
public void ReturnsCorrectDateFormatInfoForDateTimeFormat(string format, DateTimeFormatInfo expectedResult) { var result = DateTimeFormatHelper.GetDateTimeFormatInfo(format, false); Assert.That(AreEqual(result, expectedResult), Is.True); }
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; } }