Splunk Search

How to convert time format to epoch time in order to calculate difference?

kiran331
Builder

Hi

How to convert the time format ‎"2016‎-‎12‎-‎07T09:33:33.040875200Z" to epoch time for calculating difference and then to readable format?

Tags (3)
0 Karma

sundareshr
Legend

It appears there are some special chars in the data. Try this.

.... | rex mode=sed field=Previous_Time "s/(\W)//g"|  eval Previous_Time=strptime(Previous_Time, "%Y%m%dT%H%M%S%6N")  | rex mode=sed field=New_Time "s/(\W)//g"|  eval New_Time=strptime(New_Time, "%Y%m%dT%H%M%S%6N")  | eval diff=New_Time-Previous_Time | eval diff=tostring(diff, "duration") | eval New_Time=strftime(New_Time, "%Y-%m-%dT%H:%M:%S.%6N") | eval Previous_Time=strftime(Previous_Time, "%Y-%m-%dT%H:%M:%S.%6N")
0 Karma

rjthibod
Champion

From https://answers.splunk.com/answers/180660/how-to-convert-a-timestamp-field-to-epoch-format.html

First extract the timestamp into a field if it is not already set as the timestamp _time.

Then add the following command where you substitute your field name

... | convert timeformat="%Y-%m-%dT%H:%M:%S.%9NZ" mktime("yourfieldname")

0 Karma

kiran331
Builder

Its the field values, I get from the event

Previous_Time - ‎2016‎-‎12‎-‎01T15:34:37.658562500Z

New_Time - ‎2016‎-‎12‎-‎01T15:36:13.345154500Z

I have to find the difference b/w these times

0 Karma

rjthibod
Champion

OK, try this to get the difference in raw seconds.

 ... | convert timeformat="%Y-%m-%dT%H:%M:%S.%9NZ" mktime("Previous_Time") as previousepoch mktime("New_Time") as newepoch | eval difftime = newepoch - previousepoch
0 Karma

kiran331
Builder

I tried, its not working

0 Karma

rjthibod
Champion

What is it outputting? Are the new fields newepoch and previousepoch being generated at all?

0 Karma

shivarpith
Path Finder

extract the field using regex ( if its not the timestamp of the log) and you can try strptime and strftime to strip and form the timestamps

|eval time=strptime(yourfiled,"%H:%M:%S.%N") note you can use number to limit the milli seconds ( ex %3N gives 3 decimal values)

once done you can calculate the difference and form the time afterwards

| eval calculatedtime=strftime(yourfiled,"%H:%M:%S.%N")
Get Updates on the Splunk Community!

Updated Team Landing Page in Splunk Observability

We’re making some changes to the team landing page in Splunk Observability, based on your feedback. The ...

New! Splunk Observability Search Enhancements for Splunk APM Services/Traces and ...

Regardless of where you are in Splunk Observability, you can search for relevant APM targets including service ...

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...