Search code examples
text-formatting

Is there a standard way to represent a time duration, time-span or time-delta?


If we accept that there are various standard ways to represent times and dates, is there a standard way to present a time-span or time-delta? I would add the requirement that the presentation of the time-span should also be amenable to parsing.

For example is Days/HH:MM:SS.FFFFFF (E.g. 3/14:12:04.201000 ) acceptable? With HH : Hours, MM : Minutes, SS : Seconds, FFFFFFF fraction of a second.


Solution

  • ISO 8601 defines durations of the form

    P...Y...M...DT...H...M...S
    

    where ... are numbers and

    • P (period) marks the string as a duration
    • Y years
    • M months
    • D days
    • T (time) resolves ambiguity between months and minutes
    • H hours
    • M minutes
    • S seconds

    Components with value 0 can be left out as long as there is at least one component (e.g. PT5S denotes 5 seconds). The least significant component can have a fractional value (e.g. PT0.5H denotes half an hour = 30 minutes).

    However, ISO 8601 defined these durations for specifying time intervals of the form start_date_time + duration. Depending on the start date and time, the length of every component but S might vary.

    • Years can have 365 or 366 days for leap years.
    • Months can have between 28 and 31 days.
    • Days can have 23 to 25 hours when changing daylight saving time.
    • Minutes can have 60 or 61 seconds for leap seconds.

    See also What is the value of the ISO 8601 duration `P1M` (in seconds)?. ISO 8601 mentiones that some applications may fix durations like P1M = P30D and so on, but does not define it at all.

    To work around these interpretation problems, you could use seconds only, e.g. PT3600S instead of PT1H (the standard does not enforce carry-over points like 60s, 24h and so on). However, that seems pretty pointless. I'd rather just use the number in seconds without PT and S (unix-time style) or switch to a non-standardized (?) but abundant format like you suggested (video timestamps).