Calculate time difference for a given dataset

I have a list of down/up times and need to calculate the total down time, per day

Here is a snippet of the original dataset:

Jan  1 00:09:56 changed state to down
Jan  1 00:10:02 changed state to up
Jan  1 00:10:45 changed state to down
Jan  1 00:10:52 changed state to up
Jan  1 00:11:45 changed state to down
Jan  1 00:11:52 changed state to up
Jan  2 00:14:55 changed state to down
Jan  2 00:15:05 changed state to up
Jan  2 00:15:35 changed state to down
Jan  2 00:16:41 changed state to up
Jan  3 00:05:22 changed state to down
Jan  3 00:05:28 changed state to up
Jan  3 23:59:58 changed state to down
Jan  4 00:00:19 changed state to up
Jan  4 00:49:28 changed state to down
Jan  4 00:49:34 changed state to up

What I'm asking for is an output like this:

Jan 1: 20 seconds
Jan 2: 76 seconds
Jan 3: 27 seconds
Jan 4: 6 seconds

I've written this script to get the timestamps into epoch time:

while read i; do
  datetime=$(echo $i | awk '{print $1" "$2" "$3}')
  epochtime=$(date -d "$datetime" +\%s)
  echo $epochtime
done < ./file

which will give me the data like this:

1640995796
1640995802
1640995845
1640995852
...

I can't seem to find a way to combine them to a single line so I can use this code (or similar):

for i in $epochtime; do
  starttime=$(awk '{print $2}')
  endtime=$(awk '{print $1}')
  delta=$(( endtime - starttime ))
  echo $delta
done

Thanks in advance!


Solution 1:

$ cat tst.awk
{
    prevSecs = currSecs
    prevDate = currDate
    currDate = $1 " " $2
    $1 = (index("JanFebMarAprMayJunJulAugSepOctNovDec",$1) + 2) / 3
    currSecs = mktime("2022 " gensub(/:/," ","g"), 1)
}

NR%2 == 0 {
    dayDeltaSecs += (currSecs - prevSecs)
}

(NR > 1) && (currDate != prevDate) {
    printf "%s: %d seconds\n", prevDate, dayDeltaSecs
    dayDeltaSecs = 0
}

END {
    printf "%s: %d seconds\n", prevDate, dayDeltaSecs
}

$ awk -f tst.awk file
Jan 1: 20 seconds
Jan 2: 76 seconds
Jan 3: 27 seconds
Jan 4: 6 seconds

Original answer:

This may be what you're trying to do, using GNU awk for time functions:

$ cat tst.awk
{
    prevEt = epochtime
    $1 = (index("JanFebMarAprMayJunJulAugSepOctNovDec",$1) + 2) / 3
    epochtime = mktime("2022 " gensub(/:/," ","g"), 1)
}
NR%2 == 0 {
    print prevEt, epochtime, epochtime - prevEt
}

$ awk -f tst.awk file
1640995796 1640995802 6
1640995845 1640995852 7