🐛 Only truncate minutes and seconds

Also ensure we're getting date.Time for specific times
This commit is contained in:
Dan Jones 2024-02-25 09:53:52 -06:00
commit 44e79916d3
2 changed files with 33 additions and 14 deletions

View file

@ -9,14 +9,24 @@ import (
"github.com/stretchr/testify/require"
)
const day = time.Hour * 24
func TestParseDate(t *testing.T) {
now := time.Now().Local()
y, mon, d, h, loc := now.Year(), now.Month(), now.Day(), now.Hour(), now.Location()
sec := now.Truncate(time.Second)
today := now.Truncate(day)
today := time.Date(y, mon, d, 0, 0, 0, 0, loc)
tomorrow := today.Add(day)
yesterday := today.Add(-day)
twoMin := now.Add(2 * time.Minute).Truncate(time.Minute)
twoHour := now.Add(2 * time.Hour).Truncate(time.Hour)
twoHour := time.Date(y, mon, d, h+2, 0, 0, 0, loc)
firstMonth := time.Date(y, mon, 1, 0, 0, 0, 0, loc)
firstYear := time.Date(y, 1, 1, 0, 0, 0, 0, loc)
exact := "2075-02-12T12:13:54.536-02:00"
exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -2*60*60))
var ts int64 = 1708876012
tsd := time.Unix(ts, 0)
tests := []struct {
name string
exp time.Time
@ -28,8 +38,12 @@ func TestParseDate(t *testing.T) {
{"yesterday", yesterday, ""},
{"in two minutes", twoMin, ""},
{"in two hours", twoHour, ""},
{"this month", firstMonth, ""},
{"this year", firstYear, ""},
{"min", MinTime, ""},
{"max", MaxTime, ""},
{exact, exactd, ""},
{fmt.Sprint(ts), tsd, ""},
{"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")},
}
for _, tt := range tests {