How do I accurately represent date ranges in NodaTime?

Goals

I have a list of LocalDate elements that represent sets of start dates and end dates.

I would like to be able to store date ranges so that I can perform operations on them as a set of overlapping or separate ranges, etc.

Questions

  • Does NodaTime provide some DateRange construct that I missed in the docs?
  • Am I thinking about it wrong? Is there a more natural / preferred way to achieve this that NodaTime allows?
  • Am I setting myself up for trouble trying to think of a date range using LocalDate for start and end dates?

I am completely new to NodaTime and suggest that this is a conceptual misunderstanding on my part.

Update : I noticed a similar question on this issue since 2009, but it seems to belong to another class of utilies; I assume that since then, NodaTime may have evolved to accommodate this situation.

+8
c # nodatime
source share
1 answer

Noda Time provides an Interval type for a range of Instant values, but does not provide range types for other types. Part of the reason for this is the nuance of how ranges are used for different types.

If I give you a whole series of moments, it is always regarded as a half-open interval. The start value is included, but the end value is excluded. People do this naturally every time we provide a time value, for example, when I say that an event runs from 1:00 to 2:00 , I mean that the event ended at 2:00, so 2:00 is excluded.

But with integer ranges of calendar dates, usually end dates are inclusive. To represent the entire month of January (as a range of LocalDate values), I will probably say from January 1 to January 31, and I will include the last day as a whole.

Perhaps we could add some additional range types to enforce these requirements, but we need to think about how important it is to have them in the API when you might just create them as needed. I am not saying that I am for or against, anyway, but that was probably something discussed in the Noda Time user group .

To answer your specific questions:

  • No, there is no predefined range class for local dates.
  • The only thing to consider is that calendar math is usually done using the Period class. For example, to determine how many days exist between two calendar dates:

     LocalDate ld1 = new LocalDate(2012, 1, 1); LocalDate ld2 = new LocalDate(2013, 12, 25); Period period = Period.Between(ld1, ld2, PeriodUnits.Days); long days = period.Days; 
  • No, there is nothing wrong with creating a local date range; there simply cannot be many advantages. You can do the same with two properties, StartDate and EndDate , on your own classes. Just be careful about the inclusiveness of the end dates, as well as the exclusivity that you will see with an interval or interval of time.

And finally you said:

... so that I can perform operations on them as a set of overlapping or separate ranges, etc.

You are probably looking for operations such as intersecting, concatenating, calculating spaces, sorting, etc. These and others are defined by the Library of the time period , but Noda Time currently has nothing of the kind. If someone was supposed to create it, maybe he should be in the companion library (maybe "NodaTime.Ranges"?). You probably wouldn’t want to pull it into the core, but you never know ...

If you end up using this time period library, make sure you know that it only works with DateTime and completely DateTimeKind . Therefore, in order to be productive with this, you should probably be sure that you only work with UTC values ​​or “undefined” calendar dates, and try not to ask him things like “how many hours a day”, because it it will be wrong for a few days with daylight saving time.

+12
source share

All Articles