【发布时间】:2015-02-27 21:52:53
【问题描述】:
标题有点古怪,但这就是问题所在。我正在使用 C#。我正在尝试提出几种 DateTime 扩展方法。在思考的同时,我想知道我编写如下代码需要什么语法:
DateTime comparisonDate = DateTime.Now.AddMonths(-3);
if( DateTime.Now.IsWithIn(3).Of(comparisonDate) ) ....
我以前写过扩展方法,但我不确定如何编写这样的东西。 “IsWithIn”将是一种方法......但这会返回一个表达式,而“Of”方法将是 Expression 类的扩展方法吗?
编辑 1
还在想这个。我想知道这种方法虽然可读,但是否过于复杂。我的第一个修订版只是将@Wai Ha Lee 的内容调整为 DateTimeExtensions 类。我将重构它并继续迭代。这尖叫策略模式,但我还没有适应它。现在也没有“Of”方法,方法名称似乎有意义……至少在今天。一个月后?我不确定。
我还想到了另一种编写此代码的方法。我想我还在做梦,但就是这样:
date.IsWithIn(1).Days().Of(comparisonDate);
date.IsWithIn(1).Months().Of(comparisonDate);
date.IsWithIn(1).Years().Of(comparisonDate);
但除此之外,这是我的修订版,它只是一个没有方法名称链接的 DateTime 扩展。
public class Program
{
static void Main(string[] args)
{
DateTime now = DateTime.Now;
DateTime past = new DateTime(2015, 1, 15);
DateTime future = new DateTime(2015, 3, 15);
DateTime comparison = now.AddDays(-2);
int interval = 1;
DateInterval di = DateInterval.Days;
Console.WriteLine(
string.Format("Now, {0}, is with in {1} {2} of {3} is {4}",
now.ToShortDateString(),
interval.ToString(),
di.ToString(),
comparison.ToShortDateString(),
now.IsDateWithinXRangeOfAnotherDate(interval, di, comparison).ToString())
);
Console.ReadLine();
}
}
public enum DateInterval
{
Days,
Months,
Years
}
public static class DateTimeExtensions
{
public static bool IsDateWithinXRangeOfAnotherDate(this DateTime date, int interval, DateInterval dateInterval, DateTime comparisonDate)
{
DateTime _min = comparisonDate;
DateTime _max = comparisonDate;
switch(dateInterval)
{
case DateInterval.Days:
_min = _min.AddDays(-interval);
_max = _max.AddDays(interval);
Console.WriteLine(
string.Format("Min Date is {0} Max Date is {1}",
_min.ToShortDateString(),
_max.ToShortDateString()));
break;
case DateInterval.Months:
_min = _min.AddMonths(-interval);
_max = _max.AddMonths(interval);
Console.WriteLine(
string.Format("Min Date is {0} Max Date is {1}",
_min.ToShortDateString(),
_max.ToShortDateString()));
break;
case DateInterval.Years:
_min = _min.AddYears(-interval);
_max = _max.AddYears(interval);
Console.WriteLine(
string.Format("Min Date is {0} Max Date is {1}",
_min.ToShortDateString(),
_max.ToShortDateString()));
break;
}
return _min <= date && date <= _max;
}
}
编辑 2
修订:
date.IsWithIn(1).Days().Of(comparisonDate);
date.IsWithIn(1).Months().Of(comparisonDate);
date.IsWithIn(1).Years().Of(comparisonDate);
到
date.IsWithIn(1.Days()).Of(comparisonDate);
date.IsWithIn(1.Months()).Of(comparisonDate);
date.IsWithIn(1.Years()).Of(comparisonDate);
看了一些 FluentTime 之后,我注意到作者使用了几个我什至不知道存在的方法和类。一方面,他使用了 TimeSpan.FromDays 方法。他可能重载了 + 符号,因为在代码的另一点,他只是将时间跨度添加到日期。鉴于 TimeSpan 的工作方式,我可能只能实现 1.Days() 部分......我认为这就是我真正需要的。
我会一直玩弄这一切,直到我弄明白为止。我可以只使用 FluentTime 库,但是对于我需要它的东西来说,它是矫枉过正的,因为库也处理时间。我对日期范围比较非常感兴趣。 After()、Before()、IsBetween()、IsWithIn 等方法。我已经实现了前 3 个。这个问题的重点是回答最后一个问题。
编辑 3 - 已解决!
这个问题更像是一个代码练习而不是实用性。最终,Jon Skeet 关于必须创建自定义类型是正确的。解决方案分解为以下摘要:
自定义类:FluentDateTime 已创建 3 个 int 扩展方法 - 天、月、年。这些每个都返回一个 FluentDateTime 类。 1 DateTime 扩展方法 - IsWithIn 采用 FluentDateTime 参数
我想强调的是,这是一笔不小的费用……但是,无论如何,这里是代码。
public class FluentDateTime
{
public enum DateInterval
{
Days,
Months,
Years
}
private DateTime _lowDate;
private DateTime _highDate;
public DateTime BaseDate { get; set; }
public DateInterval Interval { get; set; }
public int Increment { get; set; }
public bool Of(DateTime dt)
{
_lowDate = dt;
_highDate = dt;
if(this.Interval == DateInterval.Days)
{
_lowDate = _lowDate.AddDays(-this.Increment);
_highDate = _highDate.AddDays(this.Increment);
}
else if (this.Interval == DateInterval.Months)
{
_lowDate = _lowDate.AddMonths(-this.Increment);
_highDate = _highDate.AddMonths(this.Increment);
}
else
{
_lowDate = _lowDate.AddYears(-this.Increment);
_highDate = _highDate.AddYears(this.Increment);
}
Console.WriteLine(
string.Format("{0} <= {1} <= {2}", _lowDate.ToShortDateString(), BaseDate.ToShortDateString(), _highDate.ToShortDateString()
));
return (_lowDate < BaseDate && BaseDate < _highDate) || (_lowDate.Equals(BaseDate) || _highDate.Equals(BaseDate) );
}
}
// 日期时间扩展
public static FluentDateTime IsWithIn(this DateTime date, FluentDateTime fdtParams)
{
fdtParams.BaseDate = date;
return fdtParams;
}
//INT 扩展
public static FluentDateTime Days(this int inc)
{
FluentDateTime fdt = new FluentDateTime();
fdt.Interval = FluentDateTime.DateInterval.Days;
fdt.Increment = inc;
return fdt;
}
public static FluentDateTime Months(this int inc)
{
FluentDateTime fdt = new FluentDateTime();
fdt.Interval = FluentDateTime.DateInterval.Months;
fdt.Increment = inc;
return fdt;
}
public static FluentDateTime Years(this int inc)
{
FluentDateTime fdt = new FluentDateTime();
fdt.Interval = FluentDateTime.DateInterval.Years;
fdt.Increment = inc;
return fdt;
}
//测试程序
DateTime testDate1 = new DateTime(2015, 3, 3);
DateTime testDate2 = new DateTime(2015, 3, 4);
Console.WriteLine(
string.Format("{0} is within 5 days of {1}? {2} (should be true)",
testDate1.ToShortDateString(), testDate2.ToShortDateString(), testDate1.IsWithIn(5.Days()).Of(testDate2)
));
testDate1 = new DateTime(2015, 3, 1);
testDate2 = new DateTime(2015, 3, 7);
Console.WriteLine(
string.Format("{0} is within 3 days of {1}? {2} (should be false)",
testDate1.ToShortDateString(), testDate2.ToShortDateString(), testDate1.IsWithIn(3.Days()).Of(testDate2)
));
testDate1 = new DateTime(2015, 3, 3);
testDate2 = new DateTime(2015, 4, 1);
Console.WriteLine(
string.Format("{0} is within 1 month of {1}? {2} (should be true)",
testDate1.ToShortDateString(), testDate2.ToShortDateString(), testDate1.IsWithIn(1.Months()).Of(testDate2)
));
testDate1 = new DateTime(2015, 3, 3);
testDate2 = new DateTime(2015, 6, 1);
Console.WriteLine(
string.Format("{0} is within 2 month of {1}? {2} (should be false)",
testDate1.ToShortDateString(), testDate2.ToShortDateString(), testDate1.IsWithIn(2.Months()).Of(testDate2)
));
【问题讨论】:
-
是的,您返回具有后续方法的自定义类型。
-
您可以检查现有框架作为示例 - github.com/duelinmarkers/FluentTime
-
如何使用扩展方法即时生成
TimeSpan,以便您可以键入3.Months()并让IsWithin接受时间跨度作为参数? -
@mbx 我认为这是一个明智的想法,尽管我想您仍然有不满意的属性 date.IsWithin(3.Months()) 是一个具有荒谬结果的有效表达式(直到您添加部分,我的意思是)。
-
@mbx - 正如@emodendroket 和@ScottChamberlain 指出的那样,没有参考点,3 个月是未定义的。我的方法基于参考点计算范围,以便明确定义
x个月。
标签: c# datetime lambda extension-methods