Skip to content

Commit 2086d13

Browse files
authored
Respect system/user timezone in API requests (cli#2630)
* Respect system/user timezone in API requests * Fall back to a known timezone if TZ is not set Co-authored-by: Cristian Dominguez <cristiand391@users.noreply.github.com>
1 parent b5366c6 commit 2086d13

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pkg/cmd/factory/http.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,54 @@ import (
55
"net/http"
66
"os"
77
"strings"
8+
"time"
89

910
"github.com/cli/cli/api"
1011
"github.com/cli/cli/internal/config"
1112
"github.com/cli/cli/internal/ghinstance"
1213
"github.com/cli/cli/pkg/iostreams"
1314
)
1415

16+
var timezoneNames = map[int]string{
17+
-39600: "Pacific/Niue",
18+
-36000: "Pacific/Honolulu",
19+
-34200: "Pacific/Marquesas",
20+
-32400: "America/Anchorage",
21+
-28800: "America/Los_Angeles",
22+
-25200: "America/Chihuahua",
23+
-21600: "America/Chicago",
24+
-18000: "America/Bogota",
25+
-14400: "America/Caracas",
26+
-12600: "America/St_Johns",
27+
-10800: "America/Argentina/Buenos_Aires",
28+
-7200: "Atlantic/South_Georgia",
29+
-3600: "Atlantic/Cape_Verde",
30+
0: "Europe/London",
31+
3600: "Europe/Amsterdam",
32+
7200: "Europe/Athens",
33+
10800: "Europe/Istanbul",
34+
12600: "Asia/Tehran",
35+
14400: "Asia/Dubai",
36+
16200: "Asia/Kabul",
37+
18000: "Asia/Tashkent",
38+
19800: "Asia/Kolkata",
39+
20700: "Asia/Kathmandu",
40+
21600: "Asia/Dhaka",
41+
23400: "Asia/Rangoon",
42+
25200: "Asia/Bangkok",
43+
28800: "Asia/Manila",
44+
31500: "Australia/Eucla",
45+
32400: "Asia/Tokyo",
46+
34200: "Australia/Darwin",
47+
36000: "Australia/Brisbane",
48+
37800: "Australia/Adelaide",
49+
39600: "Pacific/Guadalcanal",
50+
43200: "Pacific/Nauru",
51+
46800: "Pacific/Auckland",
52+
49500: "Pacific/Chatham",
53+
50400: "Pacific/Kiritimati",
54+
}
55+
1556
// generic authenticated HTTP client for commands
1657
func NewHTTPClient(io *iostreams.IOStreams, cfg config.Config, appVersion string, setAccept bool) *http.Client {
1758
var opts []api.ClientOption
@@ -29,6 +70,16 @@ func NewHTTPClient(io *iostreams.IOStreams, cfg config.Config, appVersion string
2970
}
3071
return "", nil
3172
}),
73+
api.AddHeaderFunc("Time-Zone", func(req *http.Request) (string, error) {
74+
if req.Method != "GET" && req.Method != "HEAD" {
75+
if time.Local.String() != "Local" {
76+
return time.Local.String(), nil
77+
}
78+
_, offset := time.Now().Zone()
79+
return timezoneNames[offset], nil
80+
}
81+
return "", nil
82+
}),
3283
)
3384

3485
if setAccept {

0 commit comments

Comments
 (0)