-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: log based on user provided date range
- Loading branch information
Showing
11 changed files
with
310 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ jobs: | |
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ui | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
timePeriodHoursUpperBound = 168 | ||
) | ||
|
||
var ( | ||
timePeriodNotValidErr = errors.New("time period is not valid") | ||
timePeriodTooLargeErr = fmt.Errorf("time period is too large; maximum allowed time period is %d hours", timePeriodHoursUpperBound) | ||
) | ||
|
||
type timePeriod struct { | ||
start time.Time | ||
end time.Time | ||
} | ||
|
||
func parseDateDuration(period string) (timePeriod, error) { | ||
var tp timePeriod | ||
|
||
elements := strings.Split(period, "...") | ||
if len(elements) != 2 { | ||
return tp, timePeriodNotValidErr | ||
} | ||
|
||
start, err := time.ParseInLocation(string(dateFormat), elements[0], time.Local) | ||
if err != nil { | ||
return tp, timePeriodNotValidErr | ||
} | ||
|
||
end, err := time.ParseInLocation(string(dateFormat), elements[1], time.Local) | ||
if err != nil { | ||
return tp, timePeriodNotValidErr | ||
} | ||
|
||
if end.Sub(start) <= 0 { | ||
return tp, timePeriodNotValidErr | ||
} | ||
|
||
if end.Sub(start).Hours() >= timePeriodHoursUpperBound { | ||
return tp, timePeriodTooLargeErr | ||
} | ||
|
||
tp.start = start | ||
tp.end = end | ||
|
||
return tp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package ui | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseDateDuration(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expectedStartStr string | ||
expectedEndStr string | ||
err error | ||
}{ | ||
// success | ||
{ | ||
name: "a range of 1 day", | ||
input: "2024/06/10...2024/06/11", | ||
expectedStartStr: "2024/06/10 00:00", | ||
expectedEndStr: "2024/06/11 00:00", | ||
}, | ||
{ | ||
name: "a range of 3 days", | ||
input: "2024/06/29...2024/07/01", | ||
expectedStartStr: "2024/06/29 00:00", | ||
expectedEndStr: "2024/07/01 00:00", | ||
}, | ||
// failures | ||
{ | ||
name: "empty string", | ||
input: "", | ||
err: timePeriodNotValidErr, | ||
}, | ||
{ | ||
name: "only one date", | ||
input: "2024/06/10", | ||
err: timePeriodNotValidErr, | ||
}, | ||
{ | ||
name: "badly formatted start date", | ||
input: "2024/0610...2024/06/10", | ||
err: timePeriodNotValidErr, | ||
}, | ||
{ | ||
name: "badly formatted end date", | ||
input: "2024/06/10...2024/0610", | ||
err: timePeriodNotValidErr, | ||
}, | ||
{ | ||
name: "a range of 0 days", | ||
input: "2024/06/10...2024/06/10", | ||
err: timePeriodNotValidErr, | ||
}, | ||
{ | ||
name: "end date before start date", | ||
input: "2024/06/10...2024/06/08", | ||
err: timePeriodNotValidErr, | ||
}, | ||
{ | ||
name: "a range of 8 days", | ||
input: "2024/06/29...2024/07/06", | ||
err: timePeriodTooLargeErr, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := parseDateDuration(tt.input) | ||
|
||
startStr := got.start.Format(timeFormat) | ||
endStr := got.end.Format(timeFormat) | ||
|
||
if tt.err == nil { | ||
assert.Equal(t, tt.expectedStartStr, startStr) | ||
assert.Equal(t, tt.expectedEndStr, endStr) | ||
assert.Nil(t, err) | ||
} else { | ||
assert.Equal(t, tt.err, err) | ||
} | ||
|
||
}) | ||
} | ||
|
||
} |
Oops, something went wrong.