Splunk Dev

find the difference between two date/time values

krishnakishoreg
Engager

Hi,

I need small help to build a query to find the difference between two date/time values of a log in table format. For example in_time=2013-12-11T22:58:50.797 and out_time=2013-12-11T22:58:51.023.

tried this query but i didn't get the result.
| eval otime=out_time| eval itime=in_time | eval TimeDiff=otime-itime | table out_time in_time TimeDiff

Labels (1)
Tags (1)

kristian_kolb
Ultra Champion

You cannot compare times that way. You'll need to convert them to epoch first. If one of those timestamps are already being used as the timestamp for the event, then the conversion has already been made for that timestamp, and it is availible in the _time field. Otherwise you'll need to do the following;

your_base_search 
| eval it = strptime(in_time, "%Y-%m-%dT%H:%M:%S.%3N") 
| eval ot = strptime(out_time, "%Y-%m-%dT%H:%M:%S.%3N") 
| eval diff = tostring((ot - it), "duration") 
| table in_time, out_time, diff 

read more here;

http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/CommonEvalFunctions
http://en.wikipedia.org/wiki/Unix_epoch
http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Commontimeformatvariables

/K

nagarajsf
Explorer

Hi,
I'm in the same scenario, but trying to get the difference from CREATED_DATE and current timestamp. For that, it is not working.

    base_search  
| eval it = strptime(CREATED_DATE, "%Y-%m-%d %H:%M:%S") 
| eval nowstring=strptime(now(), "%Y-%m-%d %H:%M:%S") 
| eval ticket_duration=tostring((now() - it), "duration" )  
| table DESCRIPTION,CREATED_DATE,TICKET_STATUS,UPDATE_DATE, ticket_duration

base_search  
| convert timeformat='%Y-%m-%dT%H:%M:%S' mktime(CREATED_DATE) mktime(now()  AS  _now) 
| eval duration=(_now-CREATED_DATE)/86400 
|table TTID,MANAGER_NAME,SEVERITY,DESCRIPTION,CREATED_DATE,TICKET_STATUS,UPDATE_DATE, duration

In both ways I'm getting null value, ticket_duration=null
Can you please suggest any?

Thanks,

0 Karma

aszewczyk
Engager

In case anyone was scratching their head like me, the time formats should be:

"%Y-%m-%d %H:%M:%S.%3N"

There should be spaces where the 'T's are.

kristian_kolb
Ultra Champion

feel free to mark the question as answered a/o upvote if it solved your problem. Thanks, K

0 Karma

krishnakishoreg
Engager

Thanks a lot, by doing some change query worked.

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...