<?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: How To Get Datetime Difference? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164619#M33382</link>
    <description>&lt;P&gt;So doing a &lt;CODE&gt;strftime&lt;/CODE&gt; on TEMPO_EXECUCAO doesn't make sense because it's not a timestamp.  And why are you fieldformatting the other fields twice?  That's entirely redundant.  &lt;/P&gt;

&lt;P&gt;I was able to generate the output you're looking for via sed replacement:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | head 1 | eval DATA_INICIO="26/02/2015-04:07:06" | eval DATA_FIM="26/02/2015-05:01:43" | table DATA_INICIO DATA_FIM 
 | eval DATA_INICIO=strptime( DATA_INICIO, "%d/%m/%Y-%T") 
 | eval DATA_FIM=strptime( DATA_FIM, "%d/%m/%Y-%T") 
 | eval TEMPO_EXECUCAO=DATA_FIM-DATA_INICIO 
 | table DATA_INICIO DATA_FIM TEMPO_EXECUCAO
 | fieldformat DATA_INICIO=strftime(DATA_INICIO, "%d/%m/%Y-%T")
 | fieldformat DATA_FIM=strftime(DATA_FIM, "%d/%m/%Y-%T")
 | fieldformat TEMPO_EXECUCAO=replace(tostring(TEMPO_EXECUCAO, "duration"),"^(.*)(\d{2}:\d{2}:\d{2})(.*)","\2")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 06 Mar 2015 19:53:59 GMT</pubDate>
    <dc:creator>emiller42</dc:creator>
    <dc:date>2015-03-06T19:53:59Z</dc:date>
    <item>
      <title>How To Get Datetime Difference?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164612#M33375</link>
      <description>&lt;P&gt;Hello guys, can anyone help me to get the lenght of each operation?&lt;BR /&gt;
I have start datetime and end datetime, both are in brazil timestamp Day/Month/Yeah-Hour:Minutes:Seconds.&lt;BR /&gt;
How can I do that?&lt;/P&gt;

&lt;P&gt;Follows a piece of sample:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=full sourcetype=temp DATA_INICIO=* DATA_FIM=* 
| eval TEMPO_EXECUCAO = (DATA_FIM - DATA_INICIO)
| table DATA_INICIO DATA_FIM TEMPO_EXECUCAO

DATA_INICIO                   DATA_FIM                              TEMPO_EXECUCAO
26/02/2015-04:07:06             26/02/2015-05:01:43  
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2015 15:07:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164612#M33375</guid>
      <dc:creator>vtsguerrero</dc:creator>
      <dc:date>2015-03-05T15:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: How To Get Datetime Difference?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164613#M33376</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;

&lt;P&gt;You need to get your time to epoch, then calculate the difference, and then convert it back to human format. Something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... your base search | eval DATA_FIM_EPOCH = strptime(DATA_INICIO,"%d/%m/%Y-%H:%M:%S") | eval DATA_FIM_EPOCH = strptime(DATA_FIM,"%d/%m/%Y-%H:%M:%S") | eval difference_epoch = DATA_FIM_EPOCH - DATA_FIM_EPOCH | eval TEMPO_EXECUCAO = tostring(difference_epoch,"duration") | table table DATA_INICIO DATA_FIM TEMPO_EXECUCAO
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;regards&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2015 15:25:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164613#M33376</guid>
      <dc:creator>gfuente</dc:creator>
      <dc:date>2015-03-05T15:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: How To Get Datetime Difference?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164614#M33377</link>
      <description>&lt;P&gt;I bet the problem is that your fields are being treated as strings and not timestamps.  You can correct this with &lt;CODE&gt;eval&lt;/CODE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=full sourcetype=temp DATA_INICIO=* DATA_FIM=*
| eval DATA_INICIO=strptime( DATA_INICIO, "%d/%m/%Y-%T") 
| eval DATA_FIM=strptime( DATA_FIM, "%d/%m/%Y-%T") 
| eval TEMPO_EXECUCAO=DATA_FIM-DATA_INICIO 
| table DATA_INICIO DATA_FIM TEMPO_EXECUCAO
| fieldformat DATA_INICIO=strftime(DATA_INICIO, "%d/%m/%Y-%T")
| fieldformat DATA_FIM=strftime(DATA_FIM, "%d/%m/%Y-%T")
| fieldformat TEMPO_EXECUCAO=tostring(TEMPO_EXECUCAO, "duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;the fieldformat lines aren't strictly necessary, but make the output more readable.  (With the exception of _time, all timestamp and duration values are displayed as decimals) &lt;/P&gt;

&lt;P&gt;Relevant links:&lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/CommonEvalFunctions"&gt;Functions for Eval and Where (search for strptime/strftime)&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fieldformat"&gt;Fieldformat&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;I successfully tested the above by using the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | head 1 | eval DATA_INICIO="26/02/2015-04:07:06" | eval DATA_FIM="26/02/2015-05:01:43" | table DATA_INICIO DATA_FIM 
| eval DATA_INICIO=strptime( DATA_INICIO, "%d/%m/%Y-%T") 
| eval DATA_FIM=strptime( DATA_FIM, "%d/%m/%Y-%T") 
| eval TEMPO_EXECUCAO=DATA_FIM-DATA_INICIO 
| table DATA_INICIO DATA_FIM TEMPO_EXECUCAO
| fieldformat DATA_INICIO=strftime(DATA_INICIO, "%d/%m/%Y-%T")
| fieldformat DATA_FIM=strftime(DATA_FIM, "%d/%m/%Y-%T")
| fieldformat TEMPO_EXECUCAO=tostring(TEMPO_EXECUCAO, "duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;DATA_INICIO         DATA_FIM               TEMPO_EXECUCAO
26/02/2015-04:07:06 26/02/2015-05:01:43 00:54:37.000000
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2015 15:29:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164614#M33377</guid>
      <dc:creator>emiller42</dc:creator>
      <dc:date>2015-03-05T15:29:54Z</dc:date>
    </item>
    <item>
      <title>Re: How To Get Datetime Difference?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164615#M33378</link>
      <description>&lt;P&gt;Convert them to &lt;CODE&gt;time_t&lt;/CODE&gt; values using the &lt;CODE&gt;eval&lt;/CODE&gt; command and its &lt;CODE&gt;strptime&lt;/CODE&gt; function.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=full sourcetype=temp DATA_INICIO=* DATA_FIM=* 
 | eval DATA_FIM_t = strptime(DATA_FIM,"%d/%m/%Y-%H:%M:%S")
 | eval DATA_INICIO_t = strptime(DATA_INICIO,"%d/%m/%Y-%H:%M:%S")
 | eval TEMPO_EXECUCAO_t = (DATA_FIM_t - DATA_INICIO_t)
 | eval TEMPO_EXECUCAO = tostring(TEMPO_EXECUCAO_t,"duration")
 | table DATA_INICIO DATA_FIM TEMPO_EXECUCAO
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2015 15:30:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164615#M33378</guid>
      <dc:creator>dwaddle</dc:creator>
      <dc:date>2015-03-05T15:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: How To Get Datetime Difference?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164616#M33379</link>
      <description>&lt;P&gt;Well, I don't know what happened with the conversion, but still doesn't show anything here, look:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=* sourcetype=* DATA_INICIO=* DATA_FIM=* 
| eval DATA_INICIO_EPOCH= strptime(DATA_INICIO,"%d/%m/%Y-%H:%M:%S") 
| eval DATA_FIM_EPOCH = strptime(DATA_FIM,"%d/%m/%Y-%H:%M:%S") 
| eval TEMPO_EXECUCAO = (DATA_FIM_EPOCH - DATA_INICIO_EPOCH)
| eval TEMPO_EXECUCAO = strftime(difference_epoch,"%d/%m/%Y-%H:%M:%S") 
| table DATA_INICIO DATA_FIM TEMPO_EXECUCAO
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Shows this table:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; DATA_INICIO                      DATA_FIM                                TEMPO_EXECUCAO
 26/02/2015-04:07:06             26/02/2015-05:01:43     
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Mar 2015 15:32:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164616#M33379</guid>
      <dc:creator>vtsguerrero</dc:creator>
      <dc:date>2015-03-05T15:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: How To Get Datetime Difference?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164617#M33380</link>
      <description>&lt;P&gt;Thanks a lot @emiller42!&lt;BR /&gt;
Worked pretty well now...&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2015 15:37:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164617#M33380</guid>
      <dc:creator>vtsguerrero</dc:creator>
      <dc:date>2015-03-05T15:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: How To Get Datetime Difference?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164618#M33381</link>
      <description>&lt;P&gt;Hello! Just a last question...&lt;BR /&gt;
How would I make this shows only time difference in Hours:Minutes:Seconds ( 2 characters at most for each ) example&lt;BR /&gt;
"Lenght = 01:32:18"&lt;/P&gt;

&lt;P&gt;It's is currently this way:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=full sourcetype=temp DATA_INICIO=* DATA_FIM=*  
| eval DATA_INICIO=strptime( DATA_INICIO, "%d/%m/%Y-%T")   
| eval DATA_FIM=strptime( DATA_FIM, "%d/%m/%Y-%T")   
| eval TEMPO_EXECUCAO=DATA_FIM-DATA_INICIO   
| table DATA_INICIO DATA_FIM TEMPO_EXECUCAO  
| fieldformat DATA_INICIO=strftime(DATA_INICIO, "%d/%m/%Y-%T")  
| fieldformat DATA_FIM=strftime(DATA_FIM, "%d/%m/%Y-%T")  
| fieldformat TEMPO_EXECUCAO=tostring(TEMPO_EXECUCAO, "duration")  
| fieldformat DATA_INICIO=strftime(DATA_INICIO, "%d/%m/%Y-%T")  
| fieldformat DATA_FIM=strftime(DATA_FIM, "%d/%m/%Y-%T")  
| fieldformat TEMPO_EXECUCAO=tostring(TEMPO_EXECUCAO, "duration")
| fieldformat TEMPO_EXECUCAO=strftime(TEMPO_EXECUCAO, "%d/%m/%Y-%T")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Mar 2015 13:34:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164618#M33381</guid>
      <dc:creator>vtsguerrero</dc:creator>
      <dc:date>2015-03-06T13:34:37Z</dc:date>
    </item>
    <item>
      <title>Re: How To Get Datetime Difference?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164619#M33382</link>
      <description>&lt;P&gt;So doing a &lt;CODE&gt;strftime&lt;/CODE&gt; on TEMPO_EXECUCAO doesn't make sense because it's not a timestamp.  And why are you fieldformatting the other fields twice?  That's entirely redundant.  &lt;/P&gt;

&lt;P&gt;I was able to generate the output you're looking for via sed replacement:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | head 1 | eval DATA_INICIO="26/02/2015-04:07:06" | eval DATA_FIM="26/02/2015-05:01:43" | table DATA_INICIO DATA_FIM 
 | eval DATA_INICIO=strptime( DATA_INICIO, "%d/%m/%Y-%T") 
 | eval DATA_FIM=strptime( DATA_FIM, "%d/%m/%Y-%T") 
 | eval TEMPO_EXECUCAO=DATA_FIM-DATA_INICIO 
 | table DATA_INICIO DATA_FIM TEMPO_EXECUCAO
 | fieldformat DATA_INICIO=strftime(DATA_INICIO, "%d/%m/%Y-%T")
 | fieldformat DATA_FIM=strftime(DATA_FIM, "%d/%m/%Y-%T")
 | fieldformat TEMPO_EXECUCAO=replace(tostring(TEMPO_EXECUCAO, "duration"),"^(.*)(\d{2}:\d{2}:\d{2})(.*)","\2")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Mar 2015 19:53:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-To-Get-Datetime-Difference/m-p/164619#M33382</guid>
      <dc:creator>emiller42</dc:creator>
      <dc:date>2015-03-06T19:53:59Z</dc:date>
    </item>
  </channel>
</rss>

