-3

I've 2 columns as below with Job submission date.

NAME  SUBMIT_TIME
Job1 Apr 25 11:30
Job2 Apr 25 16:30

I need to have an alert which should trigger if the time difference is more than 60 min from current date/time.

Please help me to calculate the time difference in Min from current date/time in another column

e.g. I need to have a column as time difference from current date/time. suppose a job is running since 11:30 and now it 13:30, so it should give 120min as diff

6
  • Is your datetime stamp always missing the year information ? What happens if a job is submitted on Dec 31 at 23:40 and finishes on Jan 01 at 00:41 of the following year ? What do your datetime stamps look like in that case ?
    – Cbhihe
    Apr 25 at 15:11
  • It gives the information running at present day. I need to have a column as time difference from current date/time. suppose a job is running since 11:30 and now it 13:30, so it should give 120min as diff, Apr 25 at 15:15
  • 1
    Can you guarantee that timed events are always less that 24 hours apart? If so, rollover of day/month/year is not necessary: if difference in minutes is negative, add (24 * 60) before you check. But beware seasonal clock change. Apr 25 at 18:36
  • Does the time zone used have daylight savings time? If so what year are the dates associated with?
    – Ed Morton
    Apr 25 at 18:49
  • 1
    Yes, that's understood but if your data is related to a TZ that has DST then WHICH day matters. On a day when there's a DST change the difference between 1am and 3am is different from other days, e.g. in the time zone I live in from 1am to 3am on March 14th in 2022 was 120 minutes whereas in 2021 it was 60 minutes because the clocks went forward an hour at 2am on 3/14/21. If your TZ is UTC or some other TZ without DST then just say that in your question.
    – Ed Morton
    Apr 25 at 20:19

1 Answer 1

2

If your version of awk support time functions, and assuming that your missing year information is the current year, then you can do:

$ awk -F'[ :]' 'BEGIN{nowtime=systime();
                      mths="JanFebMarAprMayJunJulAugSepOctNovDec";
                     } 
                /[Jj]ob/ {$2=(match(mths,$2)+2)/3; 
                          jobtime=mktime(2022" "$2" "$3" "$4" "$5" "0, 1);
                          print (nowtime-jobtime)/60;
                         }
             ' datafile

If this solution with hard-coded missing year is not satisfactory to you because year changes (per my query in comments) are not properly covered, it might be advisable to change the format of the datafile with job submission dates that include the year in the format YYYY.

Note:
Awk, when it comes equipped with time function as in its GNU version, takes care of normalization when calculating time deltas, meaning it takes into account months of different lengths and also leap years when changing months or year. For this of course one needs a job submission datafile with date stamps that include year information to start with.

EDIT:
Just noticed that OP requests a columnar output with the time delta in minutes in an additional column. Modified code is:

$ awk -F'[ :]' 'BEGIN{nowtime=systime(); mths="JanFebMarAprMayJunJulAugSepOctNovDec";} 
                NR==1 {printf "%s  %s\n",$0,"DELTA(min)"}
                /[Jj]ob/ {mo=(match(mths,$2)+2)/3;
                          jobtime=mktime(2022" "mo" "$3" "$4" "$5" "0, 1); 
                          printf "%s %-4.1f\n",$0,(nowtime-jobtime)/60;
                         }
              ' datafile

NAME  SUBMIT_TIME  DELTA(min)                                       
Job1 Apr 25 11:30 410.2                                       
Job2 Apr 25 16:30 110.2

Time difference (delta) numbers are for the example datafile provided and (my) system time.

EDIT:
I incorporated the UTC TZ-flag 1 in the code as the 2nd argument of the mktime() function, per @EdMorton's suggestion, assuming the host TZ setting corresponds to UTC time stamps.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.