Skip to content

Commit

Permalink
Added check Time method to Module and updatet Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobock committed Oct 25, 2022
1 parent a6db102 commit 5ae7ed8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ The library is probably useful only for people realizing use cases with special
See https://godoc.org/github.com/wlbr/feiertage

### Usage:
There are two types of functions:
There are four types of functions:

* `<feiertag>(year)` and
* `<region>(year optional:IncludingSundays:true)`
* `GetRegionFromString(region, year)`
* `CheckIfIsBankHolidayIn(date, state)`

`<feiertag>` returns an extended `time` object (type `feiertag`). It carries the date of the holiday
in the requested year plus the name of the holiday. `<feiertag>` may be any of the following:
Expand Down Expand Up @@ -67,6 +69,13 @@ schools etc. are generally closed but workers don't get the day off by default.
include these days in your planning, it's okay to reference `Österreich` instead, as legal holidays are
(more or less) synchronised across all Austrian states (Bundesländer).


GetRegionFromString tries to match a string to a region from Germany or Austria. Because a reagion already contains the feiertage, a `year` is needed to calc theese.

CheckIfIsBankHolidayIn checks if the given `date` is a Bank holiday in the `region`, Sundays are only positve in case they are on a feiertag for the region as well.



### Examples:

fmt.Println(Ostern(2016))
Expand Down Expand Up @@ -107,6 +116,25 @@ include these days in your planning, it's okay to reference `Österreich` instea
25.12.2016 Weihnachten
26.12.2016 Zweiter Weihnachtsfeiertag

region, _ := GetRegionFromString("Brandenburg",2016)
fmt.Println(region)
--> Brandenburg (BB)
01.01.2016 Neujahr
25.03.2016 Karfreitag
28.03.2016 Ostermontag
01.05.2016 Tag der Arbeit
05.05.2016 Christi Himmelfahrt
16.05.2016 Pfingstmontag
03.10.2016 Tag der deutschen Einheit
31.10.2016 Reformationstag
25.12.2016 Weihnachten
26.12.2016 Zweiter Weihnachtsfeiertag

fmt.Println(CheckIfIsBankHolidayIn(time.Date(2016,1,1,0,0,0,0,time.UTC),Brandenburg(2016, false)))
--> true




## Command line tool

Expand Down
38 changes: 38 additions & 0 deletions checkBackHoliday.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package feiertage

import (
"github.com/pkg/errors"
"time"
)

func CheckIfIsBankHoliday(date time.Time, region Region) bool {

for _, feiertag := range region.Feiertage {
if datesAreEqual(feiertag.Time, date) {
return true
}
}
return false

}

func datesAreEqual(date1, date2 time.Time) bool {
if date1.Year() != date2.Year() {
return false
}
if date1.Month() != date2.Month() {
return false
}
if date1.Day() != date2.Day() {
return false
}
return true
}
func CheckIfIsBankHolidayIn(date time.Time, state string) (bool, error) {

region, err := GetRegionFromString(state, date.Year())
if err != nil {
return false, errors.Wrap(err, "Failed to parse region from state string")
}
return CheckIfIsBankHoliday(date, region), nil
}
20 changes: 20 additions & 0 deletions checkBackHoliday_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package feiertage

import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestCheckIfIsBankHoliday(t *testing.T) {
isBankHoliday := CheckIfIsBankHoliday(time.Date(2022, 10, 31, 0, 0, 0, 0, time.UTC), Niedersachsen(2022))
assert.True(t, isBankHoliday)
isBankHoliday = CheckIfIsBankHoliday(time.Date(2022, 12, 25, 0, 0, 0, 0, time.UTC), Niedersachsen(2022))
assert.True(t, isBankHoliday)
isBankHoliday = CheckIfIsBankHoliday(time.Date(2022, 12, 26, 0, 0, 0, 0, time.UTC), Niedersachsen(2022))
assert.True(t, isBankHoliday)
isBankHoliday = CheckIfIsBankHoliday(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), Niedersachsen(2023))
assert.True(t, isBankHoliday)
isBankHoliday = CheckIfIsBankHoliday(time.Date(2022, 10, 30, 0, 0, 0, 0, time.UTC), Niedersachsen(2022))
assert.False(t, isBankHoliday)
}
2 changes: 1 addition & 1 deletion cmd/feiertage/feiertage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func getRegion(region string, year int, includingSundays bool) (feiertage.Region
return r, nil
}
}
return r, fmt.Errorf("Region '%s' unbekannt.", region)
return r, fmt.Errorf("Region '%s' unknown", region)
}

func main() {
Expand Down

0 comments on commit 5ae7ed8

Please sign in to comment.