Score:0

how to take the time difference between two dates and time

ch flag

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.

hr flag
You may find this helpful: [Force date to read Day/Month/Year](https://unix.stackexchange.com/questions/296052/force-date-to-read-day-month-year)
guiverc avatar
cn flag
Ubuntu 20? No such release, so do you mean Ubuntu Core 20? (Ubuntu uses *year.month* for *deb* based products & the *year* format for *snap* only products)/ It also may help if you *clean* up your post; what do you mean by starting with "*Closed. This question needs details or clarity. It is not currently accepting answers. Add details and clarify the problem you’re solving. This will help others answer the question. You can edit the question or post a new one.*" - yes that is very unclear!
Jeff Schaller avatar
ru flag
You grabbed a little too much text when you copy/pasted from your [closed SO question](https://stackoverflow.com/questions/68493819/convert-a-given-time-stamp-into-seconds); please try to make your questions as neat and focused as possible for each community. Thank you!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.