From a0a4f98f61ab4368c8008175e006721b4d717391 Mon Sep 17 00:00:00 2001 From: Falcon <12919280+falconfly@user.noreply.gitee.com> Date: Fri, 14 Feb 2025 15:49:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Object.Between=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=92=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Test/ObjectExtendTest.cs | 14 ++++++++++- Falcon.SugarApi/DateTimeExtend.cs | 29 +++++++++++++++++++--- Falcon.SugarApi/ObjectExtend.cs | 31 ++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Falcon.SugarApi.Test/ObjectExtendTest.cs b/Falcon.SugarApi.Test/ObjectExtendTest.cs index a170769..3632678 100644 --- a/Falcon.SugarApi.Test/ObjectExtendTest.cs +++ b/Falcon.SugarApi.Test/ObjectExtendTest.cs @@ -137,7 +137,7 @@ namespace Falcon.SugarApi.Test var o1 = new object(); Assert.IsTrue(o1.In(o1)); - Assert.IsFalse(o1.In( new object(),new object())); + Assert.IsFalse(o1.In(new object(),new object())); var o2 = new ObjWithComparable { val = 1 }; var o22 = new ObjWithComparable { val = 1 }; @@ -145,6 +145,18 @@ namespace Falcon.SugarApi.Test Assert.IsTrue(o2.In(o22,o23)); } + /// + /// 应用动态比较的方式比较对象是否在另外两个对象区间。 + /// + [TestMethod] + public void ObjectBetweenTest() { + Assert.IsTrue(1.Between(0,2)); + Assert.IsFalse(3.Between(0,1)); + var now = DateTime.Now; + Assert.IsTrue(now.Between(now.AddSeconds(-1),now.AddSeconds(1))); + Assert.IsFalse(now.Between(now.AddSeconds(1),now.AddSeconds(2))); + } + } /// /// 扩展属性测试类 diff --git a/Falcon.SugarApi/DateTimeExtend.cs b/Falcon.SugarApi/DateTimeExtend.cs index 908d3dc..a0b764e 100644 --- a/Falcon.SugarApi/DateTimeExtend.cs +++ b/Falcon.SugarApi/DateTimeExtend.cs @@ -145,9 +145,32 @@ namespace Falcon.SugarApi /// 开始时间 /// 结束时间 /// True是,False否 - public static bool Between(this DateTime? dt,DateTime start,DateTime? end = null) { - end ??= DateTime.Now; - return dt > start && dt <= end; + //public static bool Between(this DateTime? dt,DateTime start,DateTime? end = null) { + // end ??= DateTime.Now; + // return dt > start && dt <= end; + //} + + /// + /// 时间是否处于某个时间段内 + /// + /// 要判断的时间 + /// 开始时间 + /// 结束时间 + /// True是,False否 + public static bool Between(this DateTime? dt,DateTime? start,DateTime? end) { + if(dt==null) { + return false; + } + if(start == null && end == null) { + throw new Exception("给定的时间范围无效"); + } + if(start == null) { + return dt < end; + } + if(end == null) { + return dt >= start; + } + return dt >= start && dt <= end; } } diff --git a/Falcon.SugarApi/ObjectExtend.cs b/Falcon.SugarApi/ObjectExtend.cs index 1b1e087..52b03c2 100644 --- a/Falcon.SugarApi/ObjectExtend.cs +++ b/Falcon.SugarApi/ObjectExtend.cs @@ -248,6 +248,37 @@ namespace Falcon.SugarApi /// 目标对象数组 /// 不在数组中返回True,否则False public static bool NotIn(this T obj,params T[] values) => !obj.In(values); + + /// + /// 比较值是否在给定的上下限之间。包含下限但不包含上限。 + /// + /// 值类型 + /// 比较对象 + /// 最小值 + /// 最大值 + /// 是否包含在最小值和最大值之间 + /// 给定的参数无法进行比较 + public static bool Between(this T? obj,T? min,T? max) { + if(obj == null) { + return false; + } + if(min == null && max == null) { + throw new Exception("min和max不能都为null"); + } + dynamic? c = obj, i = min, x = max; + try { + if(i == null) { + return c < x; + } + if(x == null) { + return c >= i; + } + return c >= i && c <= x; + } + catch(Exception ex) { + throw new Exception("给定的参数无法进行比较",ex); + } + } } ///