From 54944e1a671cd66afca54e71d8ddd5fa12834d58 Mon Sep 17 00:00:00 2001 From: Elivo Date: Thu, 24 Apr 2025 16:49:49 +0800 Subject: [PATCH] =?UTF-8?q?ClockUtility=EF=BC=9A=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=88=86=E7=A6=BB=E5=8F=AF=E9=80=89=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=8F=90=E4=BE=9B=E5=87=BD=E6=95=B0=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=EF=BC=8C=E6=8C=87=E5=AE=9A=20failed=20=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=97=B6=E5=B0=86=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=20DateTime=20=E7=B1=BB=E5=9E=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/ClockUtility.cs | 159 +++++++++++++++++++++++++---------------- 1 file changed, 97 insertions(+), 62 deletions(-) diff --git a/Apewer/ClockUtility.cs b/Apewer/ClockUtility.cs index 9dcde54..a5d5c9f 100644 --- a/Apewer/ClockUtility.cs +++ b/Apewer/ClockUtility.cs @@ -11,7 +11,7 @@ namespace Apewer public static class ClockUtility { - #region DateTime + #region To DateTime /// 尝试转换内容为 DateTime 实例。 public static Class DateTime(object value) @@ -22,8 +22,7 @@ namespace Apewer try { var text = value.ToString(); - var parsed = System.DateTime.TryParse(text, out var result); - return parsed ? new Class(result) : null; + return Parse(text); } catch { @@ -31,6 +30,27 @@ namespace Apewer } } + /// 尝试转换内容为 DateTime 实例。 + /// + public static DateTime DateTime(object value, Func failed) + { + if (failed == null) throw new ArgumentNullException(nameof(failed)); + + var parsed = DateTime(value); + if (parsed != null) return parsed.Value; + + return failed.Invoke(); + } + + /// 尝试转换内容为 DateTime 实例。 + public static DateTime DateTime(object value, DateTime failed) + { + var parsed = DateTime(value); + if (parsed != null) return parsed.Value; + + return failed; + } + #endregion #region Fixed @@ -211,40 +231,6 @@ namespace Apewer #endregion - #region Text - - /// 解析文本,获取 DateTime 对象。 - public static Class FromText(string text) - { - var str = text; - if (string.IsNullOrEmpty(str)) return null; - - var utc = false; - var lower = str.ToLower(); - if (lower.EndsWith(" utc")) - { - utc = true; - str = str.Substring(0, str.Length - 4); - } - - DateTime dt; - if (!TryParse(str, out dt)) - { - if (!str.Contains("-") && TryParseExact(str, "yyyy-M-d", null, DateTimeStyles.None, out dt)) - { - if (!str.Contains("/") && TryParseExact(str, "yyyy/M/d", null, DateTimeStyles.None, out dt)) - { - return null; - } - } - } - - if (utc) dt = new DateTime(dt.Ticks, DateTimeKind.Utc); - return new Class(dt); - } - - #endregion - #region Lucid & Compact /// 表示当前本地时间的文本,显示为易于阅读的格式。 @@ -358,68 +344,117 @@ namespace Apewer /// 解析文本。 /// 要被解析的文本。 - /// 解析失败时的获取方法。 - public static Nullable Parse(string text, Func failed = null) + public static Class Parse(string text) { if (string.IsNullOrEmpty(text)) return null; // 使用默认解析。 + if (TryParse(text, out DateTime system)) return new Class(system); + + // 尝试解析 Lucid 格式。 + var lucid = ParseLucid(text); + if (lucid != null) return lucid; + + return null; + } + + /// 解析文本,获取 DateTime 对象。 + static Class ParseExact(string text) + { + var str = text; + if (string.IsNullOrEmpty(str)) return null; + + var utc = false; + var lower = str.ToLower(); + if (lower.EndsWith(" utc")) { - if (TryParse(text, out DateTime dt)) return dt; + utc = true; + str = str.Substring(0, str.Length - 4); } - // 尝试解析 Lucid 格式。 - var byLucid = ParseLucid(text); - if (byLucid != null) return byLucid; + DateTime dt; + if (!TryParse(str, out dt)) + { + if (!str.Contains("-") && TryParseExact(str, "yyyy-M-d", null, DateTimeStyles.None, out dt)) + { + if (!str.Contains("/") && TryParseExact(str, "yyyy/M/d", null, DateTimeStyles.None, out dt)) + { + return null; + } + } + } - // 尝试 failed 回调。 - if (failed != null) return failed.Invoke(); + if (utc) dt = new DateTime(dt.Ticks, DateTimeKind.Utc); + return new Class(dt); + } - return null; + /// 解析文本。 + /// 要被解析的文本。 + /// 解析失败时的获取方法。 + /// + public static DateTime Parse(string text, Func failed) + { + if (failed == null) throw new ArgumentNullException(nameof(failed)); + + var parsed = Parse(text); + if (parsed != null) return parsed.Value; + + return failed.Invoke(); + } + + /// 解析文本。 + /// 要被解析的文本。 + /// 解析失败时的默认值。 + /// + public static DateTime Parse(string text, DateTime failed) + { + var parsed = Parse(text); + if (parsed != null) return parsed.Value; + + return failed; } /// 解析文本。 - public static Nullable ParseLucid(string lucid) + static Class ParseLucid(string lucid) { - var failed = null as Nullable; - if (lucid.IsEmpty()) return failed; + if (lucid.IsEmpty()) return null; int year = 0, month = 0, day = 0; - if (lucid.Length < 10) return failed; - if (lucid[4] != '-' || lucid[7] != '-') return failed; + if (lucid.Length < 10) return null; + if (lucid[4] != '-' || lucid[7] != '-') return null; year = NumberUtility.Int32(lucid.Substring(0, 4)); month = NumberUtility.Int32(lucid.Substring(5, 2)); day = NumberUtility.Int32(lucid.Substring(8, 2)); - if (year < 1) return failed; - if (month < 1 || month > 12) return failed; - if (day < 1 || day > DaysInMonth(year, month)) return failed; + if (year < 1) return null; + if (month < 1 || month > 12) return null; + if (day < 1 || day > DaysInMonth(year, month)) return null; int hour = 0, minute = 0, second = 0, milli = 0; if (lucid.Length >= 16) { - if (lucid[10] != ' ' || lucid[13] != ':') return failed; + if (lucid[10] != ' ' || lucid[13] != ':') return null; hour = NumberUtility.Int32(lucid.Substring(11, 2)); minute = NumberUtility.Int32(lucid.Substring(14, 2)); - if (hour < 0 || hour > 23) return failed; - if (minute < 0 || minute > 59) return failed; + if (hour < 0 || hour > 23) return null; + if (minute < 0 || minute > 59) return null; if (lucid.Length >= 19) { - if (lucid[16] != ':') return failed; + if (lucid[16] != ':') return null; second = NumberUtility.Int32(lucid.Substring(17, 2)); - if (second < 0 || second > 59) return failed; + if (second < 0 || second > 59) return null; if (lucid.Length >= 23) { - if (lucid[19] != '.') return failed; + if (lucid[19] != '.') return null; milli = NumberUtility.Int32(lucid.Substring(20, 3)); - if (milli < 0 || milli > 999) return failed; + if (milli < 0 || milli > 999) return null; } } } var entity = new DateTime(year, month, day, hour, minute, second, milli); - return new Nullable(entity); + return new Class(entity); } #endregion