>_ IzzyLab

Hands-on task

This task is step 7 of the course “Dates and times in the terminal”

Seconds to HH:MM:SS

medium~5 min

The file /root/seconds.txt holds a duration in seconds (less than a day). Represent it in the HH:MM:SS format with leading zeros — for example, 3725 seconds is 01:02:05 — and write the result to /root/dur.txt. The sandbox's system time zone is Europe/Berlin, not UTC.

What you'll practise:
  • date
  • -u
  • epoch
  • %T
  • duration

Topic breakdown

The %T format is %H:%M:%S. The date -u -d @N +%T trick relies on the fact that Unix time @N is N seconds after midnight 1970-01-01 UTC; for N below 86400 the clock part matches a duration of N seconds. The -u flag pins UTC, otherwise the time zone offset would shift the hours. For durations of a day or more the trick breaks — then you compute it by hand with integer division and printf with %02d for leading zeros.

What's checked

  1. duration in HH:MM:SS is correct

What it looks like