I have access logs from my server. I need to take the time difference between two logs. My access logs have the following time format: "14/Apr/2021 06:25:09".
To take the difference between two timestamps, I thought to convert the given time stamps in seconds since epoch and then take the difference.
Example of access log:
106.222.52.107 - - [14/Apr/2021:06:25:06 -0400] "POST URL1 HTTP/1.1" 204 4649 708 "Mozilla/5.0 (Linux; Android 11; V2037) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36"
157.34.255.192 - - [14/Apr/2021:06:25:08 -0400] "GET URL2 200 5309 1125 "Mozilla/5.0 (Linux; Android 7.1.1; CPH1729 Build/N6F26Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.98 Mobile Safari/537.36"
1.38.48.202 - - [14/Apr/2021:06:25:10 -0400] "GET URL3 HTTP/1.1" 200 8692 64616 "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1"
106.222.52.107 - - [14/Apr/2021:06:28:06 -0400] URL4 200 10506 1063 "Mozilla/5.0 (Linux; Android 11; V2037) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36"
157.34.255.192 - - [14/Apr/2021:06:29:06 -0400] URL5 200 620 866 "Mozilla/5.0 (Linux; Android 7.1.1; CPH1729 Build/N6F26Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.98 Mobile Safari/537.36"
So I want to convert "14/Apr/2021:06:25:06" in seconds since epoch.
I am using date command to convert the given time stamp in seconds. Following is the command:
date "+%s" -d="14/Apr/2021 06:25:09"
It is giving the following error:
date: invalid date ‘=14/Apr/2021 06:25:09’
But when I interchange the day and month positions then it's working fine.
date "+%s" -d "04/14/2021 06:25:09" Output: 1618399509
Does the date command have some predefined format? Changing time stamp format in access logs is difficult. Could you please provide the solution for this? I need to write a shell script where I need to read the access logs and depending upon the IP and time stamp I need to create another data file.
I am using Ubuntu 20.