mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [datetime] reject non-positive intervals (#386)
This commit is contained in:
1 parent
f1db13387b
commit
5c04ffae97
2 files changed
+20
No files matched your search
@@ -495,6 +495,9 @@ func GenerateDatetimesBetween(start, end time.Time, layout string, interval stri
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if duration <= 0 {
|
||||||
|
return nil, fmt.Errorf("interval must be positive")
|
||||||
|
}
|
||||||
|
|
||||||
for current := start; !current.After(end); current = current.Add(duration) {
|
for current := start; !current.After(end); current = current.Add(duration) {
|
||||||
result = append(result, current.Format(layout))
|
result = append(result, current.Format(layout))
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package datetime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGenerateDatetimesNonPositiveInterval(t *testing.T) {
|
||||||
|
start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
for _, interval := range []string{"0s", "-1h"} {
|
||||||
|
t.Run(interval, func(t *testing.T) {
|
||||||
|
if _, err := GenerateDatetimesBetween(start, start.Add(time.Hour), "2006-01-02", interval); err == nil {
|
||||||
|
t.Error("expected an error for a non-positive interval")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user