In Software we often deal with ranges of dates and times.
Let us look at it from the perspective of an end user.
When we say something like „from 2020-03-07 to 2019-03-10“ we mean the set of all timestamps such that
or more accurately:
Important is, that we mean to include the whole 24 hour day of 2019-03-10. Btw. please try to get used to the ISO-date even when writing normal human readable texts, it just makes sense…
Now when we are not talking about dates, but about times or instants of time, the interpretation is different.
When we say sonmething like „from 07:00 to 10:00“ or „from 2020-03-10T07:00:00+TZ to 2020-04-11T09:00:00+TZ“, we actually mean the set of all timestamps such that
respectively. It is important that we have to add one in case of date only (accuracy to one day) and we do not in case of finer grained date/time information. The question if the upper bound is included or not is not so important in our everyday life, but it proves that commonly the most useful way is not to include the upper bound. If you prefer to have all options, it is a better idea to employ an interval library, i.e. to find one or to write one. But for most cases it is enough to exclude the upper limit. This guarantees disjoint adjacent intervals which is usually what we want. I have seen people write code that adds 23:59:59.999 to a date and compares with instead of , but this is an ugly hack that needs a lot of boiler plate code and a lot of time to understand. Use the exclusive upper limit, because we have it.
Now the requirement is to add one day to the upper limit to get from the human readable form of date-only ranges to something computers can work with. It is a good thing to agree on where this transformation is made. And to do it in such a way that it even behaves correctly on those dates where daylight saving starts or ends, because adding one day might actually mean „23 hours“ or „25 hours“. If we need to be really very accurate, sometimes switch seconds need to be added.
Just another issue has come up here. Local time is much harder than UTC. We need to work with local time on all kinds of user interfaces for humans, with very few exceptions like for pilots, who actually work with UTC. But local date and time is ambiguous for one hour every year and at least a bit special to handle for these two days where daylight saving starts and ends. Convert dates to UTC and work with that internally. And convert them to local date on all kinds of user interfaces, where it makes sense, including documents that are printed or provided as PDFs, for example. When we work with dates without time, we need to add one day to the upper limit and then round it to the nearest for our timezone or know when to add 23, 24 or 25 hours, respectively, which we do not want to know, but we need to use modern time libraries like the
java.time.XXX stuff in Java, for example.
Working with date and time is hard. It is important to avoid making it harder than it needs to be. Here some recommendations:
- Try to use UTC for the internal use of the software as much as possible
- Use local date or time or date and time in all kinds of user interfaces (with few exceptions)
- add one day to the upper limit and round it to the nearest midnight of local time exactly once in the stack
- exclude the upper limit in date ranges
- Use ISO-date formats even in the user interfaces, if possible
Schreibe einen Kommentar