From 36fb40b6bd4eeacb14b9a152ffb8e147227539f4 Mon Sep 17 00:00:00 2001 From: Danich Volk Date: Tue, 13 Jul 2021 21:18:55 +0300 Subject: [PATCH] CalendarPicker: + Added days, monthes and years range check + Added language selection * Fixed default language (it is English now) --- .../Controls/Inline/CalendarPicker.cs | 16 +++++++++------- TelegramBotBase/Tools/Time.cs | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/TelegramBotBase/Controls/Inline/CalendarPicker.cs b/TelegramBotBase/Controls/Inline/CalendarPicker.cs index 363b409..0fc968d 100644 --- a/TelegramBotBase/Controls/Inline/CalendarPicker.cs +++ b/TelegramBotBase/Controls/Inline/CalendarPicker.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; using TelegramBotBase.Base; using TelegramBotBase.Enums; using TelegramBotBase.Form; - +using TelegramBotBase.Tools; using static TelegramBotBase.Tools.Arrays; using static TelegramBotBase.Tools.Time; @@ -36,15 +36,17 @@ namespace TelegramBotBase.Controls.Inline public bool EnableMonthView { get; set; } = true; public bool EnableYearView { get; set; } = true; - - public CalendarPicker() + + public CalendarPicker(CultureInfo culture) { this.SelectedDate = DateTime.Today; this.VisibleMonth = DateTime.Today; this.FirstDayOfWeek = DayOfWeek.Monday; - this.Culture = new CultureInfo("de-de"); + this.Culture = culture; this.PickerMode = eMonthPickerMode.day; } + + public CalendarPicker() : this(new CultureInfo("en-en")) { } @@ -124,13 +126,13 @@ namespace TelegramBotBase.Controls.Inline default: int day = 0; - if (result.RawData.StartsWith("d-") && int.TryParse(result.RawData.Split('-')[1], out day)) + if (result.RawData.StartsWith("d-") && TryParseDay(result.RawData.Split('-')[1], this.SelectedDate, out day)) { this.SelectedDate = new DateTime(this.VisibleMonth.Year, this.VisibleMonth.Month, day); } int month = 0; - if (result.RawData.StartsWith("m-") && int.TryParse(result.RawData.Split('-')[1], out month)) + if (result.RawData.StartsWith("m-") && TryParseMonth(result.RawData.Split('-')[1], out month)) { this.SelectedDate = new DateTime(this.VisibleMonth.Year, month, 1); this.VisibleMonth = this.SelectedDate; @@ -142,7 +144,7 @@ namespace TelegramBotBase.Controls.Inline } int year = 0; - if (result.RawData.StartsWith("y-") && int.TryParse(result.RawData.Split('-')[1], out year)) + if (result.RawData.StartsWith("y-") && TryParseYear(result.RawData.Split('-')[1], out year)) { this.SelectedDate = new DateTime(year, SelectedDate.Month, SelectedDate.Day); this.VisibleMonth = this.SelectedDate; diff --git a/TelegramBotBase/Tools/Time.cs b/TelegramBotBase/Tools/Time.cs index 66cdb85..b0cd93f 100644 --- a/TelegramBotBase/Tools/Time.cs +++ b/TelegramBotBase/Tools/Time.cs @@ -8,6 +8,21 @@ namespace TelegramBotBase.Tools { public static class Time { + public static bool TryParseDay(string src, DateTime currentDate, out int resultDay) + { + return int.TryParse(src, out resultDay) && resultDay >= 1 && resultDay <= DateTime.DaysInMonth(currentDate.Year, currentDate.Month); + } + + public static bool TryParseMonth(string src, out int resultMonth) + { + return int.TryParse(src, out resultMonth) && resultMonth >= 1 && resultMonth <= 12; + } + + public static bool TryParseYear(string src, out int resultYear) + { + return int.TryParse(src, out resultYear) && resultYear >= 0 && resultYear <= DateTime.MaxValue.Year; + } + public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { int diff = dt.DayOfWeek - startOfWeek;