Add duration type
We have datetime, but no duration type.
We should add one that serializes using the ISO standard that Java uses (e.g. PT1s).
I've already come across a use case where people just use longs because nothing else is convenient, and we all know that's just a bug waiting to happen.
I know there have been some previous discussions on this topic, so wanted to address the counter arguments:
- the set of built-ins should be small
- we can create some sort of separate datetime extension that one can import, rather than add it as a built-in
- however, datetime is already a built-in and I view these to be in the same category. So we should either move both to some language extension, or have both be part of the built-ins
- APIs can name their variables "durationMs" or "durationSec"
- This is better, but people won't always do that
- There is still the risk that an incorrect conversion will be overlooked, if the duration value ends up being passed as-is to other functions such that the unit information is lost
- What if
durationSeclater needs to support a higher resolution? We might have to break the API to change its unit, or resort to using double (ugh!)
I think the language-specific APIs should allow an as-seamless-as-possible transition of a data type across the client/server boundary.
Therefore, things like .durationMillis(myDuration.toMillis()) and an equivalent Duration.ofMillis(foo.durationMillis()) should be unnecessary, we are sending a duration which should be unit-agnostic.