How to Find the Time Between two Unix Time Stamps

This article explains how to find the time interval between two Unix time stamps. The default time format in Unix is like this: “Sun 9 Aug 12:50:08 BST 2020”. It appears in many places on the system and is the default produced by the “date” command.

For example, it might appear in application log files, like this::

Start time: Sun 9 Aug 10:05:00 BST 2020
End Time:   Sun 9 Aug 12:51:12 BST 2020

This article explains how to subtract one time from the other in Bash, and obtain the intervening time in seconds.

Subtracting Times

The usual method would be to convert both times to an “epoch” number (ie. the number of seconds since 01 Jan 1970) then do the subtraction. date -d can be used for the conversion.

However, date -d refuses to “eat its own format”. That is to day, it will not process dates provided in its own default format. Using -d leads to an error:

$ date
Sun 9 Aug 12:58:41 BST 2020
$date -d "Sun 9 Aug 12:58:41 BST 2020"
date: invalid date ‘Sun 9 Aug 12:58:41 BST 2020’

Format Change

To use -d successfully, a little pre-processing is required. We need to put a comma after the day name, and move the year to be just after the time. In other words, convert a default date/time like this:


Sun 9 Aug 12:58:41 BST 2020

to be like this, comma inserted and year moved:

Sun, 9 Aug 2020 12:58:41 BST

Then it works:

$ date -d "Sun, 9 Aug 2020 12:58:41 BST"
Sun 9 Aug 12:58:41 BST 2020

Convert to Epoch Time

Now use the date command’s percent operator to convert that time to an epoch number:

$ date +%s -d "Sun, 9 Aug 2020 12:58:41 BST"
1596974321

Use awk to do the twiddling, and a “raw” date/time stamp can be converted to an epoch number in a single step:

$ date +%s -d "$(echo 'Sun 9 Aug 12:58:41 BST 2020' | awk '{print $1", ",$2, $3, $6, $4, $5}')"
1596974321

Script Example

Which leads to a simple script. if $d1 and $d2 are two Unix time stamps, the difference in seconds can be calculated thus:

#!/bin/bash

d1="Sun 9 Aug 10:05:00 BST 2020"
d2="Sun 9 Aug 12:51:12 BST 2020"

epoch1=$(date +%s -d "$(echo $d1 | awk '{print $1", ",$2, $3, $6, $4, $5}')")
epoch2=$(date +%s -d "$(echo $d2 | awk '{print $1", ",$2, $3, $6, $4, $5}')")

let interval_seconds=epoch2-epoch1
echo interval_seconds $interval_seconds

Save the script as interval.sh and test it:

$ ./interval.sh
interval_seconds 9972

The script could be easily adjusted to take times as arguments, etc.

Conclusion

Standard Unix time stamps, often found in log files and elsewhere, can be subtracted in Bash, without the need to involve Python, Perl or other tools.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.