how to convert time from 24H to 12H then subtract the two times

I have a file that collects time stamps of HH:MM need to add one hour to the 1st column and convert the both columns from 24H to 12H then subtract them.

Output in the file I collected the times from:

Start End Total Run Time
00:00 05:39
01:31 06:02
00:48 06:24
23:46 04:50
00:05 05:12
00:06 05:04
00:04 05:10
00:10 05:10
00:00 04:51
00:10 05:33
23:41 04:15

I want to give the total run time, between these two columns.

so the 1st time stamps should be 01:00 05:39 before subtracting to get the total time run 01:00-05:39.

I tried with just this command to subtract but the 24H to 12H and adding one hour to the $1 1st column, just confuses me.

awk 'BEGIN { OFS = "\t" } { $3 = $2 - $1 } 1'
1

1 Answer

You can't simply subtract datetime objects like 01:00 and 05:39 in awk (although GNU awk has its own Time Functions that you could use to do it).

Having said that, formating a datetime difference as as datetime is somewhat questionable. Miller's builtin strptime/strftime implementations look like they will handle it - including correctly rolling over the day boundary.

Ex. given

$ cat file
Start End
00:00 05:39
01:31 06:02
00:48 06:24
23:46 04:50
00:05 05:12
00:06 05:04
00:04 05:10
00:10 05:10
00:00 04:51
00:10 05:33
23:41 04:15

then

$ mlr --pprint put -S ' ${Total Run Time} = strftime(strptime(${End},"%H:%M") - 3600 - strptime(${Start},"%H:%M"),"%H:%M") ' file
Start End Total Run Time
00:00 05:39 04:39
01:31 06:02 03:31
00:48 06:24 04:36
23:46 04:50 04:04
00:05 05:12 04:07
00:06 05:04 03:58
00:04 05:10 04:06
00:10 05:10 04:00
00:00 04:51 03:51
00:10 05:33 04:23
23:41 04:15 03:34
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like