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!

Best Practices: Splunk auto adjust pipeline queue

When you enable autoAdjustQueue in Splunk, maxSize should be understood as the queue size Splunk starts with ...

Introducing the 2026 - 2027 SplunkTrust cohort!

The goal of the SplunkTrust™ membership has historically been to acknowledge and recognize those who go above ...

Splunk Auto Ingestion Parallel Pipeline Scaling

Why this feature matters Many Splunk environments experience ingestion pressure long before the host is fully ...