Trying to understand “PT1H” in a date-time string

adviner picture adviner · May 12, 2019 · Viewed 7.3k times · Source

I'm using the Weather API to pull weather information down as a service for my project. I'm trying to understand some timezone offsets that I can't seem to find information on.

The URL I'm using is:

https://api.weather.gov/gridpoints/VEF/154,48

Here is some sample return values:

"temperature": {
    "sourceUnit": "F",
    "uom": "unit:degC",
    "values": [
        {
            "validTime": "2019-05-11T16:00:00+00:00/PT1H",
            "value": 18.333333333333371
        },
        {
            "validTime": "2019-05-12T04:00:00+00:00/PT2H",
            "value": 16.1111111111112
        },
        {
            "validTime": "2019-05-12T21:00:00+00:00/PT4H",
            "value": 26.666666666666742
        },
        ...
    ]
}

I understand the PT means Pacific Timezone. But I cant seem to find any information on the next to characters like 1H, 2H, etc.

If anyone can advise that would be appreciated - Thanks

Answer

Basil Bourque picture Basil Bourque · May 12, 2019

PT1H = One hour

I understand the PT means Pacific Timezone.

No, incorrect assumption. Not 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. The T separates any years-months-days portion from any hours-minutes-seconds portion.

So PT1H means “one hour”. Two and a half hours would be PT2H30M.

Parsing

Your input "2019-05-11T16:00:00+00:00/PT1H" combining a starting moment with a duration is part of the ISO 8601 standard.

Such a combo string can be parsed by the Interval.parse method found in the ThreeTen-Extra library.

Interval interval = Interval.parse( "2019-05-11T16:00:00+00:00/PT1H" ) ;

An Interval represents a pair of moments, a pair of Instant objects. In your case here, the second moment is calculated, by adding the duration to the starting moment. We can interrogate for the pair of moments, represented as Instant objects (always in UTC by definition).

Instant start = interval.getStart() ;
Instant stop = interval.getEnd() ;