如何提取两个不同时间值之间的营业时间 [英] How to extract Business Hours between two different times values

查看:84
本文介绍了如何提取两个不同时间值之间的营业时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于评估由服务台处理的故障单,我想知道故障单有多少个工作时间。我可以轻松地减去时间并获得总小时数。但是唯一应该计数的时间是在08:30到18:00之间。

For the evaluation of tickets processed by a help desk I would like to know how many business hours an ticket is active. I can easily subtract the times and get the total amount of hours. But the only hours that should count are between 08:30 and 18:00.

例如:如果在 11/23创建票证/ 2015 10:20 并在 2015/11/24 17:20 完成,那么已经过去了31个正常小时。我只对过去的营业时间(在8:30到18:00之间)感兴趣;在这种情况下 16小时30分钟

For example: if a ticket is created at 11/23/2015 10:20 and completed on 11/24/2015 17:20, then 31 'normal' hours have passed. I'm only interested in the business hours (between 8:30 and 18:00) that have passed; in this case 16 hours and 30 minutes

推荐答案

library(lubridate)

   tickets <- data.frame(open = as.POSIXct(strptime(df$open, "%m/%d/%Y %H:%M")), 
                      closed = as.POSIXct(strptime(df$closed, "%m/%d/%Y %H:%M"))


excludeDayCount <- Vectorize(function(open, close) {

   # Check if the ticket was open and closed on the same day
   if (identical(as.Date(open), as.Date(close))) return (0)

   # All the holidays to be excluded need to be put here
   holidays <- as.POSIXct(strptime(c("12/24/2015", "12/25/2015"), 
                                  "%m/%d/%Y"))

   # Dates between open and close  
   day_seq <- floor_date(seq(open + days(1), close, by = "days"), "day")

   # Count holidays / weekend days
   return(sum(day_seq %in% holidays | wday(day_seq) %in% c(1,7)))

})

bizHrDiff <- function(open, close) {

    # Hours from the end of one work day until the start of another
    hours_between_days <- dhours(6) + dhours(8.5)

    # Number of days to exclude
    excl_days <- excludeDayCount(open, close)  
    # Number of days in include
    reg_days <- as.integer(as.Date(close) - as.Date(open)) - excl_days 



    # Total duration between dates
      span <- as.duration(interval(open, close))
      # Remove the number of holidays and weekends
      span <- span - ddays(excl_days)
      # Remove out of office hours
      span <- span - (reg_days * hours_between_days)




     # Return in hours
      return(time_length(span, unit = "hour"))


}

bizHrDiff(tickets$open, tickets$closed)

这篇关于如何提取两个不同时间值之间的营业时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆