Splunk Search

eval Time difference

lpolo
Motivator

I have the following data indexed:

initialTime     Purchase_Time
2011-11-04T13:17Z   2011-11-04 09:18:20
2011-11-04T21:08Z   2011-11-04 14:08:28
2011-11-04T15:16Z   2011-11-04 16:38:16
2011-11-04T15:15Z   2011-11-04 10:18:12
2011-11-04T21:25Z   2011-11-04 14:26:40
2011-11-04T13:35Z   2011-11-04 09:36:45
2011-11-04T20:01Z   2011-11-04 16:03:02
2011-11-04T19:31Z   2011-11-04 20:28:55

I tried to get the following result set with a splunk query:

|eval Time_Diff = initialTime - Purchase_Time

But I have not been able to...

Thanks.

Tags (2)

eelisio2
Path Finder

This should give you the difference in seconds.

| eval itime=strptime(initialTime,"%Y-%m-%dT%H:%M") | eval ptime=strptime(PurchaseTime,"%Y-%m-%d %H:%M:%S") | eval TimeDiff=itime-ptime  | table initialTime PurchaseTime TimeDiff

dwaddle
SplunkTrust
SplunkTrust

Splunk (by default) parses out the first timestamp it sees from an event (well, it could be a different timestamp if you configure it this way) and stores it in time_t format as the field _time. But, it does not do this by default with any additional timestamp-looking data within the event.

0 Karma

dwaddle
SplunkTrust
SplunkTrust

Ipolo, the reason this works where your initial approach didn't is because the example field values from your search are just strings. Splunk doesn't know how to subtract them and make sense of them. What eelisio is doing is converting the timestamp strings to time_t values (that is, the number of seconds since 1/1/1970 00:00:00 UTC). The time_t format (sometimes called epoch time) is really awesome because you can store large time ranges in simple 32-bit values, and because you can easily figure out the delta between any two timestamps via addition/subtraction.

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!

Fuel Your Journey: What’s Waiting for You at the .conf26 Acceleration Station

Navigating the show floor at .conf26 isn't just about keynotes and technical breakout sessions; it's also ...

Join the Final Session of the Data Management & Federation Bootcamp Series

Over the past three sessions of the Data Management & Federation Bootcamp Series, we've explored how to build ...

From Data to Insight: Announcing the Winners of the Splunk Dashboard Contest

Hi Splunkers, First off, thank you to everyone who participated in our very first From Data to Insight: The ...