0

I am trying to create a script in AIX (ksh/bash) where I need to compare two variables with two different date formats, and raise an alert if the difference between the StartTime and CurrentTime is greater than 5 minutes.

As an example, if I have a script that has these three variables:

StartTime="20 Oct 2022 12:20:48 -0700"
CurrentTime=$(date)
AlertThreshold=300

How can I compare the two, and do something if the difference between StartTime and CurrentTime is greater than AlertThreshold (300 seconds)?

The value returned by $(date) is in this format: Thu Oct 20 12:37:05 PDT 2022

I am stuck trying to figure out a way to convert both variables to a format where I can compare the values, so that I can test to see if the time difference is greater than AlertThreshold.

I assume both would need to be converted to unix timestamp to compare?

Any help would be appreciated.

date command usage:

[mmddHHMM[[cc]yy]] [+"Field Descriptors"]
Usage: date [-n][-u] [mmddHHMM[.SS[cc]yy]] [+"Field Descriptors"]
Usage: date [-a [+|-]sss[.fff]]
v3rd1ct
  • 67
  • 7
  • please update the question with the output from `date --version` – markp-fuso Oct 20 '22 at 19:56
  • The date command does not recognize - param, updated with some output. – v3rd1ct Oct 20 '22 at 19:58
  • "ksh/bash" -- which one, specifically? – Charles Duffy Oct 20 '22 at 20:00
  • If you only need to support versions of bash new enough to support `printf %(...)T`, that gives you options that don't require use of `date` at all. For that matter, if you only need to support bash, that means you can ask the shell for the number of seconds since the script started, so you may not need `StartTime` at all – Charles Duffy Oct 20 '22 at 20:01
  • (in general, it makes things a lot easier if you can do all of this in epoch time instead of trying to support human-centric time formats at all). – Charles Duffy Oct 20 '22 at 20:02
  • The date/time format stored in StartTime I have no control of changing (they are captured from an API call), how do I convert both formats to epoch time so I can compare? – v3rd1ct Oct 20 '22 at 21:06
  • It can be ksh or bash, whatever works. – v3rd1ct Oct 20 '22 at 21:06
  • Which _specific verison_ of bash do you have? (And also, which specific version of ksh? Very different answers for ksh93 vs mksh or ksh88, f/e). – Charles Duffy Oct 20 '22 at 21:18
  • 1
    (that said, I'm generally in the "yes, perl is a good choice for this" camp). – Charles Duffy Oct 20 '22 at 21:19
  • (For a different approach see https://stackoverflow.com/a/14545819/14122, but that question is specifically talking about converting to epoch time with _GNU_ awk). – Charles Duffy Oct 20 '22 at 21:24
  • I would convert both formats into the number of seconds since the epoch, and then compare them numerically. Getting the current time in this way is trivial - no conversion needed, just use the appropriate options for `date`, so all what is left is parsing the start date. – user1934428 Oct 21 '22 at 07:03
  • I have GNU bash, version 4.4.0(2)-release (powerpc-ibm-aix6.1.0.0) available on the system. – v3rd1ct Oct 21 '22 at 12:07

2 Answers2

1

You could use perl:

if perl -MDate::Parse -e '
   exit( str2time($ARGV[1]) - str2time($ARGV[0]) <= $ARGV[2] )
   ' "$StartTime" "$CurrentTime" "$AlertThreshold"
then
    echo alert
fi

Or a 100% portable way that you can adapt to your time formats; it shouldn't be too difficult as long as the timezones are the same for the two dates.

Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • I will give this a shot, there is a perl package installed on the system (perl 5, version 28, subversion 1 (v5.28.1) built for aix-thread-multi). – v3rd1ct Oct 21 '22 at 12:06
  • Perfect, this works after I installed the Date::Parse module; thank you! – v3rd1ct Oct 21 '22 at 12:24
1

This should do what you want. The key is to use date to convert the dates to seconds, and then the rest is straightforward. If you don't need the times to be human-readable, you can use, e.g., currentTime="$(date +%s)" and work exclusively in seconds.

#!/bin/bash
startTime="20 Oct 2022 12:20:48 -0700"
currentTime="$(date)"
alertThreshold=300

# Convert dates to seconds.
currSec=$(date -d "${currentTime}" +%s)
startSec=$(date -d "${startTime}" +%s)

if ((currSec - startSec > alertThreshold)); then
  echo "Alert!"
else
  echo "Keep waiting."
fi
Adam Liss
  • 47,594
  • 12
  • 108
  • 150