From fba59f60845843f9e39433b2e4ac05f3091dc6cb Mon Sep 17 00:00:00 2001 From: BDisp Date: Sun, 24 May 2020 00:44:55 +0100 Subject: [PATCH] Patch to bypass a wrong invalid date with DateTime. --- Terminal.Gui/Views/DateField.cs | 32 +++++++++++++++++++++++++++++- UICatalog/Scenarios/TimeAndDate.cs | 28 ++++++++++++-------------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/Terminal.Gui/Views/DateField.cs b/Terminal.Gui/Views/DateField.cs index 46e9c1231..bbc4accd4 100644 --- a/Terminal.Gui/Views/DateField.cs +++ b/Terminal.Gui/Views/DateField.cs @@ -61,10 +61,15 @@ namespace Terminal.Gui { void DateField_Changed (object sender, ustring e) { - if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) + if (!DateTime.TryParseExact (GetDate (Text).ToString(), GetInvarianteFormat (), CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) Text = e; } + string GetInvarianteFormat () + { + return $"MM{sepChar}dd{sepChar}yyyy"; + } + string GetLongFormat (string lf) { ustring [] frm = ustring.Make (lf).Split (ustring.Make (sepChar)); @@ -191,6 +196,31 @@ namespace Terminal.Gui { return date; } + ustring GetDate (ustring text) + { + ustring [] vals = text.Split (ustring.Make (sepChar)); + ustring [] frm = ustring.Make (Format).Split (ustring.Make (sepChar)); + ustring [] date = { null, null, null }; + + for (int i = 0; i < frm.Length; i++) { + if (frm [i].Contains ("M")) { + date [0] = vals [i].TrimSpace (); + } else if (frm [i].Contains ("d")) { + date [1] = vals [i].TrimSpace (); + } else { + var year = vals [i].TrimSpace (); + if (year.Length == 2) { + var y = DateTime.Now.Year.ToString (); + date [2] = y.Substring (0, 2) + year.ToString (); + } else { + date [2] = vals [i].TrimSpace (); + } + } + } + return date [0] + ustring.Make (sepChar) + date [1] + ustring.Make (sepChar) + date [2]; + + } + int GetFormatIndex (ustring [] fm, string t) { int idx = -1; diff --git a/UICatalog/Scenarios/TimeAndDate.cs b/UICatalog/Scenarios/TimeAndDate.cs index ebbf32333..d24aab005 100644 --- a/UICatalog/Scenarios/TimeAndDate.cs +++ b/UICatalog/Scenarios/TimeAndDate.cs @@ -8,36 +8,34 @@ namespace UICatalog { class TimeAndDate : Scenario { public override void Setup () { - // NOTE: The TimeField control is not ready for prime-time. See #246 - - var longTime = new TimeField (0, 0, DateTime.Now, isShort: false) { - // BUGBUG: TimeField does not support Computed Layout + var longTime = new TimeField (DateTime.Now) { X = Pos.Center (), Y = 2, + IsShortFormat = false, ReadOnly = false, }; Win.Add (longTime); - var shortTime = new TimeField (0, 2, DateTime.Now, isShort: true) { - // BUGBUG: TimeField does not support Computed Layout + var shortTime = new TimeField (DateTime.Now) { X = Pos.Center (), Y = Pos.Bottom(longTime) + 1, - ReadOnly = true, + IsShortFormat = true, + ReadOnly = false, }; Win.Add (shortTime); - var shortDate = new DateField (0, 2, DateTime.Now, isShort: true) { - // BUGBUG: TimeField does not support Computed Layout + var shortDate = new DateField (DateTime.Now) { X = Pos.Center (), Y = Pos.Bottom (shortTime) + 1, + IsShortFormat = true, ReadOnly = true, }; Win.Add (shortDate); - var longDate = new TimeField (0, 2, DateTime.Now, isShort: true) { - // BUGBUG: TimeField does not support Computed Layout + var longDate = new DateField (DateTime.Now) { X = Pos.Center (), Y = Pos.Bottom (shortDate) + 1, + IsShortFormat = false, ReadOnly = true, }; Win.Add (longDate); @@ -49,14 +47,14 @@ namespace UICatalog { longTime.ReadOnly = !longTime.ReadOnly; shortTime.ReadOnly = !shortTime.ReadOnly; - //longTime.IsShortFormat = !longTime.IsShortFormat; - //shortTime.IsShortFormat = !shortTime.IsShortFormat; + longTime.IsShortFormat = !longTime.IsShortFormat; + shortTime.IsShortFormat = !shortTime.IsShortFormat; longDate.ReadOnly = !longDate.ReadOnly; shortDate.ReadOnly = !shortDate.ReadOnly; - //longDate.IsShortFormat = !longDate.IsShortFormat; - //shortDate.IsShortFormat = !shortDate.IsShortFormat; + longDate.IsShortFormat = !longDate.IsShortFormat; + shortDate.IsShortFormat = !shortDate.IsShortFormat; } }); }