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

编程入门 行业动态 更新时间:2024-10-26 04:30:08
本文介绍了如何提取两个不同时间值之间的营业时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于评估由服务台处理的故障单,我想知道故障单有多少个工作时间。我可以轻松地减去时间并获得总小时数。但是唯一应该计数的时间是在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)

更多推荐

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

本文发布于:2023-10-23 01:44:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:营业时间   两个   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!