mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: strip timezone information from a date in dau response (#11962)
* fix: strip timezone information from a date in dau response Timezone information is lost, so do not forward it to the client. * fix: timezone offset should be flipped * Make tests deterministic
This commit is contained in:
+16
-6
@@ -2168,8 +2168,10 @@ type DAUsResponse struct {
|
||||
}
|
||||
|
||||
type DAUEntry struct {
|
||||
Date time.Time `json:"date" format:"date-time"`
|
||||
Amount int `json:"amount"`
|
||||
// Date is a string formatted as 2024-01-31.
|
||||
// Timezone and time information is not included.
|
||||
Date string `json:"date"`
|
||||
Amount int `json:"amount"`
|
||||
}
|
||||
|
||||
type DAURequest struct {
|
||||
@@ -2184,14 +2186,22 @@ func (d DAURequest) asRequestOption() RequestOption {
|
||||
}
|
||||
}
|
||||
|
||||
func TimezoneOffsetHour(loc *time.Location) int {
|
||||
// TimezoneOffsetHourWithTime is implemented to match the javascript 'getTimezoneOffset()' function.
|
||||
// This is the amount of time between this date evaluated in UTC and evaluated in the 'loc'
|
||||
// The trivial case of times being on the same day is:
|
||||
// 'time.Now().UTC().Hour() - time.Now().In(loc).Hour()'
|
||||
func TimezoneOffsetHourWithTime(now time.Time, loc *time.Location) int {
|
||||
if loc == nil {
|
||||
// Default to UTC time to be consistent across all callers.
|
||||
loc = time.UTC
|
||||
}
|
||||
_, offsetSec := time.Now().In(loc).Zone()
|
||||
// Convert to hours
|
||||
return offsetSec / 60 / 60
|
||||
_, offsetSec := now.In(loc).Zone()
|
||||
// Convert to hours and flip the sign
|
||||
return -1 * offsetSec / 60 / 60
|
||||
}
|
||||
|
||||
func TimezoneOffsetHour(loc *time.Location) int {
|
||||
return TimezoneOffsetHourWithTime(time.Now(), loc)
|
||||
}
|
||||
|
||||
func (c *Client) DeploymentDAUsLocalTZ(ctx context.Context) (*DAUsResponse, error) {
|
||||
|
||||
+42
-18
@@ -205,6 +205,7 @@ func TestTimezoneOffsets(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
Name string
|
||||
Now time.Time
|
||||
Loc *time.Location
|
||||
ExpectedOffset int
|
||||
}{
|
||||
@@ -213,29 +214,52 @@ func TestTimezoneOffsets(t *testing.T) {
|
||||
Loc: time.UTC,
|
||||
ExpectedOffset: 0,
|
||||
},
|
||||
// The following test cases are broken re: daylight savings
|
||||
//{
|
||||
// Name: "Eastern",
|
||||
// Loc: must(time.LoadLocation("America/New_York")),
|
||||
// ExpectedOffset: -4,
|
||||
// },
|
||||
//{
|
||||
// Name: "Central",
|
||||
// Loc: must(time.LoadLocation("America/Chicago")),
|
||||
// ExpectedOffset: -5,
|
||||
// },
|
||||
//{
|
||||
// Name: "Ireland",
|
||||
// Loc: must(time.LoadLocation("Europe/Dublin")),
|
||||
// ExpectedOffset: 1,
|
||||
// },
|
||||
|
||||
{
|
||||
Name: "Eastern",
|
||||
Now: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC),
|
||||
Loc: must(time.LoadLocation("America/New_York")),
|
||||
ExpectedOffset: 5,
|
||||
},
|
||||
{
|
||||
// Daylight savings is on the 14th of March to Nov 7 in 2021
|
||||
Name: "EasternDaylightSavings",
|
||||
Now: time.Date(2021, 3, 16, 0, 0, 0, 0, time.UTC),
|
||||
Loc: must(time.LoadLocation("America/New_York")),
|
||||
ExpectedOffset: 4,
|
||||
},
|
||||
{
|
||||
Name: "Central",
|
||||
Now: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC),
|
||||
Loc: must(time.LoadLocation("America/Chicago")),
|
||||
ExpectedOffset: 6,
|
||||
},
|
||||
{
|
||||
Name: "CentralDaylightSavings",
|
||||
Now: time.Date(2021, 3, 16, 0, 0, 0, 0, time.UTC),
|
||||
Loc: must(time.LoadLocation("America/Chicago")),
|
||||
ExpectedOffset: 5,
|
||||
},
|
||||
{
|
||||
Name: "Ireland",
|
||||
Now: time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC),
|
||||
Loc: must(time.LoadLocation("Europe/Dublin")),
|
||||
ExpectedOffset: 0,
|
||||
},
|
||||
{
|
||||
Name: "IrelandDaylightSavings",
|
||||
Now: time.Date(2021, 4, 3, 0, 0, 0, 0, time.UTC),
|
||||
Loc: must(time.LoadLocation("Europe/Dublin")),
|
||||
ExpectedOffset: -1,
|
||||
},
|
||||
{
|
||||
Name: "HalfHourTz",
|
||||
Now: time.Date(2024, 1, 20, 6, 0, 0, 0, must(time.LoadLocation("Asia/Yangon"))),
|
||||
// This timezone is +6:30, but the function rounds to the nearest hour.
|
||||
// This is intentional because our DAUs endpoint only covers 1-hour offsets.
|
||||
// If the user is in a non-hour timezone, they get the closest hour bucket.
|
||||
Loc: must(time.LoadLocation("Asia/Yangon")),
|
||||
ExpectedOffset: 6,
|
||||
ExpectedOffset: -6,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -244,7 +268,7 @@ func TestTimezoneOffsets(t *testing.T) {
|
||||
t.Run(c.Name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
offset := codersdk.TimezoneOffsetHour(c.Loc)
|
||||
offset := codersdk.TimezoneOffsetHourWithTime(c.Now, c.Loc)
|
||||
require.Equal(t, c.ExpectedOffset, offset)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user