0

In a streaming video dash manifest we receive the period start time as PT461486H44M42.24S. This is basically a timestamp for the media segment to be loaded. Sample will be

  <Period id="C 2022-08-26 08-45-41" start="PT461486H44M42.24S">

I want to know what is this time format? And how can I convert it to long milliseconds. I checked the [SimpleDateFormat] SDF but it doesn't matches.

WISHY
  • 11,067
  • 25
  • 105
  • 197
  • 2
    Does this answer your question? [What does 'PT' prefix stand for in Duration?](https://stackoverflow.com/questions/51168022/what-does-pt-prefix-stand-for-in-duration) – slindenau Aug 25 '22 at 09:03
  • `PT461486H44M42.24S` is how long the presentation takes, its duration. `SimpleDateFormat` cannot handle durations, you are right. Which is somehow good because the `SimpleDateFormat` class is also notoriously troublesome and long outdated, nothing that you should want to struggle with under any circumstances at all. As the answer already says use `Duration` from [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 27 '22 at 09:54
  • I don’t know Dash, but suspect there may be something wrong with your data. [One example I found here](https://ottverse.com/single-period-vs-multi-period-dash/) has ``, so `start` is 32 seconds, not 52 years like yours. I can’t understand how both can make sense. [This page](https://dashif-documents.azurewebsites.net/Guidelines-TimingModel/master/Guidelines-TimingModel.html) has example that are 20 seconds and 300 seconds (5 minutes). – Ole V.V. Aug 28 '22 at 08:09

1 Answers1

3

Your String seems to represent a duration in ISO-8601 format.

You can use java.time.Duration.parse in order to create a Duration which may give you information about specific unit. Since a duration is an amount of time in contrast to epoch millis, you should not convert Duration.toMillis() to any date or datetime. They are basically totally different.

Here's an example usage of a Duration derived from your String:

public static void main(String[] args) {
    // your example String
    String durStr = "PT461486H44M42.24S";
    // parse it to a Duration
    Duration duration = Duration.parse(durStr);
    // create a result message from the units of a Duration
    String durMsg = String.format("%d milliseconds are %d days, %d hours, %d minutes, %d seconds and %d milliseconds",
                                duration.toMillis(),
                                duration.toDays(),
                                duration.toHoursPart(), 
                                duration.toMinutesPart(), 
                                duration.toSecondsPart(), 
                                duration.toMillisPart());
    // print the result message
    System.out.println(durMsg);
}

Output:

1661352282240 milliseconds are 19228 days, 14 hours, 44 minutes, 42 seconds and 240 milliseconds

If you suspect the result of duration.toMillis() not to be the length of the video but , for example, the creation timestamp (in epoch millis) or the age in millis, don't use the outdated API (Date, SimpleDateFormat and so on) but use java.time. You can use those millis to create an Instant which you can then use to create a ZonedDateTime of OffsetDateTime:

// create a Temporal using an Instant and a fixed offset (UTC / +00:00 here)
OffsetDateTime odt = Instant.EPOCH.plus(duration)
                                  .atOffset(ZoneOffset.UTC);
// print the result
System.out.println(odt);

Output:

2022-08-24T14:44:42.240Z

Different idea:

You can subtract the parsed Duration from a current moment in time, which – under circumstances – may give you the moment in time of the first media presentation (like when it was released):

Instant instant = Instant.now().minus(duration);
OffsetDateTime odt = instant.atOffset(ZoneOffset.UTC);
System.out.println(odt);

This just output the following:

1970-01-01T21:37:52.460090Z

This approach is just an idea and it heavily depends on the duration between receiving the media presentation time (your String) and the calculation involving Instant.now(), which should ideally be the same moment in time in order to get the (most) correct time of the media presentation.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    You're welcome… Pay attention when calculating with millis represented as a duration ;-) – deHaar Aug 25 '22 at 12:30
  • Hi @deHaar, can I know the JDK version that you are using? I'm getting compilation errors for `toHoursPart()`, `toMinutesPart()`, `toSecondsPart()`, and `toMillisPart()`. – Arun Sudhakaran Aug 25 '22 at 15:44
  • 2
    I used Java 11, and those methods were introduced in Java 9, I think. – deHaar Aug 26 '22 at 08:38
  • 1
    Good answer and a good edit. A minor detail and a matter of taste. For my part I like the fluent writing style that java.time often lends itself well to. So I’d do `OffsetDateTime odt = Instant.EPOCH.plus(duration).atOffset(ZoneOffset.UTC);`. The result is still `2022-08-24T14:44:42.240Z`, the same as you got. And in the last example simply `OffsetDateTime odt = instant.atOffset(ZoneOffset.UTC);`. – Ole V.V. Aug 28 '22 at 08:28
  • @deHaar thanks for the answer. I also want the millis to convert it back to same format `PT461486H44M42.24S`. I explored the `Instant` but it doesn't give the same output. Anything to help on this? – WISHY Sep 07 '22 at 08:30
  • 1
    @WISHY That's really easy: It's the `Duration.toString()` method which produces such a `String` value. If you have millis, create a `Duration.ofMillis()` (or take any `Duration`) and use it's `toString()`. – deHaar Sep 07 '22 at 08:36