Duration: support negative durations by prefixing a '-' before the P in ISO format#111
Duration: support negative durations by prefixing a '-' before the P in ISO format#111apoelstra wants to merge 4 commits intoGothenburgBitFactory:masterfrom
Conversation
…in ISO format According to [this MDN document](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration) there is an ECMAScript extension to ISO 8601 to allow signed durations by putting a + or - before the ISO duration format. Internally we support signed durations -- we will parse "-60w" or "-60min", which we store in a `time_t` (whose signedness is technically implementation defined, but which is signed on all major compilers). However, when formatting these as ISO 8601 we get get garbed output like PT-1H-56M-9S, which we cannot parse and probably neither can anything else. (Taskwarrior, when asked to assign a negative duration to a duration-typed UDA, will store this garbled output but then reproduce it as PT0S, an unfortunate user experience.) This PR updates `Duration::formatISO` to instead prepend a '-' before negative durations, and updates `Duration::parse_designated` to parse such things. Alternative to GothenburgBitFactory#110, which simply blesses the garbled format by extending the parser to support it.
This will let us remove some special-case logic from Timewarrior.
2189501 to
b76e66e
Compare
|
@apoelstra would be great if you could also provide test coverage for this. Otherwise, given that this has been lying around for some time (sorry about that), I will take over from here and provide it myself. |
|
For sure! I am a bit busy for the next week or two -- feel free to add test coverage if you've got a moment. Otherwise I will do it, I just may be slow. No worries about not getting to this for a little while. It's a busy life being an open-source maintainer. |
Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
|
@apoelstra I added test coverage (and adapted the code style a bit) and while at it encountered the following "problem": For e.g. I would say for ISO (e.g. But how would you interpret something like In general I would say a duration is always an positive value. The minus sign simply indicates direction or that it is "missing". I am currently investigating the impact on this on Timewarrior, maybe you could have a look at Taskwarrior and give me your impression. |
According to this MDN document there is an ECMAScript extension to ISO 8601 to allow signed durations by putting a + or - before the ISO duration format.
Internally we support signed durations -- we will parse "-60w" or "-60min", which we store in a
time_t(whose signedness is technically implementation defined, but which is signed on all major compilers). However, when formatting these as ISO 8601 we get get garbed output like PT-1H-56M-9S, which we cannot parse and probably neither can anything else. (Taskwarrior, when asked to assign a negative duration to a duration-typed UDA, will store this garbled output but then reproduce it as PT0S, an unfortunate user experience.)This PR updates
Duration::formatISOto instead prepend a '-' before negative durations, and updatesDuration::parse_designatedto parse such things.Alternative to #110, which simply blesses the garbled format by extending the parser to support it.