<?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: Time function in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71276#M17821</link>
    <description>&lt;P&gt;Hi, &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;strptime(X,Y)&lt;/CODE&gt; will convert a string X, e.g. "2013-03-22 11:22:33", into &lt;CODE&gt;epoch&lt;/CODE&gt;, with the string being described by Y&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;strftime(X,Y)&lt;/CODE&gt; will convert an epoch timestamp (X) into a string, defined by Y.&lt;/P&gt;

&lt;P&gt;An example;&lt;/P&gt;

&lt;P&gt;You want to calculate the difference between two timestamps in an event. This event looks like;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;2013-03-22 11:22:33 transactionid=123 startdate=03/18/2013 enddate=03/23/2013

base search 
| eval start=strptime(startdate,"%m/%d/%Y") 
| eval end=strptime(enddate, "%m/%d/%Y") 
| eval dur = end - start 
| the rest of the search
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then you want to calculate how many transaction that started in March. Add the following lines;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval startmonth = stfrtime(start, "%m")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now &lt;CODE&gt;start&lt;/CODE&gt; and &lt;CODE&gt;end&lt;/CODE&gt; is in epoch (an integer), &lt;CODE&gt;dur&lt;/CODE&gt; is also an integer (the number of seconds between the two dates). &lt;CODE&gt;startmonth&lt;/CODE&gt; will be '03' (for March) for the event above.&lt;/P&gt;

&lt;P&gt;See the following resources for more info;&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/Commontimeformatvariables"&gt;http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/Commontimeformatvariables&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/CommonEvalFunctions"&gt;http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/CommonEvalFunctions&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www.strftime.net"&gt;http://www.strftime.net&lt;/A&gt;&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;UPDATE:&lt;/P&gt;

&lt;P&gt;Well, since you have the CloseTimeDate as a string, you can do the calculations pretty much as described above all done in one &lt;CODE&gt;eval&lt;/CODE&gt;;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="TicketAnalysis" 
| eval cd=strftime(strptime(CloseDateTime,"%Y-%m-%d %H:%M:%S %p"),"%m-%d")
| chart count by cd
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The inner function - &lt;CODE&gt;strptime()&lt;/CODE&gt; - converts your string to epoch, and the outer - &lt;CODE&gt;strftime()&lt;/CODE&gt; - converts/extracts the parts you want, and in what order from the epoch. In this case Month-Day will be stored in the new field '&lt;CODE&gt;cd&lt;/CODE&gt;'. &lt;/P&gt;

&lt;P&gt;Of course, there is more than one way to do it, one of which is to use &lt;CODE&gt;eval&lt;/CODE&gt;'s &lt;CODE&gt;substr()&lt;/CODE&gt; function to operate on the string &lt;CODE&gt;CloseDateTime&lt;/CODE&gt; directly (if you are happy with how it looks, and just want to strip off a few parts).&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="TicketAnalysis" 
| eval cd=substr(CloseDateTime,5,5)
| chart count by cd
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;which will take the field (&lt;CODE&gt;CloseDateTime&lt;/CODE&gt;) jump to offset 5 (i.e. skip the &lt;CODE&gt;2013-&lt;/CODE&gt; part) and read 5 characters, i.e. '&lt;CODE&gt;02-19&lt;/CODE&gt;'.&lt;/P&gt;

&lt;P&gt;OR if these dates actually are the same, or nearly the same, as the time of the event, you may be happy with using the built-in fields &lt;CODE&gt;date_month, date_hour, date_mday, date_second&lt;/CODE&gt; etc. These are automatically calculated for most types of event, apart from Windows EventLogs.&lt;/P&gt;

&lt;P&gt;Hope this helps,&lt;/P&gt;

&lt;P&gt;Kristian&lt;/P&gt;</description>
    <pubDate>Fri, 22 Mar 2013 11:31:31 GMT</pubDate>
    <dc:creator>kristian_kolb</dc:creator>
    <dc:date>2013-03-22T11:31:31Z</dc:date>
    <item>
      <title>Time function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71275#M17820</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
How to use strptime(X,Y) and strftime(X,Y) function in search, please explain with example.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2013 10:09:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71275#M17820</guid>
      <dc:creator>vaibhavbeohar</dc:creator>
      <dc:date>2013-03-22T10:09:51Z</dc:date>
    </item>
    <item>
      <title>Re: Time function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71276#M17821</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;strptime(X,Y)&lt;/CODE&gt; will convert a string X, e.g. "2013-03-22 11:22:33", into &lt;CODE&gt;epoch&lt;/CODE&gt;, with the string being described by Y&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;strftime(X,Y)&lt;/CODE&gt; will convert an epoch timestamp (X) into a string, defined by Y.&lt;/P&gt;

&lt;P&gt;An example;&lt;/P&gt;

&lt;P&gt;You want to calculate the difference between two timestamps in an event. This event looks like;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;2013-03-22 11:22:33 transactionid=123 startdate=03/18/2013 enddate=03/23/2013

base search 
| eval start=strptime(startdate,"%m/%d/%Y") 
| eval end=strptime(enddate, "%m/%d/%Y") 
| eval dur = end - start 
| the rest of the search
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then you want to calculate how many transaction that started in March. Add the following lines;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval startmonth = stfrtime(start, "%m")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now &lt;CODE&gt;start&lt;/CODE&gt; and &lt;CODE&gt;end&lt;/CODE&gt; is in epoch (an integer), &lt;CODE&gt;dur&lt;/CODE&gt; is also an integer (the number of seconds between the two dates). &lt;CODE&gt;startmonth&lt;/CODE&gt; will be '03' (for March) for the event above.&lt;/P&gt;

&lt;P&gt;See the following resources for more info;&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/Commontimeformatvariables"&gt;http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/Commontimeformatvariables&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/CommonEvalFunctions"&gt;http://docs.splunk.com/Documentation/Splunk/5.0.2/SearchReference/CommonEvalFunctions&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www.strftime.net"&gt;http://www.strftime.net&lt;/A&gt;&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;UPDATE:&lt;/P&gt;

&lt;P&gt;Well, since you have the CloseTimeDate as a string, you can do the calculations pretty much as described above all done in one &lt;CODE&gt;eval&lt;/CODE&gt;;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="TicketAnalysis" 
| eval cd=strftime(strptime(CloseDateTime,"%Y-%m-%d %H:%M:%S %p"),"%m-%d")
| chart count by cd
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The inner function - &lt;CODE&gt;strptime()&lt;/CODE&gt; - converts your string to epoch, and the outer - &lt;CODE&gt;strftime()&lt;/CODE&gt; - converts/extracts the parts you want, and in what order from the epoch. In this case Month-Day will be stored in the new field '&lt;CODE&gt;cd&lt;/CODE&gt;'. &lt;/P&gt;

&lt;P&gt;Of course, there is more than one way to do it, one of which is to use &lt;CODE&gt;eval&lt;/CODE&gt;'s &lt;CODE&gt;substr()&lt;/CODE&gt; function to operate on the string &lt;CODE&gt;CloseDateTime&lt;/CODE&gt; directly (if you are happy with how it looks, and just want to strip off a few parts).&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="TicketAnalysis" 
| eval cd=substr(CloseDateTime,5,5)
| chart count by cd
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;which will take the field (&lt;CODE&gt;CloseDateTime&lt;/CODE&gt;) jump to offset 5 (i.e. skip the &lt;CODE&gt;2013-&lt;/CODE&gt; part) and read 5 characters, i.e. '&lt;CODE&gt;02-19&lt;/CODE&gt;'.&lt;/P&gt;

&lt;P&gt;OR if these dates actually are the same, or nearly the same, as the time of the event, you may be happy with using the built-in fields &lt;CODE&gt;date_month, date_hour, date_mday, date_second&lt;/CODE&gt; etc. These are automatically calculated for most types of event, apart from Windows EventLogs.&lt;/P&gt;

&lt;P&gt;Hope this helps,&lt;/P&gt;

&lt;P&gt;Kristian&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2013 11:31:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71276#M17821</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2013-03-22T11:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Time function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71277#M17822</link>
      <description>&lt;P&gt;Hi &lt;BR /&gt;
I am running search with the following string&lt;/P&gt;

&lt;P&gt;"sourcetype="TicketAnalysis" | chart count by CloseDateTime" and getting result in following format&lt;/P&gt;

&lt;P&gt;"CloseDateTime"     "count"&lt;/P&gt;

&lt;P&gt;2013-02-19 11:40 AM 1&lt;/P&gt;

&lt;P&gt;2013-02-20 12:11 PM 1&lt;/P&gt;

&lt;P&gt;2013-02-22 16:26 PM 1&lt;/P&gt;

&lt;P&gt;2013-02-23 01:02 AM 1&lt;/P&gt;

&lt;P&gt;2013-02-26 12:13 PM 1&lt;/P&gt;

&lt;P&gt;i would like to have CloseDateTime field in only day or month format. i tried to do through "&lt;STRONG&gt;convert timeformat="%d" ctime(_time)&lt;/STRONG&gt; function and not getting any result. can you please help me out.&lt;/P&gt;

&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2013 11:58:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71277#M17822</guid>
      <dc:creator>vaibhavbeohar</dc:creator>
      <dc:date>2013-03-22T11:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: Time function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71278#M17823</link>
      <description>&lt;P&gt;Hi &lt;BR /&gt;
I am running search with the following string&lt;/P&gt;

&lt;P&gt;"sourcetype="TicketAnalysis" | chart count by CloseDateTime" and getting result in following format&lt;/P&gt;

&lt;P&gt;"CloseDateTime"     "count"&lt;/P&gt;

&lt;P&gt;2013-02-19 11:40 AM 1&lt;/P&gt;

&lt;P&gt;2013-02-20 12:11 PM 1&lt;/P&gt;

&lt;P&gt;2013-02-22 16:26 PM 1&lt;/P&gt;

&lt;P&gt;2013-02-23 01:02 AM 1&lt;/P&gt;

&lt;P&gt;2013-02-26 12:13 PM 1&lt;/P&gt;

&lt;P&gt;i would like to have CloseDateTime field in only day or month format. i tried to do through "&lt;STRONG&gt;convert timeformat="%d" ctime(_time)&lt;/STRONG&gt; function and not getting any result. can you please help me out.&lt;/P&gt;

&lt;P&gt;Thanks.       &lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2013 11:59:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71278#M17823</guid>
      <dc:creator>vaibhavbeohar</dc:creator>
      <dc:date>2013-03-22T11:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: Time function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71279#M17824</link>
      <description>&lt;P&gt;Thanks Kristian..&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2013 12:54:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71279#M17824</guid>
      <dc:creator>vaibhavbeohar</dc:creator>
      <dc:date>2013-03-22T12:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: Time function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71280#M17825</link>
      <description>&lt;P&gt;If it worked for you, please mark the question as 'answered'. Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2013 14:05:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Time-function/m-p/71280#M17825</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2013-03-22T14:05:42Z</dc:date>
    </item>
  </channel>
</rss>

