bool SetText(ustring text) { ustring [] vals = text.Split(ustring.Make(sepChar)); bool isValidTime = true; int hour = Int32.Parse(vals [0].ToString()); int minute = Int32.Parse(vals [1].ToString()); int second = isShort ? 0 : Int32.Parse(vals [2].ToString()); if (hour < 0) { isValidTime = false; hour = 0; vals [0] = "0"; } else if (hour > 23) { isValidTime = false; hour = 23; vals [0] = "23"; } if (minute < 0) { isValidTime = false; minute = 0; vals [1] = "0"; } else if (minute > 59) { isValidTime = false; minute = 59; vals [1] = "59"; } if (second < 0) { isValidTime = false; second = 0; vals [2] = "0"; } else if (second > 59) { isValidTime = false; second = 59; vals [2] = "59"; } string time = isShort ? $" {hour,2:00}{sepChar}{minute,2:00}" : $" {hour,2:00}{sepChar}{minute,2:00}{sepChar}{second,2:00}"; Text = time; if (!DateTime.TryParseExact(text.ToString(), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) || !isValidTime) { return(false); } return(true); }
bool SetText(ustring text) { ustring[] vals = text.Split(ustring.Make(sepChar)); ustring[] frm = ustring.Make(Format).Split(ustring.Make(sepChar)); bool isValidDate = true; int idx = GetFormatIndex(frm, "y"); int year = Int32.Parse(vals[idx].ToString()); int month; int day; idx = GetFormatIndex(frm, "M"); if (Int32.Parse(vals[idx].ToString()) < 1) { isValidDate = false; month = 1; vals[idx] = "1"; } else if (Int32.Parse(vals[idx].ToString()) > 12) { isValidDate = false; month = 12; vals[idx] = "12"; } else { month = Int32.Parse(vals[idx].ToString()); } idx = GetFormatIndex(frm, "d"); if (Int32.Parse(vals[idx].ToString()) < 1) { isValidDate = false; day = 1; vals[idx] = "1"; } else if (Int32.Parse(vals[idx].ToString()) > 31) { isValidDate = false; day = DateTime.DaysInMonth(year, month); vals[idx] = day.ToString(); } else { day = Int32.Parse(vals[idx].ToString()); } string date = GetData(month, day, year, frm); Text = date; if (!DateTime.TryParseExact(date, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) || !isValidDate) { return(false); } return(true); }
bool SetText(ustring text) { // FIXME: This validation could be made better by calculating the actual min/max values // for month/day/year. This has a 'good' chance of keeping things valid ustring[] vals = text.Split(ustring.Make(sepChar)); int month = Math.Max(Math.Min(Int32.Parse(vals[0].ToString()), 12), 1); int day = Math.Max(Math.Min(Int32.Parse(vals[1].ToString()), 31), 1); string date = isShort ? $" {month,2:00}{sepChar}{day,2:00}{sepChar}{vals[2].ToString(),2:00}" : $" {month,2:00}{sepChar}{day,2:00}{sepChar}{vals[2].ToString(),4:0000}"; if (!DateTime.TryParseExact(date, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) { return(false); } Text = date; return(true); }