Skip to content

Commit

Permalink
Respect the timezone passed when retrieving
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespfennell committed Jan 23, 2022
1 parent 137e4f4 commit d16e845
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
7 changes: 1 addition & 6 deletions hoard.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ func timeToHour(t *time.Time) *hour.Hour {
if t == nil {
return nil
}
hr := hour.Date(
t.Year(),
t.Month(),
t.Day(),
t.Hour(),
)
hr := hour.FromTime(*t)
return &hr
}
10 changes: 10 additions & 0 deletions internal/storage/hour/hour.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ func Date(year int, month time.Month, day, hour int) Hour {
return Hour{time.Date(year, month, day, hour, 0, 0, 0, time.UTC)}
}

func FromTime(t time.Time) Hour {
t = t.In(time.UTC)
return Date(
t.Year(),
t.Month(),
t.Day(),
t.Hour(),
)
}

func formatInt(i int) string {
if i < 10 {
return "0" + strconv.Itoa(i)
Expand Down
21 changes: 21 additions & 0 deletions internal/storage/hour/hour_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hour

import (
"testing"
"time"
)

func TestFromTime(t *testing.T) {
loc, err := time.LoadLocation("America/New_York")
if err != nil {
t.Fatalf("could not load America/New_York location")
}
nyTime := time.Date(2022, time.January, 22, 21, 49, 0, 0, loc)
utcTime := time.Date(2022, time.January, 23, 2, 49, 0, 0, time.UTC)

nyHour := FromTime(nyTime)
utcHour := FromTime(utcTime)
if nyHour != utcHour {
t.Errorf("don't match: ny=%s, utc=%s", nyHour, utcHour)
}
}

0 comments on commit d16e845

Please sign in to comment.