tags:

views:

53

answers:

2

DateTimes are usually some number of ticks from some starting date. These are usually displayed by some type of tool such as a calendar or the like. It seems to me that sometimes an application would like to store a DateTime with a special meaning, like "When evaluated" or "Never" or "Unknown".

For example I worked in a company where we ran Oracle ERP and we had orders that could have an end date. For orders with no end date, the company had chosen to make the end date 1/1/2020, where 1/1/2020 was a "flag" to users and reports.

I think that the only way to programmatically handle this situation is to have a second field that represents the DateTime "type' but since this is not generally agreed upon, manipulating the DateTime will require custom code. It would be nice if there were generally agreed upon special meanings to certain binary values.

Is there an existing convention for this or is this common functionality that I am unaware of?

I am thinking of the IEEE floating point format where certain binary values represent Infinity, NaN( Not A Number) and SQRT(-1)

+2  A: 

You could use a Nullable<DateTime> (documentation) to allow null values. The shorthand for this type is DateTime?.

If you need to distinguish between more states than defined/undefined, you could apply the State design pattern, but you would need to implement the states yourself.

Polymorphism in general is a good approach to addressing such issues, because you can encapsulate the special handling inside the different sub-types. This means that you wouldn't have to write a lot of special-case handling in the consuming code.

Mark Seemann
A: 

I would think that most companies come up with an arbitrary number to represent this. The main thing to keep in mind is the constraints of your language, database, etc. For instance, in .NET you could use, say, 1/1/1000 or 1/1/1500 to represent "from forever until now", but if you store your data in SQL Server, those dates won't work because it only lets you use dates back to 1753.

Whatever date you pick that's consistent with the limitations of your system, just make sure you put the constant in a place where it's easily accessible from everywhere in your project.

Kyralessa