实施"部分日期和QUOT;目的 [英] Implementing a "Partial Date" object
问题描述
我们有一个要求部分日期的对象这将允许您指定的日期,如
新PartialDate(1,NULL,NULL); //第一个月
新PartialDate(1,2,空)的; //首先二月
新PartialDate(NULL,2,空)的; //二月
用例是有关的事件。例如,你可能有一个过程发生每个1月,在这种情况下,你不想要或需要以当日指定对象的年份部分。
我们需要能够通过这些日期周围的一些任意性的规则基础进行排序,因此,我的思想来实现数据类型(将实施 IComparable的< PartialDate>
,然而,这是外。我的问题的范围)
我有几个问题:
- 这是一个坏主意?
- 我使用.NET的,有框架(或第三方库),将提供此功能? $ b的某些部分$ b
本身将是相当简单的类,仅仅有对日其它的组件3可空的整数。 (你甚至可以使一个结构,如果你觉得如何正确地实现结构读了。)
是什么使得它有点复杂正在处理的特殊情况,如:
新PartialDate(29,2,NULL); //只在闰年出现
新PartialDate(31,NULL,NULL); //发生在一年
只7个月
(另外,看看参数顺序的年,月,日
的DateTime 结构用>,你可能想复制这种模式,而不是发明新的。)
到一个类的另一种可能是一个基类为seprate种日期的具体实现:
新MonthlyPartialDate(1); //第一每月
新YearlyParticalDate(2,1); //第一二月
新YearlyPartialDate(2); //二月
We have a requirement for a "Partial Date" object which would allow you to specify dates such as
new PartialDate(1, null, null); // first of the month
new PartialDate(1, 2, null); // first of the February
new PartialDate(null, 2, null); // February
The use case is in relation to events. For example, you might have a course which takes place every January, in which case you don't want or need to specify the year component of the date object.
We need to be able to sort by these dates based around some arbitrary rules, hence my thought to implement a data type (which will implement IComparable<PartialDate>
, however this is outside the scope of my question).
I have a couple of Questions:
- Is this a bad idea?
- I'm working in .NET, Is there some part of the framework (or 3rd party library) which will provide this functionality?
The class itself would be fairly simple, just having three nullable integers for the date componenets. (You could even make it a structure, if you feel like reading up on how to implement a structure correctly.)
What makes it a bit complicated is handling the special cases, like:
new PartialDate(29, 2, null); // occurs only on leap years
new PartialDate(31, null, null); // occurs only seven months in a year
(Also, have a look at the parameter order year,month,day
used in the DateTime
structure, you might want to replicate that pattern instead of inventing a new one.)
An alternative to a single class could be a base class with specific implementations for seprate kinds of dates:
new MonthlyPartialDate(1); // the first every month
new YearlyParticalDate(2, 1); // the first of february
new YearlyPartialDate(2); // february
这篇关于实施"部分日期和QUOT;目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!