Skip to content

Draft to handle negative time scale units #12

@ronaldtse

Description

@ronaldtse

As discussed in tc39/proposal-temporal#819 , CC 18011 did not provide a mechanism for resolving negative time scale units, and as a result ISO 8601-2:2019 lacked this content as well.

Given there is now a need for this, we should update both CC 18011 and ISO 8601-2:2019. I've written up the following content to be added to both (using 8601-2 numbering).

Ping @CalConnect/tc-datetime : let's discuss!


(insert) ISO 8601-2:2019, 14.4

In date-time arithmetic, some time scale units can be converted into one another through deterministic relationships, which are transitive. These freely convertible relationships include:

  • century and decade (a calendar century contains 10 calendar decades)
  • decade and year (a calendar decade contains 10 calendar years)
  • year and week (a calendar year contains 52 calendar weeks)
  • year and month (a calendar year contains 12 calendar months)
  • week and day (a calendar week contains 7 calendar days)
  • day and hour (a calendar day contains 24 clock hours)
  • hour and minute (a clock hour contains 60 clock minutes)

The following time scale unit conversions are not deterministic in abstract, the actual durations of these time scale units depend on contextual information:

  • year and day (a calendar year may contain 365 or 366 calendar days);
  • month and day (a calendar month may contain 28, 29, 30, 31 calendar days);
  • minute and second (a clock minute may contain 59, 60, 61 clock seconds, in UTC).

These rules are considered as the "time scale unit conversion boundaries".

The result of a date time arithmetic expression does not change with time scale unit conversion as long as the conversion boundaries are not violated.

When the full context of a date time expression is provided, these boundaries can be relaxed accordingly.

EXAMPLE 1 The date time expression '2023Y59O' can be freely converted into '2023Y2M28D', because the 59th ordinal day of a Gregorian year is always February 28th.

EXAMPLE 2 The date time expression '2020Y60O' can be freely converted into '2020Y2M29D', because the 60th ordinal day of the Gregorian year 2020 is February 29th.

EXAMPLE 3 The date time expression '2016Y12M31DT61S' can be freely converted into '2017Y1M1DT-1S', because a leap second was inserted into the last clock minute on 2016-12-31.

(add) ISO 8601-2, D.3.3 Borrow to resolve underflow in time scale components

The composite duration form was meant to delay the resolution of date time duration until application because some time scale units require contextual information.

In some cases, it is possible to convert a duration expression that contains a negative time scale component into an equivalent duration expression where the previously negative time scale component becomes positive.

This process of conversion involves two operations:

  • the "borrow" operation, the counterpart to the "carry-over" operation;
  • time scale unit conversion.

NOTE: If exact duration is not necessary or is considered too complex to calculate, one could simplify to omit some boundaries to adopt "nominal duration", such as ignoring the leap second (i.e. ignore rule 3) and treat 1 minute = 60 seconds.

EXAMPLE 4 The expression 'P1Y10M3D − P2Y5MT10M' results in 'P3Y15M3DT-10M'. The negative sign at '-10M' can be converted into a positive expression by "borrowing" from a time scale unit of a higher order. Since the "hour" time scale unit is 0, the next time scale unit to borrow from is the "day" unit. One "day" unit expands to 24H, resulting in 'P3Y15M2DT24H-10M'. One "hour" unit can expand into 60M, resulting in 'P3Y15M2DT23H(60-10)M', which ends up being 'P3Y15M2DT23H50M'.

EXAMPLE 5 'P1Y-10M3D + P2Y-5M' results in 'P-1Y-15M3D'. The negative sign at '-1Y' cannot be resolved because there is no higher order unit to borrow from. The negative sign at '-15M' can be resolved by borrowing from the "year" unit. In order to pad the "month" unit to a positive number, the minimum number of years to borrow from "year" is 2, resulting in 'P-3Y(24-15)M3D', which equals 'P-3Y9M3D'.

EXAMPLE 6 'PT1H60S - PT122M' results in 'PT1H-122M60S'. The negative sign at '-122M' can be resolved, because the conversion between "hour" and "minute" does not cross any conversion boundaries. In order to resolve the negative sign at '-122M', a total of 3 hours need to be borrowed, resulting in 'PT(1-3)H(180-122)M60S', which concludes as 'PT-2H58M60S'. This expression can also be represented as 'PT-62M60S', since "hour" and "minute" can be freely converted (-2H = -120M; -120M + 58M = -62M).

EXAMPLE 7 'PT5H120S - PT1M' results in 'PT5H-1M120S'. The negative sign at '-1M' can be resolved by borrowing from the "hour" unit without crossing conversion boundaries. The borrowing of 1 hour to minutes results in 'PT4H(60-1)M120S', which concludes as 'PT4H59M120S'. The '59M120S' portion cannot be simplified as the "minute to seconds" conversion requires contextual information.

(new) ISO 8601-2, D.4.6 Resolving negative time scale units in an expression ("underflows")

The steps are as follows:

  1. Starting at the rightmost side of the composite duration expression, process every negative durational unit one by one as follows:
    a) From the negative time scale component (the "underflowed component"), find in the expression a higher order time scale component that is freely convertible (i.e. does not violate conversion boundaries) and has a positive value. There could be multiple higher order units that can be freely convertible, such as minute <> hour and minute <> day.
    b) Borrow units from the higher order value until the current time scale is positive. Ensure the time scale components being modified are properly updated.
    c) Once the underflowed component becomes positive, or if there are no more freely convertible components (even if the underflowed component is still negative), continue with step 1 on the resulting duration expression.
  2. Once all durational units have been processed, the resulting date and time representation is complete.

EXAMPLE 1 Calculation of '2022Y2M2D - P1Y10M3D':

  • Calculate in accordance to Annex D, compute the intermediate expression '(2022-1)Y(2-10)M(2-3)D' which results in '2021Y-8M-1D'.
  • Begin resolving negative time scale units.
    • Process the first underflow component from the right: '-1D'.
    • The "day" unit does not have a freely convertible higher-order component in this expression.
    • Process the second durational unit from the right: '-8M'
    • The "month" unit can be freely convertible with the higher order component "year" in this expression.
    • The "month" unit borrows 1 year, which is equal to 12 months. The partial expression under consideration
      of '2021Y-8M' becomes '2020Y(12-8)M', which resolves to '2020Y4M'.
    • The intermediate expression becomes '2020Y4M-1D'.
    • There are no more underflow components.
  • The resulting date and time expression after underflow resolution is '2020Y4M-1D'.

EXAMPLE 2 Calculation of '2025Y59O - P20DT1H30M':

  • Notice that the former and latter expressions use 'O' and 'D' to express the same time scale unit of calendar day respectively. This means that their values can be calculated within the same time scale unit. To preserve the meaning of the "ordinal day" time scale component, we select 'O' as the component to perform calculations on.

  • Calculate in accordance to Annex D, compute the intermediate expression '2025Y(59-20)OT(-1)H(-30)M' which results in '2025Y39OT-1H-30M'.

  • Begin resolving negative time scale units.

    • Process the first underflow component from the right: '-30M'.
    • The "minute" unit can be freely convertible with the higher order component "hour" in this expression.
    • The "minute" unit borrows 1 hour, which is equal to 60 minutes. The partial expression under consideration
      of 'T-1H-30M' becomes 'T(-1-1)H(60-30)M', which resolves to 'T-2H30M'.
    • Process the second durational unit from the right: '-2H'
    • The "hour" unit can be freely convertible with the higher order component "day" in this expression.
    • The "hour" unit borrows 1 day, which is equal to 24 hours. The partial expression under consideration
      of '39OT-2H' becomes '(39-1)OT(24-2)H', which resolves to '38OT22H'.
    • The intermediate expression becomes '2025Y38OT22H'.
    • There are no more underflow components.
  • The resulting date and time expression after underflow resolution is '2025Y38OT22H'.

EXAMPLE 3 Calculation of 'T10H10M10S - PT5H30M20S':

  • Calculate in accordance to Annex D, compute the intermediate expression 'T(10-5)H(10-30)M(10-20)S' which results in 'T5H-20M-10S'.
  • Begin resolving negative time scale units.
    • Process the first underflow component from the right: '-10S'.
    • The "second" unit does not have a freely convertible higher-order component in this expression.
    • Process the second durational unit from the right: '-20M'
    • The "minute" unit can be freely convertible with the higher order component "hour" in this expression.
    • The "minute" unit borrows 1 hour, which is equal to 60 minutes. The partial expression under consideration
      of 'T5H-20M' becomes 'T(5-1)H(60-20)M', which resolves to 'T4H40M'.
    • The intermediate expression becomes 'T4H40M-10S'.
    • There are no more underflow components.
  • The resulting date and time expression after underflow resolution is 'T4H40M-10S'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions