<?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 How do I convert &amp;quot;3 days 5 hrs 40 min 11 sec&amp;quot; to seconds? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235515#M69921</link>
    <description>&lt;PRE&gt;&lt;CODE&gt;3*86400+5*3600+40*60+11=279611
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The seconds part is always there. The minutes part exists when duration is at least 60 seconds. The hours part exists when duration is at least 60 minutes. The days part exists when duration is at least 24 hours. Knowledge objects have to produce the &lt;STRONG&gt;duration_in_second&lt;/STRONG&gt; field automatically when I search for &lt;CODE&gt;"*"&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Bonus: add months and years&lt;/P&gt;

&lt;P&gt;EDIT1: test case list&lt;BR /&gt;
3 days 5 hrs 40 min 11 sec&lt;BR /&gt;
5 hrs 40 min 11 sec&lt;BR /&gt;
40 min 11 sec&lt;BR /&gt;
11 sec&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 07:55:04 GMT</pubDate>
    <dc:creator>hylam</dc:creator>
    <dc:date>2020-09-29T07:55:04Z</dc:date>
    <item>
      <title>How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235515#M69921</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;3*86400+5*3600+40*60+11=279611
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The seconds part is always there. The minutes part exists when duration is at least 60 seconds. The hours part exists when duration is at least 60 minutes. The days part exists when duration is at least 24 hours. Knowledge objects have to produce the &lt;STRONG&gt;duration_in_second&lt;/STRONG&gt; field automatically when I search for &lt;CODE&gt;"*"&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Bonus: add months and years&lt;/P&gt;

&lt;P&gt;EDIT1: test case list&lt;BR /&gt;
3 days 5 hrs 40 min 11 sec&lt;BR /&gt;
5 hrs 40 min 11 sec&lt;BR /&gt;
40 min 11 sec&lt;BR /&gt;
11 sec&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:55:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235515#M69921</guid>
      <dc:creator>hylam</dc:creator>
      <dc:date>2020-09-29T07:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235516#M69922</link>
      <description>&lt;P&gt;This will give you that duration in seconds &lt;CODE&gt;| gentimes start=-1 | eval d=3*86400+5*3600+40*60+11 | eval d=tostring(d, "duration")&lt;/CODE&gt;. You could set this up as a calculated field.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2015 14:22:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235516#M69922</guid>
      <dc:creator>sundareshr</dc:creator>
      <dc:date>2015-11-14T14:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235517#M69923</link>
      <description>&lt;P&gt;This is NOT what I have asked for. Please convert "3 days 5 hrs 40 min 11 sec" to 279611.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2015 14:44:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235517#M69923</guid>
      <dc:creator>hylam</dc:creator>
      <dc:date>2015-11-14T14:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235518#M69924</link>
      <description>&lt;P&gt;How about this?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;* | head 1 
| eval str="3 days 5 hrs 40 min 11 sec" 
| eval duration_in_seconds=tonumber(replace(str, ".*?(\d+) sec", "\1")) 
+ 60 * coalesce(tonumber(replace(str, ".*?(\d+) min.*", "\1")), 0) 
+ 3600 * coalesce(tonumber(replace(str, ".*?(\d+) hrs.*", "\1")),0) 
+ 86400 * coalesce(tonumber(replace(str, ".*?(\d+) days.*", "\1")),0) 
| table str,duration_in_seconds
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The &lt;CODE&gt;eval duration_...&lt;/CODE&gt; could be used in the definition of a calculated field.&lt;/P&gt;

&lt;P&gt;EDIT:&lt;BR /&gt;
The solution above may not be optimal with regard to performance, because the string has to be parsed four times.&lt;BR /&gt;
The idea behind this was to do it in a single &lt;CODE&gt;eval&lt;/CODE&gt; expression.&lt;/P&gt;

&lt;P&gt;If you don't mind to create additional fields, it may be better to do it in two steps:&lt;/P&gt;

&lt;P&gt;1st step: field extraction:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex field=str "^((?&amp;lt;days&amp;gt;\d+) days )?((?&amp;lt;hours&amp;gt;\d+) hrs )?((?&amp;lt;minutes&amp;gt;\d+) min )?(?&amp;lt;seconds&amp;gt;\d+) sec" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;2nd step: field calculation:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval duration_in_second=if(isnull(days),0,86400*days) 
+ if(isnull(hours),0,3600*hours) 
+ if(isnull(minutes),0,60*minutes) 
+ seconds 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Nov 2015 14:50:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235518#M69924</guid>
      <dc:creator>kurdbahr</dc:creator>
      <dc:date>2015-11-14T14:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235519#M69925</link>
      <description>&lt;P&gt;Sorry mis-read your question &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2015 15:30:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235519#M69925</guid>
      <dc:creator>sundareshr</dc:creator>
      <dc:date>2015-11-14T15:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235520#M69926</link>
      <description>&lt;P&gt;Like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex "(?:(?&amp;lt;numDays&amp;gt;\d+)\s+days\s+)?(?:(?&amp;lt;numHours&amp;gt;\d+)\s+hrs\s+)?(?:(?&amp;lt;numMins&amp;gt;\d+)\s+min\s+)?(?&amp;lt;numSecs&amp;gt;\d+)\s+sec" | eval duration_in_seconds = (((((numDays * 24) + numHours) * 60) + numMins) * 60) + numSecs
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Given this, you should be able to figure out how to create the associated &lt;CODE&gt;KOs&lt;/CODE&gt; but I will help a little on that.  The &lt;CODE&gt;eval&lt;/CODE&gt; part should be a &lt;CODE&gt;calculated field&lt;/CODE&gt;; see here for all of what you need:&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.1/admin/Propsconf"&gt;http://docs.splunk.com/Documentation/Splunk/6.3.1/admin/Propsconf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2015 16:08:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235520#M69926</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-11-14T16:08:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235521#M69927</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;| makeresults count=1 | eval x="3 days 5 hrs 40 min 11 sec" | rex field=x "(?:(?&amp;lt;numDays&amp;gt;\d+)\s+days\s+)?(?:(?&amp;lt;numHours&amp;gt;\d+)\s+hrs\s+)?(?:(?&amp;lt;numMins&amp;gt;\d+)\s+min\s+)?(?&amp;lt;numSecs&amp;gt;\d+)\s+sec" | eval duration_in_seconds = ((((numDays * 24) + numHours) * 60) + numMins) * 60) + numSecs
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Error in 'eval' command: The expression is malformed.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=1 | eval x="5 hrs 40 min 11 sec" | rex field=x "(?:(?&amp;lt;numDays&amp;gt;\d+)\s+days\s+)?(?:(?&amp;lt;numHours&amp;gt;\d+)\s+hrs\s+)?(?:(?&amp;lt;numMins&amp;gt;\d+)\s+min\s+)?(?&amp;lt;numSecs&amp;gt;\d+)\s+sec"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;numDays field is null&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2015 16:27:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235521#M69927</guid>
      <dc:creator>hylam</dc:creator>
      <dc:date>2015-11-14T16:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235522#M69928</link>
      <description>&lt;P&gt;btw how can i make syntax highlight?&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2015 16:28:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235522#M69928</guid>
      <dc:creator>hylam</dc:creator>
      <dc:date>2015-11-14T16:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235523#M69929</link>
      <description>&lt;P&gt;I was missing a single open parenthesis.  I re-edited the answer and it works.  Try it again.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2015 17:01:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235523#M69929</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-11-14T17:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235524#M69930</link>
      <description>&lt;P&gt;Please handle the cases when days, hours or minutes are missing.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2015 17:10:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235524#M69930</guid>
      <dc:creator>hylam</dc:creator>
      <dc:date>2015-11-14T17:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do I convert "3 days 5 hrs 40 min 11 sec" to seconds?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235525#M69931</link>
      <description>&lt;P&gt;OK, try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex "(?:(?&amp;lt;numDays&amp;gt;\d+)\s+days\s+)?(?:(?&amp;lt;numHours&amp;gt;\d+)\s+hrs\s+)?(?:(?&amp;lt;numMins&amp;gt;\d+)\s+min\s+)?(?&amp;lt;numSecs&amp;gt;\d+)\s+sec" | eval duration_in_seconds = (((((if(isnull(numDays),0,numDays) * 24) + if(isnull(numHours),0,numHours)) * 60) + if(isnull(numMins),0,numMins)) * 60) + numSecs
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Nov 2015 18:04:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-convert-quot-3-days-5-hrs-40-min-11-sec-quot-to-seconds/m-p/235525#M69931</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-11-14T18:04:42Z</dc:date>
    </item>
  </channel>
</rss>

