Uses an interface instead of a struct now

This commit is contained in:
2022-03-07 16:46:17 -08:00
parent ca6ba89bce
commit 14aa89a047
4 changed files with 64 additions and 31 deletions

View File

@@ -12,9 +12,9 @@ func MostSpecificPeriod(ts time.Time, periods ...Period) (id string, err error)
return "", ErrNoValidPeriods
}
// find the shortest duration
d, err := GetDuration(periods[0].StartTime, periods[0].EndTime)
d, err := GetDuration(periods[0].GetStartTime(), periods[0].GetEndTime())
for _, x := range periods {
p, err := GetDuration(x.StartTime, x.EndTime)
p, err := GetDuration(x.GetStartTime(), x.GetEndTime())
if err == nil && p < d {
d = p
}
@@ -22,29 +22,29 @@ func MostSpecificPeriod(ts time.Time, periods ...Period) (id string, err error)
// find all periods with this shortest duration
var matchingDurations []Period
for _, x := range periods {
p, err := GetDuration(x.StartTime, x.EndTime)
p, err := GetDuration(x.GetStartTime(), x.GetEndTime())
if err == nil && p == d {
matchingDurations = append(matchingDurations, x)
}
}
// Find the newest time a period starts
newest := matchingDurations[0].StartTime
newest := matchingDurations[0].GetStartTime()
for _, x := range matchingDurations {
if x.StartTime.After(newest) {
newest = x.StartTime
if x.GetStartTime().After(newest) {
newest = x.GetStartTime()
}
}
// Determine whichever of these periods have the same start time in addtion to duration
var matchingDurationsAndStartTimes []Period
for _, x := range matchingDurations {
if x.StartTime == newest {
if x.GetStartTime() == newest {
matchingDurationsAndStartTimes = append(matchingDurationsAndStartTimes, x)
}
}
// Finally, return the period with the 'last' name lexicographically
var identifiers []string
for _, x := range matchingDurationsAndStartTimes {
identifiers = append(identifiers, x.Identifier)
identifiers = append(identifiers, x.GetIdentifier())
}
sort.Strings(identifiers)
return identifiers[len(identifiers)-1], nil
@@ -61,7 +61,7 @@ func GetDuration(start time.Time, end time.Time) (dur time.Duration, err error)
func ValidTimePeriods(ts time.Time, periods ...Period) []Period {
var valid []Period
for _, p := range periods {
if p.StartTime.Before(ts) && p.EndTime.After(ts) {
if p.GetStartTime().Before(ts) && p.GetEndTime().After(ts) {
valid = append(valid, p)
}
}