Skip to content

Commit

Permalink
fix: cmp time in UTC + ns to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
laszukdawid committed Dec 27, 2024
1 parent 67bb02e commit a6a4a12
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21.x
go-version: 1.23.x

# Install go-task
- name: Install Task
Expand Down
2 changes: 1 addition & 1 deletion internal/history/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func strToTime(input string) (*time.Time, error) {
// If the input was in TimeOnly format, set the date to today
if layout == time.TimeOnly {
now := time.Now()
t = time.Date(now.Year(), now.Month(), now.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
t = time.Date(now.Year(), now.Month(), now.Day(), t.Hour(), t.Minute(), t.Second(), 0, t.Location())
}

return &t, nil
Expand Down
26 changes: 13 additions & 13 deletions internal/history/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ func TestAugmentHistoryQuery(t *testing.T) {
BeforeStr: test.beforeStr,
}
err := augmentHistoryQuery(hq)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

assert.Nil(t, err)
assert.Equal(t, test.after, hq.After)
assert.Equal(t, test.before, hq.Before)
})
}
}

func TestStrToTime(t *testing.T) {
nowTime := time.Now()
// Defining location using FixedZone method
hongKong, err := time.LoadLocation("Asia/Hong_Kong")
if err != nil {
t.Fatal(err)
}
nowTime := time.Now().In(hongKong)

tests := []struct {
input string
expected *time.Time
Expand All @@ -54,25 +59,20 @@ func TestStrToTime(t *testing.T) {
{"2024-01-01", timePtr(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC))},
{"12:34:56", timePtr(time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 12, 34, 56, 0, time.UTC))},
{"2024-01-01T12:34:56", timePtr(time.Date(2024, 1, 1, 12, 34, 56, 0, time.UTC))},
{nowTime.Format(time.RFC3339), timePtr(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), nowTime.Hour(), nowTime.Minute(), nowTime.Second(), 0, nowTime.Location()))},
{nowTime.Format(time.RFC3339), timePtr(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), nowTime.Hour(), nowTime.Minute(), nowTime.Second(), 0, hongKong))},
{"invalid", nil},
}

for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
actual, err := strToTime(test.input)
if err != nil {
if test.expected != nil {
t.Errorf("unexpected error: %v", err)
}
return
}
if actual == nil {
t.Error("expected time, got nil")
assert.Nil(t, test.expected, err)
return
}

assert.Equal(t, test.expected, actual)
assert.NotNil(t, actual)
assert.Equal(t, test.expected.UTC(), actual.UTC())
})
}

Expand Down

0 comments on commit a6a4a12

Please sign in to comment.