>_ IzzyLab

Hands-on task

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

Get Unix time from a date

easy~3 min

The file /root/when.txt contains a date with time and timezone: 2024-03-01 12:00:00 UTC. Convert it to a Unix timestamp (the number of seconds since the epoch) and write the number to /root/epoch.txt. Note: the sandbox's system time zone is Europe/Berlin, not UTC.

What you'll practise:
  • date
  • unix time
  • epoch
  • timezones

Topic breakdown

The reverse of parsing a date: date not only prints a date from an epoch, but also computes an epoch from a date — the format code %s gives the number of seconds since the start of the epoch. The string to parse (together with the timezone specified in it, e.g. UTC) is passed via the -d option. Since the string contains spaces, it's important to wrap the substitution from the file in quotes, or the argument will be split apart.

What's checked

  1. epoch is correct

What it looks like