I am trying to use the Duration
class instead of long
. It has superior literal syntax. I like its flexibility, though it looks weird.
"PT10S" means 10 seconds, what is the problem to accept "10 seconds"?! Okay never mind.
I am just curious why PT prefix has been chosen (not "DU" e.g.) and why any prefix is better here rather than nothing?
PT means 'Period of Time'. The time format is standardized according to ISO 8601. For example PT1H30M15S - 1 hour 30 minutes 15 seconds. Hope this helps.
Examples: "PT20S" -- parses as "20 seconds" "PT15M" -- parses as "15 minutes" (where a minute is 60 seconds) "PT10H" -- parses as "10 hours" (where an hour is 3600 seconds) "P2D" -- parses as "2 days" (where a day is 24 hours or 86400 seconds) "P2DT3H4M" -- parses as "2 days, 3 hours and 4 minutes"
PT1H = One hourNot a time zone. The PT1H represents a duration, a span of time not tied to the timeline. This format is defined in the ISO 8601 standard. The P marks the beginning, short for “Period” I imagine, a synonym for duration.
As can be found on the page Jesper linked to (ISO-8601 - Data elements and interchange formats – Information interchange – Representation of dates and times)
P is the duration designator (for period) placed at the start of the duration representation. 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 of the representation.
So P means 'Period' and because there are no date-components it only has a 'Time'.
You could interpret this as 'Period of Time'
The 'why' this was chosen, you have to ask the ISO members that wrote the standard, but my guess is that it is easier to parse. (short and unambigious)
Java has taken a subset of the ISO 8601 standard format for a duration. So the “why” is why the standard was written the way it is, and it’s a guessing game. My go is:
P
for period was chosen so that you can distinguish a duration from a date and/or time. Especially since a period may also be written in the same format as a local date-time, for example P0003-06-04T12:30:05
for 3 years 6 months 4 days 12 hours 30 minutes 5 seconds, the P
can be necessary to distinguish. The P
also gives a little but quick and convenient bit of validation in case you happen to pass a completely different string in a place where a duration was expected. And yes, PT10S
looks weird, but once you get accustomed to it, you recognize it immediately as a duration, which can be practical.T
for time between the date part and the time part was chosen for two reasons: T
in the same place, for example 2018-07-04T15:00
for July 4, 2018 at 15:00 hours.M
for either months or minutes: P3M
unambiguously means 3 months while PT3M
means 3 minutes.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With