<?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: Arithmetic on time fields in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31547#M6517</link>
    <description>&lt;P&gt;You could calculate the seconds by hand:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex field=myField "(?&amp;lt;myFieldHrs&amp;gt;\d+):(?&amp;lt;myFieldMins&amp;gt;\d+):(?&amp;lt;myFieldSecs&amp;gt;\d+)(?&amp;lt;myFieldSub&amp;gt;\.\d+)" | eval myField= tonumber(myFieldHrs)*3600 + tonumber(myFieldMins)*60 + tonumber(myFieldSecs) + tonumber(myFieldSub)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 25 Jan 2011 22:57:01 GMT</pubDate>
    <dc:creator>ziegfried</dc:creator>
    <dc:date>2011-01-25T22:57:01Z</dc:date>
    <item>
      <title>Arithmetic on time fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31545#M6515</link>
      <description>&lt;P&gt;I have a record that has a field with a processing stat on it.&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;P&gt;myField=00:00:12.12456&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;i.e. it took 12 and a bit seconds.&lt;/P&gt;

&lt;P&gt;I want to report on the average time for a particular operation over a time bucket, something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="MySource" | timechart avg(myField) by Activity
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem is that &lt;CODE&gt;avg(myField)&lt;/CODE&gt; always yields 0.  So I am assuming that the default conversion is giving rubbish.  So I tried explicitly using convert to push into a computable format, something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| convert dur2sec(myField) as fieldAsSecs
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But this always comes out as zero as well!!  So I tried being explicit about the format:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;convert timeformat="%H:%M:%S:" dur2sec(myField) as convertTime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But that didn't create the field at all!!  Even if it did, how do I handle millisecs?&lt;/P&gt;

&lt;P&gt;Any clues?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2011 21:48:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31545#M6515</guid>
      <dc:creator>stuartamurray</dc:creator>
      <dc:date>2011-01-25T21:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic on time fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31546#M6516</link>
      <description>&lt;P&gt;Ok I have part of the answer from other postings.  If I spoof the date portion of the time format it at least converts, so:&lt;/P&gt;

&lt;P&gt;| eval convertTime = strptime("0000 01 01 " + myField, "%Y %m %d %H:%M:%S.%q")&lt;/P&gt;

&lt;P&gt;This actually works but has a large second component (presumably because I guessed the base year wrong).  I tried the other obvious one:&lt;/P&gt;

&lt;P&gt;| eval convertTime = strptime("1970 01 01 " + myField, "%Y %m %d %H:%M:%S.%q")&lt;/P&gt;

&lt;P&gt;This yields an even bigger offset&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2011 22:29:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31546#M6516</guid>
      <dc:creator>stuartamurray</dc:creator>
      <dc:date>2011-01-25T22:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic on time fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31547#M6517</link>
      <description>&lt;P&gt;You could calculate the seconds by hand:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex field=myField "(?&amp;lt;myFieldHrs&amp;gt;\d+):(?&amp;lt;myFieldMins&amp;gt;\d+):(?&amp;lt;myFieldSecs&amp;gt;\d+)(?&amp;lt;myFieldSub&amp;gt;\.\d+)" | eval myField= tonumber(myFieldHrs)*3600 + tonumber(myFieldMins)*60 + tonumber(myFieldSecs) + tonumber(myFieldSub)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jan 2011 22:57:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31547#M6517</guid>
      <dc:creator>ziegfried</dc:creator>
      <dc:date>2011-01-25T22:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic on time fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31548#M6518</link>
      <description>&lt;P&gt;You could try using a &lt;CODE&gt;sed&lt;/CODE&gt; based &lt;CODE&gt;rex&lt;/CODE&gt; command to convert your timeformat into the expected &lt;CODE&gt;dur2sec()&lt;/CODE&gt; format, or into an expression usable by &lt;CODE&gt;relative_time&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;I think you may run into issues with your sub-seconds, I would try stripping off the milliseconds first, then once you get a working solution, add them back in.&lt;/P&gt;

&lt;P&gt;Heres one possible approach:&lt;/P&gt;

&lt;P&gt;Convert &lt;CODE&gt;00:00:12.12456&lt;/CODE&gt; into &lt;CODE&gt;+0h+0m+12s&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Here is a test search I used to test this out on my system:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;* | head 1 | eval myField="00:00:12.12456" | eval myField_orig=myField
| rex mode=sed field=myField "s/^0?(\d+):0?(\d+):0?(\d+)(?:\.\d+)?$/+\1h+\2h+\3s/"
| eval output=relative_time(0, myField)
| table myField_orig myField output
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So I guess you could simply this down to a single eval, like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval output=relative_time(0, replace(myField, "^0?(\d+):0?(\d+):0?(\d+)(?:\.\d+)?$", "+\1h+\2h+\3s"))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Unfortunately, &lt;CODE&gt;relative_time&lt;/CODE&gt; doesn't accept sub-seconds (as far as I can tell), but you can do some simple string trickery to get around that.  There's an updated example that handles sub-seconds:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval output=tostring(round(relative_time(0, replace(myField, "^0?(\d+):0?(\d+):0?(\d+)(?:\.\d+)?$", "+\1h+\2h+\3s")),0)) + replace(myField, "^.+:\d+(\.\d+)?$", "\1")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This seems like a good candidate for a macro; so you don't have to keep copy-n-pasting this beast (especially if you find some corner case that I missed; it's nice to only have to fix it in one location)&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;I've run into some other weird time manipulation situation before that may interest you as well.  It would be nice if there were some more direct ways to handle some of these  situations, but for now there are at least workarounds...&lt;/P&gt;

&lt;P&gt;&lt;A href="http://answers.splunk.com/questions/4528/finding-your-local-timezone-with-eval" rel="nofollow"&gt;http://answers.splunk.com/questions/4528/finding-your-local-timezone-with-eval&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2011 23:03:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31548#M6518</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-01-25T23:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic on time fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31549#M6519</link>
      <description>&lt;P&gt;Thanks for the other answers guys, useful techniques.&lt;/P&gt;

&lt;P&gt;In the end I figured out a way to do it and support millisecs as follows:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval ProcessingTime= strptime("2000 01 01 " + myField, "%Y %d %m %H:%M:%S.%q") - strptime("2000 01 01 00:00:00.000000", "%Y %d %m %H:%M:%S.%q")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Basically strp my time then strp the base time period and then do the diff.  Works a treat but a bit ugly!!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2011 16:38:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Arithmetic-on-time-fields/m-p/31549#M6519</guid>
      <dc:creator>stuartamurray</dc:creator>
      <dc:date>2011-01-27T16:38:58Z</dc:date>
    </item>
  </channel>
</rss>

