Skip to content

Commit

Permalink
Drop usage of go's log package
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Nov 30, 2023
1 parent b1b00aa commit 0d4e144
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
17 changes: 9 additions & 8 deletions internal/timeperiod/timeperiod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package timeperiod

import (
"github.com/teambition/rrule-go"
"log"
"time"
)

Expand Down Expand Up @@ -46,6 +45,8 @@ func (p *TimePeriod) NextTransition(base time.Time) time.Time {
return transition
}

// Entry represents a single TimePeriod segment and defines its own start and end time.
// You should initialize it by calling its Init method before using any of its other methods.
type Entry struct {
Start, End time.Time

Expand All @@ -57,6 +58,7 @@ type Entry struct {
}

// Init initializes the rrule instance from the configured rrule string
// Returns error when it fails to initiate an RRule instance from the configured rrule string (if any).
func (e *Entry) Init() error {
if e.rrule != nil || e.RecurrenceRule == "" {
return nil
Expand All @@ -82,10 +84,10 @@ func (e *Entry) Init() error {
}

// Contains returns whether a point in time t is covered by this entry.
// It panics when it's used before initializing the rrule instance while a rrule string is configured.
func (e *Entry) Contains(t time.Time) bool {
err := e.Init()
if err != nil {
log.Printf("Can't initialize entry: %s", err)
if e.rrule == nil && e.RecurrenceRule != "" {
panic("time period entry not initialized")
}

if t.Before(e.Start) {
Expand All @@ -108,11 +110,10 @@ func (e *Entry) Contains(t time.Time) bool {

// NextTransition returns the next recurrence start or end of this entry relative to the given time inclusively.
// This function returns also time.Time's zero value if there is no transition that starts/ends at/after the
// specified time.
// specified time. It panics when it's used before initializing the rrule instance while a rrule string is configured.
func (e *Entry) NextTransition(t time.Time) time.Time {
err := e.Init()
if err != nil {
log.Printf("Can't initialize entry: %s", err)
if e.rrule == nil && e.RecurrenceRule != "" {
panic("time period entry not initialized")
}

if t.Before(e.Start) {
Expand Down
38 changes: 24 additions & 14 deletions internal/timeperiod/timeperiod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func TestEntry(t *testing.T) {
start := berlinTime("2023-03-01 09:00:00")
end := berlinTime("2023-03-01 11:00:00")
until := berlinTime("2023-03-03 09:00:00")
e := &timeperiod.Entry{
e := initEntry(&timeperiod.Entry{
Start: start,
End: end,
RecurrenceRule: fmt.Sprintf("FREQ=DAILY;UNTIL=%s", until.UTC().Format(rrule.DateTimeFormat)),
}
})

t.Run("TimeAtFirstRecurrenceStart", func(t *testing.T) {
assert.True(t, e.Contains(start))
Expand Down Expand Up @@ -72,11 +72,11 @@ func TestEntry(t *testing.T) {
t.Run("DST", func(t *testing.T) {
start := berlinTime("2023-03-25 01:00:00")
end := berlinTime("2023-03-25 02:30:00")
e := &timeperiod.Entry{
e := initEntry(&timeperiod.Entry{
Start: start,
End: end,
RecurrenceRule: "FREQ=DAILY",
}
})

assert.True(t, e.Contains(start))

Expand All @@ -94,11 +94,11 @@ func TestEntry(t *testing.T) {

start := berlinTime("2023-03-01 08:00:00")
end := berlinTime("2023-03-01 12:30:00")
e := &timeperiod.Entry{
e := initEntry(&timeperiod.Entry{
Start: start,
End: end,
RecurrenceRule: "FREQ=DAILY",
}
})

t.Run("TimeAtFirstRecurrenceStart", func(t *testing.T) {
assert.Equal(t, end, e.NextTransition(start))
Expand All @@ -123,11 +123,11 @@ func TestEntry(t *testing.T) {
t.Run("DST", func(t *testing.T) {
start := berlinTime("2023-03-25 01:00:00")
end := berlinTime("2023-03-25 02:30:00")
e := &timeperiod.Entry{
e := initEntry(&timeperiod.Entry{
Start: start,
End: end,
RecurrenceRule: "FREQ=DAILY",
}
})

assert.Equal(t, end, e.NextTransition(start), "next transition should match the first recurrence end")

Expand All @@ -149,13 +149,14 @@ func TestTimePeriodTransitions(t *testing.T) {
t.Run("WithOneEntry", func(t *testing.T) {
start := berlinTime("2023-03-27 09:00:00")
end := berlinTime("2023-03-27 17:00:00")

p := &timeperiod.TimePeriod{
Name: "Transition Test",
Entries: []*timeperiod.Entry{{
Entries: []*timeperiod.Entry{initEntry(&timeperiod.Entry{
Start: start,
End: end,
RecurrenceRule: "FREQ=DAILY",
}},
})},
}

assert.Equal(t, end, p.NextTransition(start), "next transition should match the interval end")
Expand All @@ -169,16 +170,16 @@ func TestTimePeriodTransitions(t *testing.T) {
p := &timeperiod.TimePeriod{
Name: "Transition Test",
Entries: []*timeperiod.Entry{
{
initEntry(&timeperiod.Entry{
Start: start,
End: end,
RecurrenceRule: "FREQ=HOURLY;BYHOUR=1,3,5,7,9,11,13,15",
},
{
}),
initEntry(&timeperiod.Entry{
Start: berlinTime("2023-03-27 08:00:00"),
End: berlinTime("2023-03-27 08:30:00"),
RecurrenceRule: "FREQ=HOURLY;BYHOUR=0,2,4,6,8,10,12,14",
},
}),
},
}

Expand Down Expand Up @@ -219,3 +220,12 @@ func berlinTime(value string) time.Time {

return t
}

func initEntry(e *timeperiod.Entry) *timeperiod.Entry {
err := e.Init()
if err != nil {
panic(err)
}

return e
}

0 comments on commit 0d4e144

Please sign in to comment.