Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'PT' prefix stand for in Duration?

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?

like image 419
Daniil Iaitskov Avatar asked Jul 04 '18 07:07

Daniil Iaitskov


People also ask

What does PT mean in duration?

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.

What does PT15M mean?

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"

What is PT1H?

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.


2 Answers

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)

like image 136
Rob Audenaerde Avatar answered Oct 02 '22 05:10

Rob Audenaerde


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:
    • For consistency with date-time strings that have T in the same place, for example 2018-07-04T15:00 for July 4, 2018 at 15:00 hours.
    • To disambiguate the otherwise ambiguous M for either months or minutes: P3M unambiguously means 3 months while PT3M means 3 minutes.
like image 45
Ole V.V. Avatar answered Oct 02 '22 06:10

Ole V.V.