gpt4 book ai didi

r - 如何提取两个不同时间值之间的营业时间

转载 作者:行者123 更新时间:2023-12-05 08:23:47 25 4
gpt4 key购买 nike

对于服务台处理的工单的评估,我想知道工单在多少营业时间内处于事件状态。我可以很容易地减去时间并得到总小时数。但唯一应该计算的时间是 08:30 到 18:00 之间。

例如:如果工单创建于11/23/2015 10:20,完成于11/24/2015 17:20,则 31 'normal ’几个小时过去了。我只对过去的营业时间(8:30 到 18:00 之间)感兴趣;在这种情况下 16 小时 30 分钟

最佳答案

我已经尝试了@mvan 和@Steffan Jansson 的解决方案。不幸的是,它们都不符合我的需要。第一个返回不正确的信息。后者是为了满足我的要求而变慢,并且不考虑夏令时。

我创建了一个更快并且确实考虑了夏令时的函数。您可以指定营业时间和假期。

用法

该函数接受 5 个参数。其中 3 个是可选的。

  1. 开始时间戳(POSIXct)
  2. 结束时间戳(POSIXct)
  3. 一个工作日的开始时间(字符串:'00:00' 到 '24:00')
  4. 一个工作日的结束时间(字符串:'00:00' 到 '24:00')
  5. 假期列表(日期列表(as.Date))

示例:

start <- as.POSIXct('2014-09-27 10:12:37', tz = 'Europe/Amsterdam')
end <- as.POSIXct('2016-12-10 20:04:18', tz = 'Europe/Amsterdam')

biz_hrs(start, end, '10:00', '17:00')

您还可以在数据框列上运行它。确保每个值的格式都正确。如果结束值早于起始值,则函数返回 NA。更改该编辑行 6

代码

library(lubridate)


biz_hrs <- Vectorize(function(start, end, starting_time = '9:00', ending_time = '17:00', holidays = NULL){


if(end < start){

return(NA)

} else {

start_datetime <- as.POSIXct(paste0(substr(start,1,11), starting_time, ':00'))
end_datetime <- as.POSIXct(paste0(substr(end,1,11), ending_time, ':00'))

if(as.Date(start) == as.Date(end) & !as.Date(start) %in% holidays & !format(as.Date(start), "%u") %in% c(6,7)){ #if starting time stamp is on same day as ending time stamp and if day is not a holiday or weekend

if(start > start_datetime & end < end_datetime){ #if starting time stamp is later than start business hour and ending time stamp is earlier then ending business hour.
return(as.numeric(difftime(end, start), units = 'hours'))
} else if(start > start_datetime & end > end_datetime & start < end_datetime){ #if starting time stamp is later than end business hour and ending time stamp is earlier then ending business hour.
return(as.numeric(difftime(as.POSIXct(paste0(substr(start,1,11), ending_time, ':00')), start), units = 'hours'))
} else if(start < start_datetime & end < end_datetime & end > start_datetime){ #if starting time stamp is earlier than end business hour and ending time stamp is later than starting business hour.
return(as.numeric(difftime(end, start_datetime), units = 'hours'))
} else if(start > end_datetime & end > end_datetime){ #if starting time stamp is later than end business hour and ending time stamp is later than ending business hour.
return(0)
} else if(start < start_datetime & end < start_datetime){ #if starting time stamp is earlier than start business hour and ending time stamp is earlier than starting business hour.
return(0)
} else {
return(as.numeric(difftime(end_datetime, start_datetime), units = 'hours'))
}

} else { #if starting time stamp and ending time stamp occured on a different day.

business_hrs <- as.numeric(difftime(as.POSIXct(paste0('2017-01-01', ending_time, ':00')),
as.POSIXct(paste0('2017-01-01', starting_time, ':00')) #calculate business hours range by specified parameters
), units = 'hours')

start_day_hrs <- ifelse(start < as.POSIXct(paste0(substr(start,1,11), ending_time, ':00')) & !as.Date(start) %in% holidays & !format(as.Date(start), "%u") %in% c(6,7), #if start time stamp is earlier than specified ending time
as.numeric(difftime(as.POSIXct(paste0(substr(start,1,11), ending_time, ':00')), start), units = 'hours'), #calculate time between time stamp and specified ending time
0 #else set zero
) #calculate amount of time on starting day
start_day_hrs <- pmin(start_day_hrs, business_hrs) #cap the maximum amount of hours dertermined by the specified business hours
start_day_hrs
end_day_hrs <- ifelse(end > as.POSIXct(paste0(substr(end,1,11), starting_time, ':00')) & !as.Date(end) %in% holidays & !format(as.Date(end), "%u") %in% c(6,7), #if end time stamp is later than specified starting time
as.numeric(difftime(end, as.POSIXct(paste0(substr(end,1,11), starting_time, ':00'))), units = 'hours'), #calculate time between time stamp and specified starting time
0) #calculate amount of time on ending day
end_day_hrs <- pmin(end_day_hrs, business_hrs) #cap the maximum amount of hours dertermined by the specified business hours
days_between <- seq(as.Date(start), as.Date(end), by = 1) #create a vector of dates (from and up to including) the starting time stamp and ending time stamp
business_days <- days_between[!days_between %in% c(as.Date(start), as.Date(end)) & !days_between %in% holidays & !format(as.Date(days_between), "%u") %in% c(6,7)] #remove weekends and holidays from vector of dates

return(as.numeric(((length(business_days) * business_hrs) + start_day_hrs + end_day_hrs))) #multiply the remaining number of days in the vector (business days) by the amount of business hours and add hours from the starting and end day. Return the result

}

}


})

关于r - 如何提取两个不同时间值之间的营业时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34068109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com