Skip to content

Commit

Permalink
util.FilterTimeOffs(): function to filter timeOffs
Browse files Browse the repository at this point in the history
  • Loading branch information
lyind committed Mar 17, 2024
1 parent f631b98 commit 60257ce
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `util.FilterTimeOffs()` function

## [0.3.0] - 2023-07-05

### Added
Expand Down
55 changes: 55 additions & 0 deletions util.go
Expand Up @@ -2,6 +2,8 @@ package util

import (
"time"

v1 "github.com/giantswarm/personio-go/v1"
)

// PersonioDateMax is the maximum representable time.Time value for the Personio API
Expand All @@ -22,3 +24,56 @@ func GetTimeIntersection(start1 time.Time, end1 time.Time, start2 time.Time, end

return endMin.Sub(startMax)
}

// FilterTimeOffs returns a slice of timeOffs that overlap with the given bounds and optionally matches minimum length and email
// Specify a minLengthHours of 0 to disable length filtering
// Specify email as nil to disable email filtering
func FilterTimeOffs(timeOffs []*v1.TimeOff, start time.Time, end time.Time, minLengthHours float64, email *string) []*v1.TimeOff {
var matchedTimeOffs []*v1.TimeOff

for _, timeOff := range timeOffs {

timeOffEmail := timeOff.Employee.GetStringAttribute("email")
if email != nil && (timeOffEmail == nil || *email != *timeOffEmail) {
continue
}

timeOffStart := timeOff.StartDate
timeOffEnd := timeOff.EndDate
// we need to adjust the wall-clock time according to other fields
if timeOff.DaysCount > 1 {
if timeOff.HalfDayStart {
timeOffStart = timeOffStart.Add(time.Hour * 12)
}
if timeOff.HalfDayEnd {
timeOffEnd = timeOffEnd.Add(time.Hour * 12)
} else {
timeOffEnd = timeOffEnd.Add(time.Hour * 24)
}
} else {
if timeOff.HalfDayStart && !timeOff.HalfDayEnd {
timeOffEnd = timeOffEnd.Add(time.Hour * 12)
} else if !timeOff.HalfDayStart && timeOff.HalfDayEnd {
timeOffStart = timeOffStart.Add(time.Hour * 12)
timeOffEnd = timeOffEnd.Add(time.Hour * 24)
} else {
timeOffEnd = timeOffEnd.Add(time.Hour * 24)
}
}

if minLengthHours > 0 && timeOffEnd.Sub(timeOffStart).Hours() < minLengthHours {
// timeOff event too short, ignore
continue
}

overlap := GetTimeIntersection(timeOffStart, timeOffEnd, start, end)
if overlap <= 0 {
// timeOff and bounds don't overlap
continue
}

matchedTimeOffs = append(matchedTimeOffs, timeOff)
}

return matchedTimeOffs
}

0 comments on commit 60257ce

Please sign in to comment.