Skip to content

Consider formatting negative Duration using a prefixed single '-' sign #121

@BastienClement

Description

@BastienClement

Currently, a Duration of minus [2 hours and 21 minutes] is formatted as "PT-2H-21M".
Another equivalent (and I would argue better) form would be "-PT2H21M".

Firstly, negative durations are not (I believe) part of the ISO 8601-1 standard.

The Java 8 Duration.parse method gives examples of valid input where both forms would be accepted but specifies

The leading plus/minus sign, and negative values for other units are not part of the ISO-8601 standard.1

Negative durations seem to have been introduced in ISO 8601-2:2019, an extension to the original standard.

From my understanding, this version of the standard introduces the concept of a "negative duration", which is simply a "positive duration" prefixed with a single - sign (section 4.4.1.92). However, at the same time, they also introduced a "composite representation" which indeed encompasses the current brick/date-time representation PT-2H-21M (sections 11.2 and 11.3.22).

I also found a thread from the Ecma TC39 (JavaScript) "Temporal API" design group discussing the changes from ISO 8601-2:2019 and the impact on the proposal. This made me look at the Temporal.Duration documentation from MDN to find out their up-to-date conclusions.

There, we can see that their definition of the ISO 8061 format for Duration is ±P nY nM nW nD T nH nM nS3.
And surely, this is reflected in the Firefox JS console:

> Temporal.Duration.from("-PT2H21M")
< Temporal.Duration -PT2H21M
> Temporal.Duration.from("PT-2H-21M")
Uncaught RangeError: can't parse duration: missing duration digits

So, while the current representation PT-2H-21M seems valid as an ISO 8601-2:2019 "composite representation", it looks like the standard encourages instead the -PT2H21M format (simply "negative duration").

And anyway, while Java seems happy with either, Javascript Temporal (still experimental) only accepts -PT2H21M. I believe JS compatibility for the output of a PHP library to be an important factor to consider.

We would be happy to submit a patch to Duration::toISOString(). What do you think?

Footnotes

  1. https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-

  2. https://www.postgresql.org/message-id/9q0ftb37dv7.fsf%40gmx.us 2

  3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration#iso_8601_duration_format

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions