<?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 search the total number of business hours of an event from a csv file? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120389#M25019</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I was curious about your request so I tried this in my lab using your example csv and I think I managed to get the result you were expecting:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputcsv mycsv.csv

| eval start_time_epoch = strptime(start_time,"%d/%m/%Y %I:%M:%S %p")
| eval start_time_second = strftime(start_time_epoch,"%S")
| eval start_time_epoch_rounded = start_time_epoch - start_time_second
| fields - start_time_epoch, start_time_second

| eval close_time_epoch = strptime(close_time,"%d/%m/%Y %I:%M:%S %p")
| eval close_time_second = strftime(close_time_epoch,"%S")
| eval close_time_epoch_rounded = close_time_epoch - close_time_second
| fields - close_time_epoch, close_time_second

| eval minute = mvrange(0, (close_time_epoch_rounded - start_time_epoch_rounded), 60)
| mvexpand minute

| eval _time = start_time_epoch_rounded + minute
| eval myHour = strftime(_time,"%H")
| eval myMinute = strftime(_time,"%H")
| eval myDay = strftime(_time,"%A")

| where myDay != "Saturday" AND myDay != "Sunday" AND myHour &amp;gt;= 9 AND (myHour &amp;lt; 17 OR (myHour = 17 AND myMinute = 0))
| stats count as durationInMinutes by start_time, close_time
| eval duration = tostring(durationInMinutes*60, "duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;start_time  close_time  durationInMinutes   duration
22/08/2014 3:00:08 PM   26/08/2014 4:50:32 PM   1070    17:50:00
25/08/2014 3:30:03 PM   27/08/2014 5:04:57 PM   1050    17:30:00
25/08/2014 5:54:53 PM   26/08/2014 1:28:55 PM   268     04:28:00 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
J&lt;/P&gt;</description>
    <pubDate>Mon, 15 Feb 2016 13:04:29 GMT</pubDate>
    <dc:creator>javiergn</dc:creator>
    <dc:date>2016-02-15T13:04:29Z</dc:date>
    <item>
      <title>How to search the total number of business hours of an event from a csv file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120386#M25016</link>
      <description>&lt;P&gt;HI, I have a file called file1.csv whose fields are start_time,close_time&lt;/P&gt;

&lt;P&gt;start_time              close_time &lt;BR /&gt;
22/08/2014 3:00:08 PM   26/08/2014 4:50:32 PM&lt;BR /&gt;
25/08/2014 5:54:53 PM   26/08/2014 1:28:55 PM &lt;BR /&gt;
25/08/2014 3:30:03 PM   27/08/2014 5:04:57 PM&lt;/P&gt;

&lt;P&gt;i want to create third field which would be total_time, whose value will be (close_time) - (start_time).But while finding the total_time, i want splunk to give me total business time only&lt;BR /&gt;
i.e., from 9AM to 5PM and monday to Friday&lt;/P&gt;

&lt;P&gt;SO total_time for above three examples would be 17.50,4:28 and 9:30 respectively.&lt;BR /&gt;
Kindly let me knwo what would be the Query in splunk to get the above result.Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 17:33:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120386#M25016</guid>
      <dc:creator>karthikTIL</dc:creator>
      <dc:date>2020-09-28T17:33:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the total number of business hours of an event from a csv file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120387#M25017</link>
      <description>&lt;P&gt;i have nearly the same problem, did someone have a answer? thanks&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 09:41:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120387#M25017</guid>
      <dc:creator>nikkkc</dc:creator>
      <dc:date>2016-02-15T09:41:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the total number of business hours of an event from a csv file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120388#M25018</link>
      <description>&lt;P&gt;I'm sure there is an easier way, but you could try something like this to build a filter before your calculation:&lt;/P&gt;

&lt;P&gt;| eval start_weekday=strftime(strptime(start_time,"%d/%m/%Y %I:%M:%S %p"),"%w") &lt;BR /&gt;
| eval start_hour=strftime(strptime(start_time,"%d/%m/%Y %I:%M:%S %p"),"%H")&lt;/P&gt;

&lt;P&gt;| eval close_weekday=strftime(strptime(close_time,"%d/%m/%Y %I:%M:%S %p"),"%w") &lt;BR /&gt;
| eval close_hour=strftime(strptime(close_time,"%d/%m/%Y %I:%M:%S %p"),"%H") &lt;/P&gt;

&lt;P&gt;| search start_weekday&amp;gt;=1 AND start_weekday&amp;lt;=5 AND start_hour&amp;gt;=9 AND start_hour&amp;lt;=17 AND close_weekday&amp;gt;=1 AND close_weekday&amp;lt;=5 AND close_hour&amp;gt;=9 AND close_hour&amp;lt;=17&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:45:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120388#M25018</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2020-09-29T08:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the total number of business hours of an event from a csv file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120389#M25019</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I was curious about your request so I tried this in my lab using your example csv and I think I managed to get the result you were expecting:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputcsv mycsv.csv

| eval start_time_epoch = strptime(start_time,"%d/%m/%Y %I:%M:%S %p")
| eval start_time_second = strftime(start_time_epoch,"%S")
| eval start_time_epoch_rounded = start_time_epoch - start_time_second
| fields - start_time_epoch, start_time_second

| eval close_time_epoch = strptime(close_time,"%d/%m/%Y %I:%M:%S %p")
| eval close_time_second = strftime(close_time_epoch,"%S")
| eval close_time_epoch_rounded = close_time_epoch - close_time_second
| fields - close_time_epoch, close_time_second

| eval minute = mvrange(0, (close_time_epoch_rounded - start_time_epoch_rounded), 60)
| mvexpand minute

| eval _time = start_time_epoch_rounded + minute
| eval myHour = strftime(_time,"%H")
| eval myMinute = strftime(_time,"%H")
| eval myDay = strftime(_time,"%A")

| where myDay != "Saturday" AND myDay != "Sunday" AND myHour &amp;gt;= 9 AND (myHour &amp;lt; 17 OR (myHour = 17 AND myMinute = 0))
| stats count as durationInMinutes by start_time, close_time
| eval duration = tostring(durationInMinutes*60, "duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;start_time  close_time  durationInMinutes   duration
22/08/2014 3:00:08 PM   26/08/2014 4:50:32 PM   1070    17:50:00
25/08/2014 3:30:03 PM   27/08/2014 5:04:57 PM   1050    17:30:00
25/08/2014 5:54:53 PM   26/08/2014 1:28:55 PM   268     04:28:00 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
J&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 13:04:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120389#M25019</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-02-15T13:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the total number of business hours of an event from a csv file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120390#M25020</link>
      <description>&lt;P&gt;Wow many thanks, &lt;BR /&gt;
i am glad! it works perfectly, you are the best!!! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 10:21:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120390#M25020</guid>
      <dc:creator>nikkkc</dc:creator>
      <dc:date>2016-02-16T10:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the total number of business hours of an event from a csv file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120391#M25021</link>
      <description>&lt;P&gt;one more question, is it possible to explain your search query a little bit... so i could learn something? thanks&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2016 14:07:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120391#M25021</guid>
      <dc:creator>nikkkc</dc:creator>
      <dc:date>2016-02-17T14:07:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to search the total number of business hours of an event from a csv file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120392#M25022</link>
      <description>&lt;P&gt;Sure, I guess the meat is in the following lines:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval minute = mvrange(0, (close_time_epoch_rounded - start_time_epoch_rounded), 60)
| mvexpand minute
| eval _time = start_time_epoch_rounded + minute
....
| where myDay != "Saturday" AND myDay != "Sunday" AND myHour &amp;gt;= 9 AND (myHour &amp;lt; 17 OR (myHour = 17 AND myMinute = 0))
| stats count as durationInMinutes by start_time, close_time
| eval duration = tostring(durationInMinutes*60, "duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Because you only have to static timestamps (start and end), mvrange + mvexpand will generate one event every 60 seconds from 0 to the difference in seconds between start and close times. &lt;BR /&gt;
The eval _time will then add that minute to the current start time. For instance:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;_time, start_time, close_time
17/Feb/2016 9:00:00, 17/Feb/2016 9:00:00, 17/Feb/2016 9:05:00
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Will become:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;_time, start_time, close_time
17/Feb/2016 9:00, 17/Feb/2016 9:00:00, 17/Feb/2016 9:05:00
17/Feb/2016 9:01, 17/Feb/2016 9:00:00, 17/Feb/2016 9:05:00
17/Feb/2016 9:02, 17/Feb/2016 9:00:00, 17/Feb/2016 9:05:00
17/Feb/2016 9:03, 17/Feb/2016 9:00:00, 17/Feb/2016 9:05:00
17/Feb/2016 9:04, 17/Feb/2016 9:00:00, 17/Feb/2016 9:05:00
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;We then filter by working days and business hours only.&lt;BR /&gt;
And we count the total number of minutes between start_time and close_time&lt;/P&gt;

&lt;P&gt;The eval duration is just to display this in a nice format.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:46:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-search-the-total-number-of-business-hours-of-an-event/m-p/120392#M25022</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2020-09-29T08:46:18Z</dc:date>
    </item>
  </channel>
</rss>

