businessDuration: businessDuration

Description Usage Arguments Details Author(s) Examples

Description

A function to calculate business duration between two dates excluding weekends, public holidays and non-business hours in days, hours, minutes and seconds.

Usage

1
2
3
businessDuration(startdate="",enddate="",starttime=NA,
endtime=NA,weekendlist=c("Saturday","Sunday"),
holidaylist=c(),unit='min')

Arguments

startdate

Start date in "POSIXlt"/"POSIXct"

enddate

End date in "POSIXlt"/"POSIXct"

starttime

Start time in 24 hours format as a string. Eg- "07:00:00". Default is NA

endtime

End time in 24 hours format as a string. Eg- "17:00:00". Default is NA

weekendlist

Custom weekend list. Default is "Saturday" & "Sunday"

holidaylist

Custom holiday list. Default is NULL

unit

Unit of duration - "day","hour","min" or "sec". Default is "min"

Details

Returns the business duration between two dates by excluding weekends, public holidays and non-business hours in days, hours, minutes or seconds

Author(s)

Gnaneshwar G

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
### EXAMPLE 1
library(BusinessDuration)

# start date must be in standard R format
startdate <- strptime("2017-07-01 02:02:00",
"%Y-%m-%d %H:%M:%S")

# End date must be in standard R format
enddate <- strptime("2017-07-07 04:48:00",
"%Y-%m-%d %H:%M:%S")

# Business Start time
starttime <- "07:00:00"

# Business End time
endtime <- "17:00:00"

# Custom holiday list
holidaylist <- as.Date(c("2017-01-01" ,"2017-01-02",
"2017-01-16", "2017-02-15", "2017-02-20", "2017-03-31",
"2017-05-29", "2017-07-04", "2017-09-04", "2017-10-09",
"2017-11-10", "2017-11-11", "2017-11-23" ,"2017-12-25"))

# Custom unit of business duration
unit<-"day"

# Calling the function
businessDuration(startdate = startdate,
                 enddate = enddate,
                 starttime = starttime,endtime = endtime,
                 holidaylist = holidaylist,
                 unit = unit)

### EXAMPLE 2
library(BusinessDuration)

# Reading the file as dataframe
inputdata <- data.frame("Index"=1:5,
                        "sys_created_on"=c("12/6/2017 8:29",
                                           "12/1/2017 2:36",
                                           "12/6/2017 8:51",
                                           "12/1/2017 8:05",
                                           "12/1/2017 0:07"),
                        "resolved_at"=c("12/11/2017 4:56",
                                        "12/5/2017 4:10",
                                        "12/6/2017 8:52",
                                        "12/7/2017 6:46",
                                        "12/1/2017 0:23"))

# Converting to standard R datetime format
inputdata$sys_created_on <- strptime(inputdata$sys_created_on,
                                     "%m/%d/%Y %H:%M")
inputdata$resolved_at <- strptime(inputdata$resolved_at,
                                  "%m/%d/%Y %H:%M")

# Business open time
starttime <- "08:00:00"

# Business close time
endtime <- "17:00:00"

# Weekend list
weekend_list <- c("Saturday","Sunday")

# Custom US holidays
US_holiday_list <- as.Date(c("2018-01-01",
                             "2018-05-28",
                             "2018-07-04",
                             "2018-09-03",
                             "2018-11-22",
                             "2018-12-25"))

# Business duration - day, hour, min, sec
unit_hour <- "hour"

# Apply function to entire dataframe
inputdata$Biz_Hour<-lapply(1:nrow(inputdata),function(x){
                    businessDuration(startdate = inputdata$sys_created_on[x],
                    enddate = inputdata$resolved_at[x],
                    starttime = starttime,
                    endtime = endtime,
                    weekendlist = weekend_list,
                    holidaylist = US_holiday_list,
                    unit = unit_hour)})

BusinessDuration documentation built on May 2, 2019, 7:23 a.m.