How should a duration of 500 milliseconds be expressed using ISO 8601? To me, the documentation is not 100% clear. Is it just "PT0.5S" ?
Yes.
I could only find an old draft for 8601 but it is explicit on the fractional representation:
5.5 Periods of time
5.5.1 Means of specifying periods of time
A period of time shall be expressed in one of the following ways:
a) As a duration delimited by a specific start and a specific end;
b) As a duration expressed in one or more specific components but not associated with any specific start or end;
c) As a duration associated with a specific start;
d) As a duration associated with a specific end.
5.5.2 Separators and designators
A solidus [/] shall be used to separate the two components in each of 5.5.1 a), c) and d).
For 5.5.1 b), c) and d) the designator [P] shall precede, without spaces, the representation of the duration.
Other designators (and the hyphen when used to indicate omitted components) shall be used as shown in 5.5.4 and 5.5.5 below.
and later
5.5.3.1 Format with time-unit designators
In expressions of period of time or recurring time-interval duration can be represented by a data element of variable length. The number of years shall be followed by the designator [Y], the number of months by [M], the number of weeks by [W], and the number of days by [D]. The part including time components shall be preceded by the designator [T]; the number of hours shall be followed by [H], the number of minutes by [M] and the number of seconds by [S]. In the examples [n] represents one or more digits, constituting a positive integer or zero.
In complete representations the format shall be nYnMnDTnHnMnS or nW.
For reduced precision, decimal or truncated representations of this format of duration the following rules apply:
a) If necessary for a particular application the lowest order components may be omitted to represent duration with reduced precision;
b) If necessary for a particular application the lowest order component may have a decimal fraction. The decimal fraction shall be divided from the integer part by the decimal sign specified in ISO 31-0: i.e. the comma [,] or full stop [.]. Of these, the comma is the preferred sign. If the magnitude of the number is less than unity, the decimal sign shall be preceded by a zero (see ISO 31-0);
c) If the number of years, months, days, hours, minutes or seconds in any of these expressions equals zero, the number and the corresponding designator may be absent; however, at least one number and its designator shall be present. Note that the removal of leading non-zero components is not allowed;
d) The designator T shall be absent if all of the time components are absent.
All this is coherent with the wikipedia article, so the ISO-8601 of a duration of 500 ms should be PT0,5S
or PT0.5S
Is it just "PT0.5S" ?
Yes, it is a correct representation.
Test using Java-8 standard library
java.time.Duration
is modelled on ISO-8601 standards and was introduced as part of JSR-310 implementation.
Demo:
import java.time.Duration;
public class Main {
public static void main(String[] args) {
System.out.println(Duration.ofMillis(500));
}
}
Output:
PT0.5S
Learn about the modern date-time API from Trail: Date Time.
PT0.5S
is correct provided the ISO 8601 Duration format is
P(n)Y(n)M(n)DT(n)H(n)M(n)S
there is no place for milliseconds.
So for 10 milliseconds
it will be PT0.01S
and 1 millisecond can be denoted as PT0.001S
ISO 8601
Durations are expressed using the following format, where (n)
is replaced by the value for each of the date and time elements that follow the (n):
P(n)
Y(n)
M(n)
DT(n)
H(n)
M(n)
S
Here
P is the duration designator (referred to as "period"), and is always placed at the beginning of the duration.
Y is the year designator that follows the value for the number of years.
M is the month designator that follows the value for the number of months.
W is the week designator that follows the value for the number of weeks.
D is the day designator that follows the value for the number of days.
T is the time designator that precedes the time components.
H is the hour designator that follows the value for the number of hours.
M is the minute designator that follows the value for the number of minutes.
S is the second designator that follows the value for the number of seconds.
Example
To represent 9 years 8 month 7 days 6 hours 5 min & 4 second
will be written as
P9Y8M7DT6H5M4S
It can also be written individually as the 0 is omitted:
P9Y
= 9 years
P8M
= 8 months
P7D
= 7 days
PT6H
= 6 hour
PT5M
= 5 minutes
PT4S
= 4 second
See the use of T
between P8M
& PT5M
to represent Month or Minute.
To represent 0(Zero) second you can use PT0S
but to represent value in between like millisecond you have to use PT0.5S
which is half second or 500 millisecond. The same will be true to P0.5Y
to represent half year or P6M
As of today, as an unofficial response quoting unofficial source, one may visit timestamp-converter.com to find out the following example (loaded at page creation I guess)
...
ISO 8601
2023-08-25T08:35:16.792Z
supporting format: 1970-01-01T00:00:00.000Z or 1970-01-01T00:00:00.000+00:00
...
© 2022 - 2025 — McMap. All rights reserved.