Acg N. .
Temat: Może się przyda: dni robocze, weekdays, święta w Polsce i...
A nuż się komuś przyda do obliczeń.Przykład pokazuje:
1. skąd pobrać listę dni świątecznych, w tym wolnych od pracy, dla różnych krajów i lat
2. jak przetworzyć te dane do postaci wektora dat
3. jak wykorzystać pakiet "bizdays" do określenia liczby dni roboczych...
4. ...oraz dnia roboczego N dni od podanej daty z uwzględnieniem listy dni wolnych od pracy
> library(XML)
> library(stringr)
> library(bizdays)
> options(stringsAsFactors=FALSE)
> url <- "http://www.timeanddate.com/holidays/poland/2016"
> ( holidays <- readHTMLTable(url)[[1]] )
V1 V2 V3 V4
1 Jan 1 Friday New Year's Day National holiday
2 Jan 6 Wednesday Epiphany National holiday
3 Feb 14 Sunday Valentine's Day Observance
4 Mar 20 Sunday March equinox Season
5 Mar 25 Friday Good Friday Observance
6 Mar 26 Saturday Holy Saturday Observance
7 Mar 27 Sunday Easter Day National holiday
8 Mar 28 Monday Easter Monday National holiday
9 May 1 Sunday Labor Day / May Day National holiday
10 May 3 Tuesday Constitution Day National holiday
11 May 15 Sunday Whit Sunday National holiday
12 May 26 Thursday Corpus Christi National holiday
13 May 26 Thursday Mother's Day Observance
14 Jun 20 Monday June Solstice Season
15 Jun 23 Thursday Father's Day Observance
16 Aug 15 Monday Assumption of Mary National holiday
17 Sep 22 Thursday September equinox Season
18 Nov 1 Tuesday All Saints' Day National holiday
19 Nov 11 Friday Independence Day National holiday
20 Dec 21 Wednesday December Solstice Season
21 Dec 24 Saturday Christmas Eve Observance
22 Dec 25 Sunday Christmas Day National holiday
23 Dec 26 Monday Second Day of Christmas National holiday
24 Dec 31 Saturday New Year's Eve Observance
> ( dates <- subset(holidays, V4 == "National holiday")$V1 )
[1] "Jan 1" "Jan 6" "Mar 27" "Mar 28" "May 1" "May 3" "May 15" "May 26" "Aug 15" "Nov 1" "Nov 11" "Dec 25" "Dec 26"
> ( months <- match( sapply(strsplit(dates, " "), "[", 1), month.abb) )
[1] 1 1 3 3 5 5 5 5 8 11 11 12 12
> ( days <- sapply(strsplit(dates, " "), "[", 2) )
[1] "1" "6" "27" "28" "1" "3" "15" "26" "15" "1" "11" "25" "26"
> ( holDates <- as.Date(paste("2016", months, days, sep="-")) )
[1] "2016-01-01" "2016-01-06" "2016-03-27" "2016-03-28" "2016-05-01" "2016-05-03" "2016-05-15" "2016-05-26" "2016-08-15"
[10] "2016-11-01" "2016-11-11" "2016-12-25" "2016-12-26"
> ( cal <- Calendar(name="WeekdaysPL", holidays=holDates, weekdays=c("saturday", "sunday"), dib=365, start.date="2016-01-01", end.date="2016-12-31") )
Calendar: WeekdaysPL
Range: 2016-01-01 to 2016-12-31
weekdays: saturday sunday
dib: 365
> is.bizday("2016-01-01", cal) # Nowy Rok
[1] FALSE
> is.bizday("2016-01-06", cal) # Święto Trzech Króli
[1] FALSE
> is.bizday("2016-05-03", cal) # Święto Konstytucji
[1] FALSE
> bizdays("2016-01-01", "2016-01-07", cal) + 1
[1] 3
> offset("2016-01-05", 1, cal) # przeskocz 3. Króli
[1] "2016-01-07"
> offset("2016-01-05", 3, cal) # przeskocz 3. Króli + weekend
[1] "2016-01-11"
I przydatna funkcja:
PrepareCalendar <- function(country, year) {Ten post został edytowany przez Autora dnia 30.11.16 o godzinie 10:32
require(XML)
require(stringr)
require(bizdays)
options(stringsAsFactors=FALSE)
url <- paste0("http://www.timeanddate.com/holidays/", country, "/", year)
holidays <- readHTMLTable(url)[[1]]
dates <- subset(holidays, V4 == "National holiday")$V1
months <- match( sapply(strsplit(dates, " "), "[", 1), month.abb)
days <- sapply(strsplit(dates, " "), "[", 2)
holDates <- as.Date(paste(year, months, days, sep="-"))
country <- tools::toTitleCase(country)
cal <- Calendar(name=paste("Weekdays", country), holidays=holDates, weekdays=c("saturday", "sunday"), dib=365, start.date=paste0(year, "-01-01"), end.date=paste0(year, "-12-31") )
return(cal)
}
> ( calRussia <- PrepareCalendar("russia", 2017) )
Calendar: Weekdays Russia
Range: 2017-01-01 to 2017-12-31
weekdays: saturday sunday
dib: 365
> is.bizday("2017-02-23", calRussia) # Dzień Obrońców Ojczyzny
[1] FALSE
> bizdays("2017-02-23", "2017-02-27", calRussia) + 1
[1] 2
> offset("2017-02-22", 1, calRussia) # przeskocz Dzień Obrońców Ojczyzny
[1] "2017-02-24"
> ( calPoland <- PrepareCalendar("poland", 2019) )
Calendar: Weekdays Poland
Range: 2019-01-01 to 2019-12-31
weekdays: saturday sunday
dib: 365
> is.bizday("2019-5-1", calPoland) # Labor Day
[1] FALSE