Display elapsed time (seconds) to HH:MM:SS

Hi!

I’m currently developing an binding for the Clementine Player. Everything works nicely, but I have a formatting question:

The player, when playing a song, sends the position (seconds within the current track) every second.

I handle this in the following way:

private void handleTrackPos(ResponseUpdateTrackPosition message) {
    currentPos = message.getPosition(); // seconds
    var epoch = Instant.ofEpochSecond(currentPos).atZone(ZoneOffset.UTC);
    updateState(CHANNEL_POSITION,new DateTimeType(epoch));
}

CHANEL_POSITION is of type DateTime.

When I create an instance of my ClementineRemote binding and create an Item of this channel, I want the elapsed time to be formatted as hours:minutes:seconds.

I tried to use %1$tH:%1$tM:%1$tS or %1$tT as pattern in the stateDescription of the item.

This almost gets the job done; however any track now starts at 1:00:00, since my timezone offset to UTC is one hour.

How can I mitigate this offset problem?

Of course I could use my zone within the .atZone call, but this would break the function for any user not in my zone.

atZone(ZoneId.systemDefault())

Thanks for the hint. That is what I tried first. Unfortunately, the clock of the system on which my openhabian runs uses UTC zone. Thus, the timestamps I get are the same for both versions:

.atZone(ZoneOffset.UTC)

and

.atZone(ZoneId.systemDefault())

And the epoch object I get out of this is correctly carrying the seconds elapsed.

My problem is, that the formatting in the Item’s stateDescription pattern does apply a zone conversion where none is needed.

You should not use DateTime for duration, but instead Number:Time. DateTime with or without date (i.e. time-only) is used to express a point in time, whereas Number:Time is used to express an amount of time.

See this PR for an example, which also shows how it can be formatted:

Perfect! This does the trick.

My fork is making progress towards a first release candidate :wink:

1 Like