<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculate Difference between two time fields in single event in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246650#M73540</link>
    <description>&lt;P&gt;Hi lguinn,&lt;/P&gt;

&lt;P&gt;Thanks for replying!&lt;/P&gt;

&lt;P&gt;the provided query didn't work, its through an error invalid number. Below are my created and last access time format from event.&lt;/P&gt;

&lt;P&gt;Could you please let me how convert these times and get the difference?&lt;/P&gt;

&lt;P&gt;CREATED_TIME                      LAST_ACCESS_TIME&lt;BR /&gt;
2016-11-24 16:08:55.0             2016-11-24 16:08:58.0&lt;BR /&gt;
2016-11-24 16:07:38.0             2016-11-24 16:07:38.0&lt;BR /&gt;
2016-11-24 16:07:05.0             2016-11-24 16:07:06.0&lt;BR /&gt;
2016-11-24 16:06:54.0             2016-11-24 16:06:57.0&lt;BR /&gt;
2016-11-24 16:05:05.0             2016-11-24 16:05:06.0&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 11:55:52 GMT</pubDate>
    <dc:creator>kpavan</dc:creator>
    <dc:date>2020-09-29T11:55:52Z</dc:date>
    <item>
      <title>Calculate Difference between two time fields in single event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246648#M73538</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;

&lt;P&gt;Am trying to calculate difference between starttime and endtime for tasksession, both start and end time are in single event like&lt;BR /&gt;
TASKNAME CREATED_TIME LAST_ACCESS_TIME,  but using two different query unable to get the expected result 1st query difference is null and second query difference is all 00:00. Not sure where is missing. &lt;/P&gt;

&lt;P&gt;Could you please help me on getting correct difference values.&lt;/P&gt;

&lt;P&gt;1st Query : Result is NULL&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search 
|  eval StartTime=strftime(strptime(CREATED_TIME, "%Y-%m-%d %H:%M:%S"),"%Y/%m/%d %H:%M:%S") 
| eval EndTime=strftime(strptime(LAST_ACCESS_TIME, "%Y-%m-%d %H:%M:%S"),"%Y/%m/%d %H:%M:%S") 
| eval Diff=tostring(EndTime-StartTime,"duration") | table TASKNAME StartTime EndTime Diff
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;BR /&gt;
TASKNAME NAME   StartTime   EndTime Diff&lt;BR /&gt;
Registration    2016/11/24 16:08:55 2016/11/24 16:08:58    (NULL)&lt;/P&gt;

&lt;P&gt;2nd Query : Result is 00:00&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=cfa_prod_ca_idm_db source=cfa_idm_all_tasks sourcetype=cfa_idm_tasks STATE=128
| dedup TASKSESSIONID 
| eval StartTime=strftime(strptime(CREATED_TIME, "%Y-%m-%d %H:%M:%S"),"%Y/%m/%d %H:%M:%S") 
| eval EndTime=strftime(strptime(LAST_ACCESS_TIME, "%Y-%m-%d %H:%M:%S"),"%Y/%m/%d %H:%M:%S") 
| transaction TASKNAME maxevents=1
| eval Duration=strftime(duration,"%M:%S") | table TASKNAME StartTime EndTime Duration
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;BR /&gt;
TASKNAME    StartTime   EndTime Duration&lt;BR /&gt;
Registration    2016/11/24 16:08:55 2016/11/24 16:08:58 00:00&lt;/P&gt;

&lt;P&gt;Expected Duration : 00:03 but showing 00:00&lt;/P&gt;

&lt;P&gt;Thanks!&lt;BR /&gt;
Pavan&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 11:55:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246648#M73538</guid>
      <dc:creator>kpavan</dc:creator>
      <dc:date>2020-09-29T11:55:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Difference between two time fields in single event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246649#M73539</link>
      <description>&lt;P&gt;Since you aren't showing the original events, I can't be sure,  but I think that you are parsing the time incorrectly.&lt;BR /&gt;
If  CREATED_TIME and LAST_ACCESS_TIME are formatted in epoch time, then you don't need the strftime/strptime at all. If these fields are formatted as text ("Nov 24 2016 11:39"), then you only need the strptime to convert them into epoch time. The transaction command with maxevents=1 is useless; you need to remove it.&lt;/P&gt;

&lt;P&gt;I would do this, assuming that the time fields appear as text in the event:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; search 
 | eval StartTime=strptime(CREATED_TIME, "%Y-%m-%d %H:%M:%S")
 | eval EndTime=strptime(LAST_ACCESS_TIME, "%Y-%m-%d %H:%M:%S")
 | eval Diff=tostring(EndTime-StartTime,"duration") 
 | table TASKNAME StartTime EndTime Diff CREATED_TIME LAST_ACCESS_TIME
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Once you are happy with the results, you can rename the fields or whatever. For the second search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=cfa_prod_ca_idm_db source=cfa_idm_all_tasks sourcetype=cfa_idm_tasks STATE=128  
| dedup TASKSESSIONID   
| eval StartTime=strptime(CREATED_TIME,"%Y-%m-%d %H:%M:%S")  
| eval EndTime=strptime(LAST_ACCESS_TIME,"%Y-%m-%d %H:%M:%S") 
| eval Duration=strftime(EndTime-StartTime,"%M:%S")
| table TASKNAME StartTime EndTime Duration CREATED_TIME LAST_ACCESS_TIME
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If this doesn't work, please show an example event containing CREATED_TIME and LAST_ACCESS_TIME&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 11:55:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246649#M73539</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2020-09-29T11:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Difference between two time fields in single event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246650#M73540</link>
      <description>&lt;P&gt;Hi lguinn,&lt;/P&gt;

&lt;P&gt;Thanks for replying!&lt;/P&gt;

&lt;P&gt;the provided query didn't work, its through an error invalid number. Below are my created and last access time format from event.&lt;/P&gt;

&lt;P&gt;Could you please let me how convert these times and get the difference?&lt;/P&gt;

&lt;P&gt;CREATED_TIME                      LAST_ACCESS_TIME&lt;BR /&gt;
2016-11-24 16:08:55.0             2016-11-24 16:08:58.0&lt;BR /&gt;
2016-11-24 16:07:38.0             2016-11-24 16:07:38.0&lt;BR /&gt;
2016-11-24 16:07:05.0             2016-11-24 16:07:06.0&lt;BR /&gt;
2016-11-24 16:06:54.0             2016-11-24 16:06:57.0&lt;BR /&gt;
2016-11-24 16:05:05.0             2016-11-24 16:05:06.0&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 11:55:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246650#M73540</guid>
      <dc:creator>kpavan</dc:creator>
      <dc:date>2020-09-29T11:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Difference between two time fields in single event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246651#M73541</link>
      <description>&lt;P&gt;hi kpavan,&lt;BR /&gt;
try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | eval StartTime=strptime(CREATED_TIME,"%Y-%m-%d %H:%M:%S.%N")  
 | eval EndTime=strptime(LAST_ACCESS_TIME,"%Y-%m-%d %H:%M:%S.%N") 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;if it continues to give error, try your search deleting a pipe at a time from the end&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=cfa_prod_ca_idm_db source=cfa_idm_all_tasks sourcetype=cfa_idm_tasks STATE=128  
 | dedup TASKSESSIONID   
 | eval StartTime=strptime(CREATED_TIME,"%Y-%m-%d %H:%M:%S.%N")  
 | eval EndTime=strptime(LAST_ACCESS_TIME,"%Y-%m-%d %H:%M:%S.%N")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;to understand what is the command in error.&lt;BR /&gt;
Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Fri, 25 Nov 2016 08:04:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-Difference-between-two-time-fields-in-single-event/m-p/246651#M73541</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2016-11-25T08:04:50Z</dc:date>
    </item>
  </channel>
</rss>

