]> git.vanrenterghem.biz Git - R/operatingdays.git/commitdiff
Initial commit. Depends RCurl (not Imports).
authorFrederik Vanrenterghem <frederikv@fairgofinance.com.au>
Mon, 9 May 2016 00:00:49 +0000 (08:00 +0800)
committerFrederik Vanrenterghem <frederikv@fairgofinance.com.au>
Mon, 9 May 2016 00:00:49 +0000 (08:00 +0800)
.Rbuildignore [new file with mode: 0644]
.gitignore [new file with mode: 0644]
DESCRIPTION [new file with mode: 0644]
NAMESPACE [new file with mode: 0644]
R/getRemainingOpDays.R [new file with mode: 0644]
operatingdays.Rproj [new file with mode: 0644]

diff --git a/.Rbuildignore b/.Rbuildignore
new file mode 100644 (file)
index 0000000..91114bf
--- /dev/null
@@ -0,0 +1,2 @@
+^.*\.Rproj$
+^\.Rproj\.user$
diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..5b6a065
--- /dev/null
@@ -0,0 +1,4 @@
+.Rproj.user
+.Rhistory
+.RData
+.Ruserdata
diff --git a/DESCRIPTION b/DESCRIPTION
new file mode 100644 (file)
index 0000000..03aa672
--- /dev/null
@@ -0,0 +1,10 @@
+Package: operatingdays
+Type: Package
+Title: Return Number of Operating Days in Month
+Version: 0.1.0
+Author: Frederik Vanrenterghem
+Maintainer: Frederik Vanrenterghem <frederik@vanrenterghem.biz>
+Description: Return the number of operating days in a given month. Currently returns remaining days in current month.
+Depends: RCurl
+License: GPL-3
+LazyData: TRUE
diff --git a/NAMESPACE b/NAMESPACE
new file mode 100644 (file)
index 0000000..d75f824
--- /dev/null
+++ b/NAMESPACE
@@ -0,0 +1 @@
+exportPattern("^[[:alpha:]]+")
diff --git a/R/getRemainingOpDays.R b/R/getRemainingOpDays.R
new file mode 100644 (file)
index 0000000..9325670
--- /dev/null
@@ -0,0 +1,34 @@
+# getRemainingOpDays
+# This is a function that returns the remaining 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)
+
+getRemainingOpDays <- function(country,state,date) {
+  cutoff.time = "15:00"
+
+  # 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")
+
+  today <- Sys.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)
+
+  # if run before cutoff time, count today as remaining working day
+  remaining.operating.days.in.month <- ifelse(strftime(Sys.time(), "%H:%M") > cutoff.time,sum(days.in.month$WorkingDay==1 & days.in.month$Date>today),sum(days.in.month$WorkingDay==1 & days.in.month$Date>=today))
+  return(remaining.operating.days.in.month)
+}
diff --git a/operatingdays.Rproj b/operatingdays.Rproj
new file mode 100644 (file)
index 0000000..497f8bf
--- /dev/null
@@ -0,0 +1,20 @@
+Version: 1.0
+
+RestoreWorkspace: Default
+SaveWorkspace: Default
+AlwaysSaveHistory: Default
+
+EnableCodeIndexing: Yes
+UseSpacesForTab: Yes
+NumSpacesForTab: 2
+Encoding: UTF-8
+
+RnwWeave: Sweave
+LaTeX: pdfLaTeX
+
+AutoAppendNewline: Yes
+StripTrailingWhitespace: Yes
+
+BuildType: Package
+PackageUseDevtools: Yes
+PackageInstallArgs: --no-multiarch --with-keep.source