1. Introduction

Since seconds are part of the SI (International System) standard, we often see and use values measured in this time unit. In fact, the UNIX epoch format represents the number of seconds since 1970-01-01T00:00:00+00:00. Still, we can have intervals of seconds starting from arbitrary points in time.

In this tutorial, we’ll discuss ways to convert a timestamp from one-dimensional seconds to other formats. First, we go over different time formats in seconds. After that, we leverage a common tool for date and time manipulation in Linux. Finally, we perform manual operations to achieve our goal of converting seconds to formatted time.

We tested the code in this tutorial on Debian 12 (Bookworm) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments unless otherwise specified.

2. Time Formats in Seconds

There can be many reference points for representing time with just seconds. Let’s explore some common ones.

2.1. UNIX Epoch

Naturally, one of the most common ways to measure time in seconds is the UNIX epoch timestamp. In essence, it measures the seconds since 1 January 1970.

For example, the datetime 9 September 2009 at 09:09:09 Coordinated Universal Time (UTC) is exactly 1252487349 seconds in UNIX epoch time. Importantly, according to the standard, we measure in seconds, although we could be more precise.

2.2. Start of Day

Commonly, schedulers like cron may restart their counters every day. This means that their reference point is often at 00:00:00 every 24 hours.

Thus, we might have timestamps which reflect that fact:

  • 59 for 00:00:59
  • 60 for 00:01:00
  • 3600 for 01:00:00
  • 32949 for 09:09:09

While these might not look familiar, internal task scheduling isn’t always done with regard to the initial task run, so we could end up with such values.

2.3. Specific Epoch

Of course, we’re free to choose our own reference point depending on the use case.

For example, a stopwatch starts from an arbitrary point in time. In fact, stopwatches create a new epoch at each start. To add on top of that, we can temporarily pause the measurement, which leaves us with three time components:

  • initial start
  • pause intervals
  • current time

Still, it’s usually not hard to perform the basic arithmetic required to reach the final number of seconds. In fact, we only need addition when we work with seconds, although subtraction and multiplication may also be needed for time unit conversion.

Naturally, humans are rarely the target audience of such numbers. So, let’s see how to convert timespans plus reference points to specific dates.

3. Using date

As usual, the standard GNU coreutils date command is the first utility to turn to when it comes to datetime manipulation.

In our case of converting timespans in seconds to formatted dates, we can leverage the –date or -d flag to supply a specific UNIX timestamp for conversion:

$ date --date=@1149573966 --utc
Tue Jun  6 06:06:06 AM UTC 2006

In this case, we convert 1149573966 seconds since 1970-01-01T00:00:00+00:00 into the default output time format date uses.

Furthermore, date can output any supported format, including custom formats:

$ date --date=@1149573966 --utc --rfc-3339=ns
2006-06-06 06:06:06.000000000+00:00
$ date --date=@1149573966 --utc --iso-8601=ns
2006-06-06T06:06:06,000000000+00:00
$ date --date=@1149573966 --utc +%Y%m%d%H%M%S
20060606060606

In this case, we first convert the same timestamp to both the RFC-3339 and the ISO-8601 standards in [n]ano[s]econd precision using –utc (–universal, -u). Finally, we also use a custom format that begins with a four-digit [%Y]ear and continues with two digits for each of the [%m]onth, [%d]ate, [%H]our, [%M]inute, and [%S]econd.

Of course, many programming languages, such as Java, support this time conversion due to the ubiquity of the UNIX timestamp. Still, neither date, nor other utilities can handle every possible case out of the box.

4. Manual Conversion

If our reference points change or we need to account for shifts, we can still use standard tools by first calculating the difference between our timespan and the UNIX epoch.

4.1. Start of Day Example

For example, if we use the start of the current day at 00:00:00 as a custom epoch, we can convert that to the UNIX standard:

$ date --date="$(date +%F)" +%s --utc
1694217600

In this case, we leverage command substitution with $(). Within, we use date with the %F sequence for a full date (equivalent to %+4Y-%m-%d). This way, we strip the time from the timestamp. By passing the result to the –date option, we effectively convert the start of the current day to the %s seconds since the UNIX epoch format.

After that, we can just add time as necessary or store the number for future offsetting.

4.2. Unit Conversion

Depending on our needs, we can even convert seconds to other units:

$ seconds=106660                         # 106660 seconds total =
$
$ minutes=$(( $seconds / (60)         )) # 1777 minutes total ~
$   hours=$(( $seconds / (60*60)      )) # 29 hours total ~
$    days=$(( $seconds / (60*60*24)   )) # 1 days total ~
$   weeks=$(( $seconds / (60*60*24*7) )) # 0 weeks total

In this case, the divisions are based on the number of smaller units within bigger ones and only leave the whole part. Still, we can perform the same calculations in a language that supports floating point numbers to get fractions as well.

4.3. Time Composition

Any time interval in seconds can be deconstructed into bigger time units:

$ seconds=106660                                        # 106660 seconds total =
$
$   weeks=$(( $seconds                / (60*60*24*7) )) # 0 weeks    +
$    days=$(( $seconds % (60*60*24*7) / (60*60*24)   )) # 1 day      +
$   hours=$(( $seconds % (60*60*24)   / (60*60)      )) # 5 hours    +
$ minutes=$(( $seconds % (60*60)      / (60)         )) # 37 minutes +
$ seconds=$(( $seconds % (60)                        )) # 40 seconds

Thus, we can get a timespan of seconds in a more human-readable form.

Naturally, all methods above are valid for any time reference and measurements in seconds from it.

5. Summary

In this article, we discussed ways to represent second timespans in common time formats.

In conclusion, depending on our particular timestamp, we have different means to switch from seconds to more complex timekeeping.