为了评估由服务台处理的故障单,我想知道故障单有多少个工作时间。我可以轻松地减去时间并获得总小时数。但是唯一应该计数的时间是08:30到18:00之间。
例如:如果在创建了工单并在上11/23/2015 10:20
完成工单11/24/2015 17:20
,则已经过去了31个“正常”小时。我只对过去的营业时间(在8:30到18:00之间)感兴趣。在这种情况下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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句