5

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.

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
  • Are you planning on using your comparisons to sort? – Rawling May 15 '14 at 12:04
  • @Rawling Yes; I would imagine it would be like alphabetical ordering. Just as A comes before AB, 2013 would come before 2013 June. – David Pfeffer May 15 '14 at 12:05
  • @Rawling Perhaps the better example would be increasing levels of significant figures, since that's sort of what this is: 0.1 < 0.15 even though 0.1 might just be the less precise representation of 0.18. – David Pfeffer May 15 '14 at 12:08
  • 1
    Have a look at [this](http://ericlippert.com/2011/01/20/bad-comparisons-part-one/) then; it talks about sorting when you don't have a complete ordering function. (But yes, putting e.g. `2013` as just before `2013-01-01` would give you an order.) (But then `2013 < June 2013` would be *true*, not *false*.) – Rawling May 15 '14 at 12:08
  • 2
    @Rawling I don't even know how to represent the data though except using my own class at the moment. I'd prefer to use something standard. Undoubtedly, as seems to happen with every time related question I have, Jon Skeet will tell me I have forgotten some key thing that makes the whole question 10x more complicated. :) – David Pfeffer May 15 '14 at 12:10
  • Comparing dissimilar types is not usually a good idea. For example, would you sort these values as `[2012, 2013, June 2013, 2014]` or would you sort it as `[2012, June 2013, 2013, 2014]`. Neither really makes sense. Can you give us any more information about what these dissimilar values belong to? Context is usually the key to solving these kind of problems. – Matt Johnson-Pint May 16 '14 at 05:11
  • @MattJohnson Just like with increasing levels of precision or alphabetical order... `[2012, 2013, June 2013, 2014]` You have the entire context -- founding dates of companies with as much specificity as is available. – David Pfeffer May 17 '14 at 23:17
  • 1
    Opened [issue](https://github.com/nodatime/nodatime/issues/1503). – Shimmy Weitzhandler Apr 12 '20 at 01:20

1 Answers1

3

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).

Malcolm Rowe
  • 747
  • 3
  • 14