Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to represent partial dates in Noda Time?

I'm attempting to represent a company's founded date in C#. I occasionally have a full day, month, and year, but more often I only have a month and year, or sometimes a year alone.

Is there a way I can represent this in NodaTime, in such a way as to then also allow comparison between them? i.e. 2012 < June 2013 == true, but 2013 < June 2013 == false because it is the same year.

I also need to be able to unambiguously retrieve the stored data. What I mean by unambiguously is that I cannot store 2013 as 1/1/2013 because I would not be able to retrieve the information that I only know the date to be 2013.

like image 360
David Pfeffer Avatar asked Oct 21 '22 08:10

David Pfeffer


1 Answers

To answer the question as asked: no, there is no Noda Time type that you can use to directly represent the concept of "year" or "year and month".

(Well, for completeness, I should note that you could probably use Period, but I think it would be pretty confusing to use a calendrical duration to represent a date, and somewhat of an abuse of what Period is meant to be used for.)

Were I to do this myself, I'd probably create a type that aggregated a Noda Time LocalDate (storing "year 2013" as 2013-01-01) together with an enum that recorded which parts of the date were valid (i.e. Year, YearMonth, or YearMonthDay), and then implement IComparer<MyPartialLocalDate> to order the dates as-needed (by delegating to the LocalDate, then using the enum to break ties, probably).

like image 151
Malcolm Rowe Avatar answered Oct 23 '22 10:10

Malcolm Rowe