Bash: Calculate the time differences in hours from input like "YYYYMMDDHH"

Viewed 88

I have two dates in forms like: YYYYMMDDHH and want to calculate the differences (in hours) between these two dates. For example

start_date=1996010100
end_date=1996010122

which stands for two dates: 1996-01-01 00:00:00 and 1996-01-01 22:00:00. I want to use date to calculate the difference in hours, the result shall be 22 hours. I tried with

START=$(date -d "$start_date" +"%s")
END=$(date -d "$end_date" +"%s")
HOURS=$(bc -l <<< "($END - $START) / 3600")

but it failed... So how can I do this? Thanks!

5 Answers

For performance reasons we want to limit the number of sub-process calls we need to invoke:

  • use bash substring functionality to convert inputs into usable date/time strings
  • use bash math to replace bc call

bash substring functionality to break the inputs into a usable date/time format, eg:

# convert to usable date/time format:

$ start_date=1996010100
$ echo "${start_date:0:4}-${start_date:4:2}-${start_date:6:2} ${start_date:8:2}:00:00"
1996-01-01 00:00:00

# convert to epoch/seconds:

$ start=$(date -d "${start_date:0:4}-${start_date:4:2}-${start_date:6:2} ${start_date:8:2}:00:00" +"%s")
$ echo $start
820476000

Applying to ${end_date} and using bash math:

$ end_date=1996010122
$ end=$(date -d "${end_date:0:4}-${end_date:4:2}-${end_date:6:2} ${end_date:8:2}:00:00" +"%s")
$ echo $end
820555200

$ hours=$(( (end - start) / 3600))
$ echo $hours
22

This leaves us with 2 sub-process calls ($(date ...)). While other languages/tools (awk, perl, etc) can likely speed this up a bit, if you need to store the result in a bash variable then you're looking at needing at least 1 sub-process call (ie, hours=$(awk/perl/??? ...)).

If performance is really important (eg, needing to perform this 1000's of times) take a look at this SO answer that uses a fifo, background date process and io redirection ... yeah, a bit more coding and a bit more convoluted but also a bit faster for large volumes of operations.

busybox date can do the trick

start_date=1996010100
end_date=1996010122

START=$(busybox date -D "%Y%m%d%H" -d "$start_date" +"%s")
END=$(busybox date -D "%Y%m%d%H" -d "$end_date" +"%s")
HOURS=$(bc -l <<< "scale=0;($END - $START) / 3600")
echo $HOURS

If it's possible for you to use a more fully-featured scripting language like Python, it'll provide a much more pleasant and understandable date parsing experience, and is probably installed by default (datetime is also a standard Python library)

Structured with shell vars

start_date=1996010100
end_date=1996010122
python -c "import datetime ; td = datetime.datetime.strptime('${end_date}', '%Y%m%d%H') - datetime.datetime.strptime('${start_date}', '%Y%m%d%H') ; print(int(td.total_seconds() / 3600))"

Structured to read dates and format code from stdin

echo '%Y%m%d%H' 1996010100 1996010122 | python -c "import datetime,sys ; fmt, date_start, date_end = sys.stdin.read().strip().split() ; td = datetime.datetime.strptime(date_end, fmt) - datetime.datetime.strptime(date_start, fmt) ; print(int(td.total_seconds() / 3600))"

Should work with both Python 3 and Python 2.7

format codes available here (1989 C standard)
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

which stands for two dates: 1996-01-01 00:00:00

So convert it to that form if it stands for it.

start_date=1996010100
start_date=$(sed -E 's/(....)(..)(..)(..)/\1-\2-\3 \4:00:00/' <<<"$start_date")
start=$(date -d "$start_date" +"%s")

and the same with end.

the most simple way is to install "dateutils" using this command

sudo apt-get install dateutils

Run these commands to get the difference in seconds:

dateutils.ddiff -i '%Y%m%d%H%M%S' 20200817040001 20210817040101

output:

31536060s

next step: Simply divide by 86400 to get the number of days or similarly for hours and minutes :)

Related