Splunk Search

Subtracting two timestamps

guimilare
Communicator

Hi Splunkers.

I have one issue about subtracting two timestamps.
I have the following fields:

start=20150917 18:28:32.460
end=20150917 18:28:32:500

I tried something like this:

<mysearch> |  eval result_field=end-start

but no lucky.

I need the output as result_field=00:00:00.040.

Can you guys help me?
Thank you!
Best regards.

0 Karma
1 Solution

somesoni2
Revered Legend

Try this

<Your search> | eval result_field=tostring(strptime(end,"%Y%m%d %H:%M:%S.%N")-strptime(start,"%Y%m%d %H:%M:%S.%N"),"duration")

Updated per new req

<Your search > | eval result_field=strptime(end,"%Y%m%d %H:%M:%S.%N")-strptime(start,"%Y%m%d %H:%M:%S.%N") | stats avg(result_field) as result_field | eval result_field=tostring(result_field,"duration")

View solution in original post

sunilsk1
Path Finder

It still not working for . i have this sample log entry where boot-time for each machine in ENV are reported
I have 2016-05,ENV,R1,W,05/25/16 21:00:00.000,05/26/16 02:00:00.000,05/25/16 23:09:00.000,05/26/16 05:12:00.000,183

Where my "Scheduled End Time" : 05/26/16 02:00:00.000
"Actual End Time" : 05/26/18 05:12:00.000
I wanted the difference in minutes between Actual End Time and Scheduled End time. (192 minutes)

I have tried this approach
{search}|eval ac_end='Actual End Time'|convert mktime(ac_end) as ac_time timeformat="%Y-%m-%d %H:%M:%S"

0 Karma

DavidHourani
Super Champion

Hey,

What you're looking for is a tostring conversion.

Simply use the following:

 <mysearch> |  eval result_field=end-start | eval result_field = tostring(result_field,"duration")

Or simply:

<mysearch> |  eval result_field = tostring(end-start,"duration")

Let me know how that works out for you!

Cheers,
David

0 Karma

somesoni2
Revered Legend

Try this

<Your search> | eval result_field=tostring(strptime(end,"%Y%m%d %H:%M:%S.%N")-strptime(start,"%Y%m%d %H:%M:%S.%N"),"duration")

Updated per new req

<Your search > | eval result_field=strptime(end,"%Y%m%d %H:%M:%S.%N")-strptime(start,"%Y%m%d %H:%M:%S.%N") | stats avg(result_field) as result_field | eval result_field=tostring(result_field,"duration")

guimilare
Communicator

Hi Somesoni2,
this appears to work!
How can I get an average from result_field?
I tried to do

 <Your search> | eval result_field=tostring(strptime(end,"%Y%m%d %H:%M:%S.%N")-strptime(start,"%Y%m%d %H:%M:%S.%N"),"duration") | avg(result_field) as Average

but no success.
Regards

0 Karma

somesoni2
Revered Legend

The above query generates a string field with the format you requested, so can't get the avg. Try the updated query.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

SOC4Kafka - New Kafka Connector Powered by OpenTelemetry

The new SOC4Kafka connector, built on OpenTelemetry, enables the collection of Kafka messages and forwards ...

Event Series: Level up your SOC: Advancing with Splunk Enterprise Security

AI has fundamentally raised the stakes for security operations, and this three-part series is your guide to ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...