]> git.vanrenterghem.biz Git - R/operatingdays.git/commitdiff
Add function to get the number of operating days in the month. master
authorFrederik Vanrenterghem <frederikv@fairgofinance.com.au>
Thu, 26 May 2016 11:42:00 +0000 (19:42 +0800)
committerFrederik Vanrenterghem <frederikv@fairgofinance.com.au>
Thu, 26 May 2016 11:42:00 +0000 (19:42 +0800)
Fix getRemainingOpDays function bug by casting input as date.

R/getOpDays.R [new file with mode: 0644]
R/getRemainingOpDays.R

diff --git a/R/getOpDays.R b/R/getOpDays.R
new file mode 100644 (file)
index 0000000..df5a259
--- /dev/null
@@ -0,0 +1,37 @@
+#' getOpDays
+# This is a function that returns the number of operating days in a month
+# It takes as arguments the country, state, date for which the request is made.
+# It returns a number
+# It currently only supports AU (Australia)
+#' @param country A country - use ISO format (currently only takes AU)
+#' @param state A state - use 2 or 3 letter abbreviation, in capitals
+#' @param date A date in the month of which the operating days will be counted
+#' @return The number of operating days in \code{state}, \code{country} as of \code{date}
+#' @examples getOpDays("AU","WA","2016-05-10")
+
+getOpDays <- function(country,state,date) {
+
+  # open dataset with Australian public holidays for 2016-2017
+  URL <- "http://data.gov.au/dataset/b1bc6077-dadd-4f61-9f8c-002ab2cdff10/resource/a24ecaf2-044a-4e66-989c-eacc81ded62f/download/australianpublicholidays-201617.csv"
+  x <- RCurl::getURL(URL)
+  public.holidays <- read.csv(textConnection(x))
+
+  # limit to public holidays observed in the state (including national holidays)
+  state.public.holidays <- public.holidays[public.holidays$Applicable.To %in% c(state,"NAT"),]
+  state.public.holidays$Date<-as.Date(state.public.holidays$Date,"%Y%m%d")
+
+  date <- as.Date(date)
+  startdate <- as.Date(cut(date, "month"))
+  enddate <- seq(startdate, by = "1 months", length.out = 2)[2]
+  days.in.month <- as.data.frame(seq(startdate,enddate-1,by = "1 day"))
+  colnames(days.in.month)<- c("Date")
+  # Add number of the weekday (1 to 7) to each day
+  days.in.month$weekdays <- mapply(function(x) strftime(x,"%u"),days.in.month)
+  # Saturday and Sunday are days 6 and 7 and non-working days
+  days.in.month$WorkingDay <- mapply(function(x) ifelse(x %in% c(6,7),"0","1"),days.in.month$weekdays)
+  # Overwrite working day flag if a working day is a public holiday, leaving as-is otherwise
+  days.in.month$WorkingDay <- mapply(function(x,y) ifelse(x %in% state.public.holidays$Date & identical(y,"1"),"0",y), days.in.month[,1],days.in.month$WorkingDay)
+
+  operating.days.in.month <- sum(days.in.month$WorkingDay==1)
+  return(operating.days.in.month)
+}
index eae5c8716b096264ac45856198364c929d2d6136..a201071c26a182e6fb192e2f2f791fe67db0cd55 100644 (file)
@@ -21,6 +21,7 @@ getRemainingOpDays <- function(country,state,date) {
   state.public.holidays <- public.holidays[public.holidays$Applicable.To %in% c(state,"NAT"),]
   state.public.holidays$Date<-as.Date(state.public.holidays$Date,"%Y%m%d")
 
+  date <- as.Date(date)
   today <- Sys.Date()
   startdate <- as.Date(cut(date, "month"))
   enddate <- seq(startdate, by = "1 months", length.out = 2)[2]