Determine previous time in minutes

I have several logs with where the time stamp in the logs are "YYYYMMDDHHMM".

I would like to check the last line in each file to make sure the entry is less than 5 minutes old.
My timezone is EST5EDT so the following will work for 1 hour. But I need something easy for 5 minutes ago.

STALE_TIME=$(TZ=$(date +%Z)+5; date '+%Y%m%d%H%M')

I am using ksh and do not have gnu or ksh94.

Is there an easy way I can find what the time was 5 minutes ago?

Example1: current time: 201108081607
stale time: 201108081602

Example2: current time: 201201010001
stale time: 201112312356

One possible solution is to place your script under cron to run every 5 minutes.

In the same script you can create a file with the current YYYYMMDDHHmm.

Every time you run your script, you read the only record in the timestamp file.

At the end, you overwrite it with the current date/time.

I was hoping for something a little cleaner. This is an on demand script I am writing that will parse logs and display just the latest status. Any other ideas? :wall:

Would using perl be an option?

Date arithmetic in Unix shell scripts - Stack Overflow

Not sure but I can try. Do you have something in mind that I can try. I have never used perl.

I do think I did something a few years ago using the Julian date. I am goint to see if I can remember if that worked for me before or not.

---------- Post updated at 10:31 AM ---------- Previous update was at 10:05 AM ----------

Ok, I think it was cut time.

 
#!/bin/ksh
MIN_BACK=5
SEC_BACK=`expr ${MIN_BACK} \* 60`
CURRENT_TIME=`date +%Y%m%d%H%M'`
CURRENT_TIME_S=`date +%s`
STALE_TIME_S=`expr ${CURRENT_TIME_S} - ${SEC_BACK}`
echo "CURRENT_TIME=${CURRENT_TIME}"
echo "CURRENT_TIME_S=${CURRENT_TIME_S}"
echo "STALE_TIME_S=${STALE_TIME_S}"
 

CURRENT_TIME=201108091024
CURRENT_TIME_S=1312899850
STALE_TIME_S=1312899550

Now I just need to figure out how to get the STALE_TIME_S (seconds since January 1, 1970) into STALE_TIME (YYYYMMDDHHMM). I think I can use the date command or maybe TZ. Off the top of their head does anyone know?

---------- Post updated at 03:11 PM ---------- Previous update was at 10:31 AM ----------

I went in a different direction.

Since I already have the OLDTIME in YYYYMMDDHHMM format I simply touch my temp file with it. Then do a find on my temp file and return the file name if its over 5 minutes old.

touch -t ${OLDTIME} ${TMP_FILE}
STALE_CHECK_05MIN=`find ${TMP_FILE} -mmin +5`

Now I can to a test if the STALE_CHECK_05MIN is null. If Its not null then I know its older than 5 minutes.