mirror of
https://github.com/taigrr/most-specific-period.git
synced 2026-04-02 03:38:41 -07:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 34a9fe93f6 | |||
| 523d062b04 | |||
| f3bf5c7d6b | |||
| 8b431b5d2f | |||
|
433d28a87b
|
12
.github/FUNDING.yml
vendored
Normal file
12
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
github: taigrr # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||||
|
patreon: # Replace with a single Patreon username
|
||||||
|
open_collective: # Replace with a single Open Collective username
|
||||||
|
ko_fi: # Replace with a single Ko-fi username
|
||||||
|
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||||
|
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||||
|
liberapay: # Replace with a single Liberapay username
|
||||||
|
issuehunt: # Replace with a single IssueHunt username
|
||||||
|
otechie: # Replace with a single Otechie username
|
||||||
|
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||||
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module github.com/taigrr/most-specific-period
|
module github.com/taigrr/most-specific-period
|
||||||
|
|
||||||
go 1.18
|
go 1.26
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -41,7 +41,7 @@ func warnMessage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func helpMessage() {
|
func helpMessage() {
|
||||||
fmt.Print("\nmost-significant-period [-h][-d]\n\nGenerates a timeline of periods and will provide a most significant period if available.\n\n-h\tShows this help menut\n-d\tProvide a RFC 3339 time to provide an alternate point for calculating MSP.")
|
fmt.Print("\nmost-specific-period [-h][-d]\n\nGenerates a timeline of periods and will provide a most specific period if available.\n\n-h\tShows this help menu\n-d\tProvide an RFC 3339 time to provide an alternate point for calculating MSP.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package msp
|
package msp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -148,7 +147,7 @@ func TestGetChangeOvers(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
t.Run(tc.testID, func(t *testing.T) {
|
||||||
changeovers := GetChangeOvers(tc.periods...)
|
changeovers := GetChangeOvers(tc.periods...)
|
||||||
if !slicesEqual(changeovers, tc.result) {
|
if !slicesEqual(changeovers, tc.result) {
|
||||||
t.Errorf("Expected %v but got %v", tc.result, changeovers)
|
t.Errorf("Expected %v but got %v", tc.result, changeovers)
|
||||||
@@ -320,7 +319,7 @@ func TestFlattenPeriods(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
t.Run(tc.testID, func(t *testing.T) {
|
||||||
changeovers := FlattenPeriods(tc.periods...)
|
changeovers := FlattenPeriods(tc.periods...)
|
||||||
if !slicesEqual(changeovers, tc.result) {
|
if !slicesEqual(changeovers, tc.result) {
|
||||||
t.Errorf("Expected %v but got %v", tc.result, changeovers)
|
t.Errorf("Expected %v but got %v", tc.result, changeovers)
|
||||||
@@ -468,7 +467,7 @@ func TestGetNextChangeOver(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
t.Run(tc.testID, func(t *testing.T) {
|
||||||
ts, err := GetNextChangeOver(now, tc.periods...)
|
ts, err := GetNextChangeOver(now, tc.periods...)
|
||||||
if tc.err != err {
|
if tc.err != err {
|
||||||
t.Errorf("Error %v does not match expected %v", tc.err, err)
|
t.Errorf("Error %v does not match expected %v", tc.err, err)
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrEndAfterStart occurs when a period given has an end time after its start time
|
// ErrEndAfterStart occurs when a period's start time is after its end time
|
||||||
ErrEndAfterStart = errors.New("error: start time is after end time")
|
ErrEndAfterStart = errors.New("error: start time is after end time")
|
||||||
// ErrNoValidPeriods occurs when an empty set of periods is passed or when ll periods are invalid
|
// ErrNoValidPeriods occurs when an empty set of periods is passed or when all periods are invalid
|
||||||
ErrNoValidPeriods = errors.New("error: no valid periods available")
|
ErrNoValidPeriods = errors.New("error: no valid periods available")
|
||||||
// ErrNoNextChangeover occurs when GetNextChangeover is called but there are no changeovers after t
|
// ErrNoNextChangeover occurs when GetNextChangeover is called but there are no changeovers after t
|
||||||
ErrNoNextChangeover = errors.New("error: no valid changeovers available")
|
ErrNoNextChangeover = errors.New("error: no valid changeovers available")
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ func MostSpecificPeriod(ts time.Time, periods ...Period) (id string, err error)
|
|||||||
return "", ErrNoValidPeriods
|
return "", ErrNoValidPeriods
|
||||||
}
|
}
|
||||||
// find the shortest duration
|
// find the shortest duration
|
||||||
d, err := GetDuration(periods[0].GetStartTime(), periods[0].GetEndTime())
|
d, _ := GetDuration(periods[0].GetStartTime(), periods[0].GetEndTime())
|
||||||
for _, x := range periods {
|
for _, x := range periods {
|
||||||
p, err := GetDuration(x.GetStartTime(), x.GetEndTime())
|
p, err := GetDuration(x.GetStartTime(), x.GetEndTime())
|
||||||
if err == nil && p < d {
|
if err == nil && p < d {
|
||||||
@@ -34,7 +34,7 @@ func MostSpecificPeriod(ts time.Time, periods ...Period) (id string, err error)
|
|||||||
newest = x.GetStartTime()
|
newest = x.GetStartTime()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Determine whichever of these periods have the same start time in addtion to duration
|
// Determine whichever of these periods have the same start time in addition to duration
|
||||||
var matchingDurationsAndStartTimes []Period
|
var matchingDurationsAndStartTimes []Period
|
||||||
for _, x := range matchingDurations {
|
for _, x := range matchingDurations {
|
||||||
if x.GetStartTime() == newest {
|
if x.GetStartTime() == newest {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package msp
|
package msp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -146,7 +145,7 @@ func TestMostSpecificPeriod(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
t.Run(tc.testID, func(t *testing.T) {
|
||||||
id, err := MostSpecificPeriod(tc.ts, tc.periods...)
|
id, err := MostSpecificPeriod(tc.ts, tc.periods...)
|
||||||
if id != tc.result {
|
if id != tc.result {
|
||||||
t.Errorf("ID '%s' does not match expected '%s'", id, tc.result)
|
t.Errorf("ID '%s' does not match expected '%s'", id, tc.result)
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ func TestGenerateTime(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
t.Run(tc.testID, func(t *testing.T) {
|
||||||
timeline := GenerateTimeline(tc.periods...)
|
timeline := GenerateTimeline(tc.periods...)
|
||||||
if len(timeline) != len(tc.result) {
|
if len(timeline) != len(tc.result) {
|
||||||
t.Fatalf("Time line had %d results, expected %d", len(timeline), len(tc.result))
|
t.Fatalf("Time line had %d results, expected %d", len(timeline), len(tc.result))
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package msp
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
// Compile-time interface check.
|
||||||
|
var _ Period = TimeWindow{}
|
||||||
|
|
||||||
type Period interface {
|
type Period interface {
|
||||||
GetStartTime() time.Time
|
GetStartTime() time.Time
|
||||||
GetEndTime() time.Time
|
GetEndTime() time.Time
|
||||||
|
|||||||
Reference in New Issue
Block a user