refactor: move packages to 'pkg' directory
This commit is contained in:
187
pkg/calendar/calendar.go
Normal file
187
pkg/calendar/calendar.go
Normal file
@ -0,0 +1,187 @@
|
||||
package calendar
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dolanor/caldav-go/caldav"
|
||||
"github.com/dolanor/caldav-go/caldav/entities"
|
||||
"github.com/dolanor/caldav-go/icalendar/components"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Caldav interface {
|
||||
QueryEvents(path string, query *entities.CalendarQuery) (events []*components.Event, oerr error)
|
||||
}
|
||||
|
||||
type Calendar struct {
|
||||
Location *time.Location
|
||||
cdav Caldav
|
||||
caldavPath string
|
||||
caldavSummaryPattern string
|
||||
}
|
||||
|
||||
func NewCaldav(caldavUrl, caldavPath string) (Caldav, error) {
|
||||
// create a reference to your CalDAV-compliant server
|
||||
server, _ := caldav.NewServer(caldavUrl)
|
||||
// create a CalDAV client to speak to the server
|
||||
var client = caldav.NewClient(server, http.DefaultClient)
|
||||
// start executing requests!
|
||||
err := client.ValidateServer(caldavPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bad caldav configuration, unable to validate connexion: %w", err)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
type Option func(calendar *Calendar)
|
||||
|
||||
func WithCaldav(cdav Caldav) Option {
|
||||
return func(calendar *Calendar) {
|
||||
calendar.cdav = cdav
|
||||
}
|
||||
}
|
||||
|
||||
func WithCaldavSummaryPattern(caldavSummaryPattern string) Option {
|
||||
return func(calendar *Calendar) {
|
||||
calendar.caldavSummaryPattern = caldavSummaryPattern
|
||||
}
|
||||
}
|
||||
|
||||
func WithCaldavPath(caldavPath string) Option {
|
||||
return func(calendar *Calendar) {
|
||||
calendar.caldavPath = caldavPath
|
||||
}
|
||||
}
|
||||
|
||||
func New(location *time.Location, opts ...Option) *Calendar {
|
||||
c := &Calendar{
|
||||
location,
|
||||
nil,
|
||||
"",
|
||||
"",
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (cal *Calendar) GetEasterDay(year int) time.Time {
|
||||
g := float64(year % 19.0)
|
||||
c := math.Floor(float64(year) / 100.0)
|
||||
c4 := math.Floor(c / 4.0)
|
||||
h := float64(int(19.0*g+c-c4-math.Floor((8.0*c+13)/25)+15) % 30.0)
|
||||
k := math.Floor(h / 28.0)
|
||||
i := (k*math.Floor(29./(h+1.))*math.Floor((21.-g)/11.)-1.)*k + h
|
||||
|
||||
// jour de Pâques (0=dimanche, 1=lundi....)
|
||||
dayWeek := int(math.Floor(float64(year)/4.)+float64(year)+i+2+c4-c) % 7
|
||||
|
||||
// Jour de Pâques en jours enpartant de 1 = 1er mars
|
||||
presJour := int(28 + int(i) - dayWeek)
|
||||
|
||||
// mois (0 = janvier, ... 2 = mars, 3 = avril)
|
||||
month := 2
|
||||
if presJour > 31 {
|
||||
month = 3
|
||||
}
|
||||
|
||||
// Mois dans l'année
|
||||
month += 1
|
||||
|
||||
// jour du mois
|
||||
day := presJour - 31
|
||||
if month == 2 {
|
||||
day = presJour
|
||||
}
|
||||
|
||||
return time.Date(year, 3, 31, 0, 0, 0, 0, cal.Location).AddDate(0, 0, day)
|
||||
}
|
||||
|
||||
func (cal *Calendar) GetHolidays(year int) *[]time.Time {
|
||||
|
||||
// Calcul du jour de pâques
|
||||
paques := cal.GetEasterDay(year)
|
||||
|
||||
joursFeries := []time.Time{
|
||||
// Jour de l'an
|
||||
time.Date(year, time.January, 1, 0, 0, 0, 0, cal.Location),
|
||||
// Easter
|
||||
paques.AddDate(0, 0, 1),
|
||||
// 1 mai
|
||||
time.Date(year, time.May, 1, 0, 0, 0, 0, cal.Location),
|
||||
// 8 mai
|
||||
time.Date(year, time.May, 8, 0, 0, 0, 0, cal.Location),
|
||||
// Ascension
|
||||
paques.AddDate(0, 0, 39),
|
||||
// 14 juillet
|
||||
time.Date(year, time.July, 14, 0, 0, 0, 0, cal.Location),
|
||||
// 15 aout
|
||||
time.Date(year, time.August, 15, 0, 0, 0, 0, cal.Location),
|
||||
// Toussaint
|
||||
time.Date(year, time.November, 1, 0, 0, 0, 0, cal.Location),
|
||||
// 11 novembre
|
||||
time.Date(year, time.November, 11, 0, 0, 0, 0, cal.Location),
|
||||
// noël
|
||||
time.Date(year, time.December, 25, 0, 0, 0, 0, cal.Location),
|
||||
}
|
||||
|
||||
return &joursFeries
|
||||
}
|
||||
|
||||
func (cal *Calendar) GetHolidaysSet(year int) map[time.Time]bool {
|
||||
holidays := cal.GetHolidays(year)
|
||||
result := make(map[time.Time]bool, len(*holidays))
|
||||
for _, h := range *holidays {
|
||||
result[h] = true
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (cal *Calendar) IsHoliday(date time.Time) bool {
|
||||
h := cal.GetHolidaysSet(date.Year())
|
||||
d := date.In(cal.Location)
|
||||
day := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, cal.Location)
|
||||
caldavHolidays, err := cal.IsHolidaysFromCaldav(day)
|
||||
if err != nil {
|
||||
log.Printf("unable to check holidays from caldav: %v", err)
|
||||
}
|
||||
return h[day] || caldavHolidays
|
||||
}
|
||||
|
||||
func (cal *Calendar) IsWorkingDay(date time.Time) bool {
|
||||
return !cal.IsHoliday(date) && date.Weekday() >= time.Monday && date.Weekday() <= time.Friday
|
||||
}
|
||||
|
||||
func (cal *Calendar) IsWorkingDayToday() bool {
|
||||
return cal.IsWorkingDay(time.Now())
|
||||
}
|
||||
|
||||
func (cal *Calendar) IsWeekDay(day time.Time) bool {
|
||||
return day.Weekday() >= time.Monday && day.Weekday() <= time.Friday
|
||||
}
|
||||
|
||||
func (cal *Calendar) IsHolidaysFromCaldav(day time.Time) (bool, error) {
|
||||
if cal.cdav == nil {
|
||||
return false, nil
|
||||
}
|
||||
query, err := entities.NewEventRangeQuery(day.UTC(), day.UTC().Add(23*time.Hour+59*time.Minute))
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("unable to build events range query: %v", err)
|
||||
}
|
||||
events, err := cal.cdav.QueryEvents(cal.caldavPath, query)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("unable list events from caldav: %v", err)
|
||||
}
|
||||
|
||||
for _, evt := range events {
|
||||
if strings.Contains(evt.Summary, cal.caldavSummaryPattern) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
280
pkg/calendar/calendar_test.go
Normal file
280
pkg/calendar/calendar_test.go
Normal file
@ -0,0 +1,280 @@
|
||||
package calendar
|
||||
|
||||
import (
|
||||
"github.com/dolanor/caldav-go/caldav/entities"
|
||||
"github.com/dolanor/caldav-go/icalendar/components"
|
||||
"github.com/dolanor/caldav-go/icalendar/values"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCalendar_GetEasterDay(t *testing.T) {
|
||||
loc, err := time.LoadLocation("Europe/Paris")
|
||||
if err != nil {
|
||||
t.Errorf("unable to load time location: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
easterDays := []time.Time{
|
||||
time.Date(2019, time.April, 21, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.April, 12, 0, 0, 0, 0, loc),
|
||||
time.Date(2021, time.April, 4, 0, 0, 0, 0, loc),
|
||||
}
|
||||
|
||||
c := New(loc)
|
||||
|
||||
for _, d := range easterDays {
|
||||
easter := c.GetEasterDay(d.Year())
|
||||
if easter != d {
|
||||
t.Errorf("bad date for year %d, expected:%v ; actual:%v", d.Year(), d, easter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendar_GetHolidays(t *testing.T) {
|
||||
loc, err := time.LoadLocation("Europe/Paris")
|
||||
if err != nil {
|
||||
t.Errorf("unable to load time location: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
expectedHolidays := map[time.Time]bool{
|
||||
time.Date(2020, time.January, 1, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.April, 13, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.May, 1, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.May, 8, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.May, 21, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.July, 14, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.August, 15, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.November, 1, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.November, 11, 0, 0, 0, 0, loc): true,
|
||||
time.Date(2020, time.December, 25, 0, 0, 0, 0, loc): true,
|
||||
}
|
||||
|
||||
c := New(loc)
|
||||
holidays := c.GetHolidays(2020)
|
||||
if len(*holidays) != len(expectedHolidays) {
|
||||
t.Errorf("bad number of holidays, %d but %d are expected", len(*holidays), len(expectedHolidays))
|
||||
}
|
||||
for _, h := range *holidays {
|
||||
if !expectedHolidays[h] {
|
||||
t.Errorf("%v is not a holiday", h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendar_GetHolidaysSet(t *testing.T) {
|
||||
loc, err := time.LoadLocation("Europe/Paris")
|
||||
if err != nil {
|
||||
t.Errorf("unable to load time location: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
expectedHolidays := []time.Time{
|
||||
time.Date(2020, time.January, 1, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.April, 13, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.May, 1, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.May, 8, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.May, 21, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.July, 14, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.August, 15, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.November, 1, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.November, 11, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.December, 25, 0, 0, 0, 0, loc),
|
||||
}
|
||||
|
||||
c := New(loc)
|
||||
holidays := c.GetHolidaysSet(2020)
|
||||
if len(holidays) != len(expectedHolidays) {
|
||||
t.Errorf("bad number of holidays, %d but %d are expected", len(holidays), len(expectedHolidays))
|
||||
}
|
||||
for _, h := range expectedHolidays {
|
||||
if !(holidays)[h] {
|
||||
t.Errorf("%v is not a holiday", h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendar_IsHolidays(t *testing.T) {
|
||||
loc, err := time.LoadLocation("Europe/Paris")
|
||||
if err != nil {
|
||||
t.Errorf("unable to load time location: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
expectedHolidays := []time.Time{
|
||||
time.Date(2020, time.January, 1, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.April, 13, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.May, 1, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.May, 8, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.May, 21, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.July, 14, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.August, 15, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.November, 1, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.November, 11, 0, 0, 0, 0, loc),
|
||||
time.Date(2020, time.December, 25, 0, 0, 0, 0, loc),
|
||||
}
|
||||
|
||||
c := New(loc)
|
||||
holidays := c.GetHolidaysSet(2020)
|
||||
if len(holidays) != len(expectedHolidays) {
|
||||
t.Errorf("bad number of holidays, %d but %d are expected", len(holidays), len(expectedHolidays))
|
||||
}
|
||||
for _, h := range expectedHolidays {
|
||||
if !c.IsHoliday(h) {
|
||||
t.Errorf("%v is a holiday", h)
|
||||
}
|
||||
}
|
||||
if c.IsHoliday(time.Date(2019, time.January, 02, 0, 0, 0, 0, loc)) {
|
||||
t.Error("02 january should not be a holiday")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendar_IsWorkingDay(t *testing.T) {
|
||||
loc, err := time.LoadLocation("Europe/Paris")
|
||||
if err != nil {
|
||||
t.Errorf("unable to load time location: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
c := New(loc)
|
||||
|
||||
if c.IsWorkingDay(time.Date(2019, time.January, 01, 0, 0, 0, 0, loc)) {
|
||||
t.Error("1st january is not a working day")
|
||||
}
|
||||
if !c.IsWorkingDay(time.Date(2019, time.January, 02, 0, 0, 0, 0, loc)) {
|
||||
t.Error("02 january should be a working day")
|
||||
}
|
||||
|
||||
if !c.IsWorkingDay(time.Date(2019, time.January, 7, 0, 0, 0, 0, loc)) {
|
||||
t.Error("Monday should be a working day")
|
||||
}
|
||||
if !c.IsWorkingDay(time.Date(2019, time.January, 8, 0, 0, 0, 0, loc)) {
|
||||
t.Error("Tuesday should be a working day")
|
||||
}
|
||||
if !c.IsWorkingDay(time.Date(2019, time.January, 9, 0, 0, 0, 0, loc)) {
|
||||
t.Error("Wednesday should be a working day")
|
||||
}
|
||||
if !c.IsWorkingDay(time.Date(2019, time.January, 10, 0, 0, 0, 0, loc)) {
|
||||
t.Error("Thursday should be a working day")
|
||||
}
|
||||
if !c.IsWorkingDay(time.Date(2019, time.January, 11, 0, 0, 0, 0, loc)) {
|
||||
t.Error("Friday should be a working day")
|
||||
}
|
||||
if c.IsWorkingDay(time.Date(2019, time.January, 12, 0, 0, 0, 0, loc)) {
|
||||
t.Error("Saturday should not be a working day")
|
||||
}
|
||||
if c.IsWorkingDay(time.Date(2019, time.January, 13, 0, 0, 0, 0, loc)) {
|
||||
t.Error("Sunday should not be a working day")
|
||||
}
|
||||
}
|
||||
|
||||
type MockCaldav struct {
|
||||
events []*components.Event
|
||||
}
|
||||
|
||||
func (m *MockCaldav) QueryEvents(_ string, _ *entities.CalendarQuery) ([]*components.Event, error) {
|
||||
return m.events, nil
|
||||
}
|
||||
|
||||
func TestCalendar_IsHolidaysFromCaldav(t *testing.T) {
|
||||
loc, err := time.LoadLocation("Europe/Paris")
|
||||
if err != nil {
|
||||
t.Errorf("unable to load time location: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
type fields struct {
|
||||
Location *time.Location
|
||||
cdav *MockCaldav
|
||||
caldavPath string
|
||||
caldavSummaryPattern string
|
||||
}
|
||||
type args struct {
|
||||
day time.Time
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want bool
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Holidays in events",
|
||||
fields: fields{
|
||||
Location: loc,
|
||||
cdav: &MockCaldav{
|
||||
events: []*components.Event{
|
||||
{
|
||||
UID: "1",
|
||||
DateStart: values.NewDateTime(time.Date(2022, time.April, 16, 0, 0, 0, 0, loc)),
|
||||
DateEnd: values.NewDateTime(time.Date(2022, time.April, 17, 0, 0, 0, 0, loc)),
|
||||
Summary: "Holidays",
|
||||
},
|
||||
},
|
||||
},
|
||||
caldavPath: "my_calendar/",
|
||||
caldavSummaryPattern: "Holidays",
|
||||
},
|
||||
args: args{
|
||||
day: time.Date(2022, time.April, 16, 0, 0, 0, 0, loc),
|
||||
},
|
||||
want: true,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Not Holidays in events",
|
||||
fields: fields{
|
||||
Location: loc,
|
||||
cdav: &MockCaldav{
|
||||
events: []*components.Event{
|
||||
{
|
||||
UID: "1",
|
||||
DateStart: values.NewDateTime(time.Date(2022, time.April, 16, 0, 0, 0, 0, loc)),
|
||||
DateEnd: values.NewDateTime(time.Date(2022, time.April, 17, 0, 0, 0, 0, loc)),
|
||||
Summary: "Another event",
|
||||
},
|
||||
},
|
||||
},
|
||||
caldavPath: "my_calendar/",
|
||||
caldavSummaryPattern: "Holidays",
|
||||
},
|
||||
args: args{
|
||||
day: time.Date(2022, time.April, 16, 0, 0, 0, 0, loc),
|
||||
},
|
||||
want: false,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "No events",
|
||||
fields: fields{
|
||||
Location: loc,
|
||||
cdav: &MockCaldav{},
|
||||
caldavPath: "my_calendar/",
|
||||
caldavSummaryPattern: "Holidays",
|
||||
},
|
||||
args: args{
|
||||
day: time.Date(2022, time.April, 15, 0, 0, 0, 0, loc),
|
||||
},
|
||||
want: false,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cal := New(
|
||||
loc,
|
||||
WithCaldav(tt.fields.cdav),
|
||||
WithCaldavPath(tt.fields.caldavPath),
|
||||
WithCaldavSummaryPattern(tt.fields.caldavSummaryPattern),
|
||||
)
|
||||
got, err := cal.IsHolidaysFromCaldav(tt.args.day)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("IsHolidaysFromCaldav() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("IsHolidaysFromCaldav() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user