<?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 can I find the time duration between two fields? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372494#M109556</link>
    <description>&lt;P&gt;@locose -  &lt;/P&gt;

&lt;P&gt;First, the difference between &lt;CODE&gt;strftime&lt;/CODE&gt; and &lt;CODE&gt;strptime&lt;/CODE&gt; is &lt;CODE&gt;f&lt;/CODE&gt; for &lt;CODE&gt;F&lt;/CODE&gt;ORMAT, &lt;CODE&gt;p&lt;/CODE&gt; for &lt;CODE&gt;P&lt;/CODE&gt;ULL.  &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;strftime&lt;/CODE&gt; takes data that is in epoch form, and &lt;CODE&gt;f&lt;/CODE&gt;ormats it &lt;CODE&gt;f&lt;/CODE&gt;orward to human-readable &lt;CODE&gt;f&lt;/CODE&gt;orm.  &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;strptime&lt;/CODE&gt; takes time data that is formatted for display, and strips ( &lt;CODE&gt;strp&lt;/CODE&gt;s) it back into epoch time, &lt;CODE&gt;p&lt;/CODE&gt;erfect for &lt;CODE&gt;p&lt;/CODE&gt;erforming &lt;CODE&gt;p&lt;/CODE&gt;roductive calculations.  In this case, you want &lt;CODE&gt;strptime&lt;/CODE&gt;, as @3no said.&lt;/P&gt;

&lt;P&gt;Second, whichever direction you are going, each piece of the display format needs to be exactly right.  &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;%y&lt;/CODE&gt; is 2-digit year, &lt;CODE&gt;%Y&lt;/CODE&gt; is 4-digit year.  &lt;/P&gt;

&lt;P&gt;Also, both &lt;CODE&gt;%N&lt;/CODE&gt; and &lt;CODE&gt;%Q&lt;/CODE&gt;  are for sub-second components, and one defaults to 3 digits, the other to 6 digits.  Since you have exactly one digit, neither default will work and you must specify the 1 -  &lt;CODE&gt;%1Q&lt;/CODE&gt; or &lt;CODE&gt;%1N&lt;/CODE&gt; are fine.  Also, there is a period &lt;CODE&gt;.&lt;/CODE&gt; between seconds and sub-seconds in your fields, not a colon &lt;CODE&gt;:&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;So, to properly extract your times...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Starttime=strftime(START_TS,"%y-%m-%d %H:%M:%S:%N")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...should be...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Starttime=strptime(START_TS,"%Y-%m-%d %H:%M:%S.%1N")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;... and then when you subtract the two, your difference in epoch time will read out as the number of seconds between the two times.&lt;/P&gt;</description>
    <pubDate>Thu, 17 Aug 2017 14:51:24 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2017-08-17T14:51:24Z</dc:date>
    <item>
      <title>How can I find the time duration between two fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372490#M109552</link>
      <description>&lt;P&gt;Trying to find the time duration between 2 fields&lt;/P&gt;

&lt;P&gt;Field name :  START_TS&lt;BR /&gt;
2017-08-16 04:07:00.0&lt;/P&gt;

&lt;P&gt;Field name : END_TS&lt;BR /&gt;
2017-08-16 04:12:00.0&lt;/P&gt;

&lt;P&gt;I tried something like....&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;my search query  | eval Starttime=strftime(START_TS,"%y-%m-%d %H:%M:%S:%N")
 | eval Endtime=strftime(END_TS,"%y-%m-%d %H:%M:%S:%N")
 |eval duration = Endtime - Starttime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But it didn't work. &lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2017 14:07:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372490#M109552</guid>
      <dc:creator>locose</dc:creator>
      <dc:date>2017-08-17T14:07:30Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the time duration between two fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372491#M109553</link>
      <description>&lt;P&gt;Hey locose, &lt;/P&gt;

&lt;P&gt;Try with &lt;CODE&gt;strptime&lt;/CODE&gt; instead of &lt;CODE&gt;strftime&lt;/CODE&gt;. &lt;/P&gt;

&lt;P&gt;If it doesn't work try to change the name of your variable because starttime and endtime are already used by splunk (and I'm not sure about how it react about that):&lt;/P&gt;

&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/SplunkCloud/6.6.1/SearchReference/SearchTimeModifiers"&gt;https://docs.splunk.com/Documentation/SplunkCloud/6.6.1/SearchReference/SearchTimeModifiers&lt;/A&gt; &lt;/P&gt;

&lt;P&gt;3no. &lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2017 14:19:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372491#M109553</guid>
      <dc:creator>3no</dc:creator>
      <dc:date>2017-08-17T14:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the time duration between two fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372492#M109554</link>
      <description>&lt;P&gt;@3no - splunk is case-sensitive in field names, so your comment about starttime and endtime is not part of the issue.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2017 14:36:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372492#M109554</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-08-17T14:36:45Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the time duration between two fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372493#M109555</link>
      <description>&lt;P&gt;didnt work. Tried&lt;/P&gt;

&lt;P&gt;| eval starthere_time=strptime(START_TS,"%y-%m-%d %H:%M:%S:%N")&lt;BR /&gt;
 | eval endhere_time=strptime(END_TS,"%y-%m-%d %H:%M:%S:%N")&lt;BR /&gt;
 |eval duration = endhere_time - starthere_time&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 15:23:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372493#M109555</guid>
      <dc:creator>locose</dc:creator>
      <dc:date>2020-09-29T15:23:21Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the time duration between two fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372494#M109556</link>
      <description>&lt;P&gt;@locose -  &lt;/P&gt;

&lt;P&gt;First, the difference between &lt;CODE&gt;strftime&lt;/CODE&gt; and &lt;CODE&gt;strptime&lt;/CODE&gt; is &lt;CODE&gt;f&lt;/CODE&gt; for &lt;CODE&gt;F&lt;/CODE&gt;ORMAT, &lt;CODE&gt;p&lt;/CODE&gt; for &lt;CODE&gt;P&lt;/CODE&gt;ULL.  &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;strftime&lt;/CODE&gt; takes data that is in epoch form, and &lt;CODE&gt;f&lt;/CODE&gt;ormats it &lt;CODE&gt;f&lt;/CODE&gt;orward to human-readable &lt;CODE&gt;f&lt;/CODE&gt;orm.  &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;strptime&lt;/CODE&gt; takes time data that is formatted for display, and strips ( &lt;CODE&gt;strp&lt;/CODE&gt;s) it back into epoch time, &lt;CODE&gt;p&lt;/CODE&gt;erfect for &lt;CODE&gt;p&lt;/CODE&gt;erforming &lt;CODE&gt;p&lt;/CODE&gt;roductive calculations.  In this case, you want &lt;CODE&gt;strptime&lt;/CODE&gt;, as @3no said.&lt;/P&gt;

&lt;P&gt;Second, whichever direction you are going, each piece of the display format needs to be exactly right.  &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;%y&lt;/CODE&gt; is 2-digit year, &lt;CODE&gt;%Y&lt;/CODE&gt; is 4-digit year.  &lt;/P&gt;

&lt;P&gt;Also, both &lt;CODE&gt;%N&lt;/CODE&gt; and &lt;CODE&gt;%Q&lt;/CODE&gt;  are for sub-second components, and one defaults to 3 digits, the other to 6 digits.  Since you have exactly one digit, neither default will work and you must specify the 1 -  &lt;CODE&gt;%1Q&lt;/CODE&gt; or &lt;CODE&gt;%1N&lt;/CODE&gt; are fine.  Also, there is a period &lt;CODE&gt;.&lt;/CODE&gt; between seconds and sub-seconds in your fields, not a colon &lt;CODE&gt;:&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;So, to properly extract your times...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Starttime=strftime(START_TS,"%y-%m-%d %H:%M:%S:%N")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...should be...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Starttime=strptime(START_TS,"%Y-%m-%d %H:%M:%S.%1N")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;... and then when you subtract the two, your difference in epoch time will read out as the number of seconds between the two times.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2017 14:51:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372494#M109556</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-08-17T14:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the time duration between two fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372495#M109557</link>
      <description>&lt;P&gt;Ok, &lt;/P&gt;

&lt;P&gt;let's give this a try, then : &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;my search query 
| convert timeformat="%Y-%m-%d %H:%M:%S" mktime("START_TS")
| convert timeformat="%Y-%m-%d %H:%M:%S" mktime("END_TS")
| eval duration = END_TS - START_TS
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;3no &lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2017 14:57:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372495#M109557</guid>
      <dc:creator>3no</dc:creator>
      <dc:date>2017-08-17T14:57:08Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the time duration between two fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372496#M109558</link>
      <description>&lt;P&gt;I found the problem(s):&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;"Year" has to be a capital "Y", instead of lowercase.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Before "%N", you have a colon, instead of a period.&lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Here's my working search:&lt;/P&gt;

&lt;P&gt;| makeresults | eval START_TS="2017-08-16 04:07:00.0" | eval END_TS="2017-08-16 04:12:00.0" | eval st = strptime(START_TS, "%Y-%m-%d %H:%M:%S.%N") | eval et = strptime(END_TS, "%Y-%m-%d %H:%M:%S.%N") | eval diff = et - st | eval dur = tostring(diff, "duration")&lt;/P&gt;

&lt;P&gt;More info on the date variables can be found here: &lt;A href="https://docs.splunk.com/Documentation/Splunk/6.6.1/SearchReference/Commontimeformatvariables" target="_blank"&gt;https://docs.splunk.com/Documentation/Splunk/6.6.1/SearchReference/Commontimeformatvariables&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 15:23:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372496#M109558</guid>
      <dc:creator>myriadic</dc:creator>
      <dc:date>2020-09-29T15:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: How can I find the time duration between two fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372497#M109559</link>
      <description>&lt;P&gt;@3no - this should work, but you need to account for the ".%1Q" in the original field.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2017 15:43:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-find-the-time-duration-between-two-fields/m-p/372497#M109559</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-08-17T15:43:28Z</dc:date>
    </item>
  </channel>
</rss>

