Skip to content

Commit 7646cbc

Browse files
authored
Use .UTC everywhere we use time.Unix (#7066)
time.Unix attaches the local timezone, which can then leak out (e.g. in the alert json). While this is harmless, we should be consistent. Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent c38ca2c commit 7646cbc

File tree

10 files changed

+18
-18
lines changed

10 files changed

+18
-18
lines changed

Diff for: cmd/promtool/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ func QueryLabels(url *url.URL, name string, p printer) int {
571571
func parseTime(s string) (time.Time, error) {
572572
if t, err := strconv.ParseFloat(s, 64); err == nil {
573573
s, ns := math.Modf(t)
574-
return time.Unix(int64(s), int64(ns*float64(time.Second))), nil
574+
return time.Unix(int64(s), int64(ns*float64(time.Second))).UTC(), nil
575575
}
576576
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
577577
return t, nil

Diff for: cmd/promtool/unittest.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func ruleUnitTest(filename string) []error {
8080
}
8181

8282
// Bounds for evaluating the rules.
83-
mint := time.Unix(0, 0)
83+
mint := time.Unix(0, 0).UTC()
8484
maxd := unitTestInp.maxEvalTime()
8585
maxt := mint.Add(maxd)
8686
// Rounding off to nearest Eval time (> maxt).
@@ -232,7 +232,7 @@ func (tg *testGroup) test(mint, maxt time.Time, evalInterval time.Duration, grou
232232
for _, r := range g.Rules() {
233233
if r.LastError() != nil {
234234
errs = append(errs, errors.Errorf(" rule: %s, time: %s, err: %v",
235-
r.Name(), ts.Sub(time.Unix(0, 0)), r.LastError()))
235+
r.Name(), ts.Sub(time.Unix(0, 0).UTC()), r.LastError()))
236236
}
237237
}
238238
}

Diff for: pkg/timestamp/timestamp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ func FromTime(t time.Time) int64 {
2222

2323
// Time returns a new time.Time object from a millisecond timestamp.
2424
func Time(ts int64) time.Time {
25-
return time.Unix(ts/1000, (ts%1000)*int64(time.Millisecond))
25+
return time.Unix(ts/1000, (ts%1000)*int64(time.Millisecond)).UTC()
2626
}

Diff for: promql/test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const (
4545
epsilon = 0.000001 // Relative error allowed for sample values.
4646
)
4747

48-
var testStartTime = time.Unix(0, 0)
48+
var testStartTime = time.Unix(0, 0).UTC()
4949

5050
// Test is a sequence of read and write commands that are run
5151
// against a test storage.
@@ -660,7 +660,7 @@ func (ll *LazyLoader) appendTill(ts int64) error {
660660

661661
// WithSamplesTill loads the samples till given timestamp and executes the given function.
662662
func (ll *LazyLoader) WithSamplesTill(ts time.Time, fn func(error)) {
663-
tsMilli := ts.Sub(time.Unix(0, 0)) / time.Millisecond
663+
tsMilli := ts.Sub(time.Unix(0, 0).UTC()) / time.Millisecond
664664
fn(ll.appendTill(int64(tsMilli)))
665665
}
666666

Diff for: rules/manager.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func (g *Group) evalTimestamp() time.Time {
487487
base = adjNow - (adjNow % int64(g.interval))
488488
)
489489

490-
return time.Unix(0, base+offset)
490+
return time.Unix(0, base+offset).UTC()
491491
}
492492

493493
func nameAndLabels(rule Rule) string {
@@ -747,8 +747,8 @@ func (g *Group) RestoreForState(ts time.Time) {
747747
return
748748
}
749749

750-
downAt := time.Unix(t/1000, 0)
751-
restoredActiveAt := time.Unix(int64(v), 0)
750+
downAt := time.Unix(t/1000, 0).UTC()
751+
restoredActiveAt := time.Unix(int64(v), 0).UTC()
752752
timeSpentPending := downAt.Sub(restoredActiveAt)
753753
timeRemainingPending := alertHoldDuration - timeSpentPending
754754

Diff for: tsdb/cmd/tsdb/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ func printBlocks(blocks []tsdb.BlockReader, humanReadable *bool) {
460460

461461
func getFormatedTime(timestamp int64, humanReadable *bool) string {
462462
if *humanReadable {
463-
return time.Unix(timestamp/1000, 0).String()
463+
return time.Unix(timestamp/1000, 0).UTC().String()
464464
}
465465
return strconv.FormatInt(timestamp, 10)
466466
}

Diff for: web/api/v1/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ func parseTime(s string) (time.Time, error) {
14571457
if t, err := strconv.ParseFloat(s, 64); err == nil {
14581458
s, ns := math.Modf(t)
14591459
ns = math.Round(ns*1000) / 1000
1460-
return time.Unix(int64(s), int64(ns*float64(time.Second))), nil
1460+
return time.Unix(int64(s), int64(ns*float64(time.Second))).UTC(), nil
14611461
}
14621462
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
14631463
return t, nil

Diff for: web/api/v2/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ func extractTimeRange(min, max *time.Time) (mint, maxt time.Time, err error) {
104104
}
105105

106106
var (
107-
minTime = time.Unix(math.MinInt64/1000+62135596801, 0)
108-
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999)
107+
minTime = time.Unix(math.MinInt64/1000+62135596801, 0).UTC()
108+
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999).UTC()
109109
)
110110

111111
var (

Diff for: web/ui/templates/status.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h2 id="runtime">Runtime Information</h2>
77
<tbody>
88
<tr>
99
<th>Uptime</th>
10-
<td>{{.Birth.UTC}}</td>
10+
<td>{{.Birth}}</td>
1111
</tr>
1212
<tr>
1313
<th>Working Directory</th>
@@ -19,7 +19,7 @@ <h2 id="runtime">Runtime Information</h2>
1919
</tr>
2020
<tr>
2121
<th>Last successful configuration reload</th>
22-
<td>{{.LastConfigTime.UTC}}</td>
22+
<td>{{.LastConfigTime}}</td>
2323
</tr>
2424
<tr>
2525
<th>WAL corruptions</th>

Diff for: web/web.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func New(logger log.Logger, o *Options) *Handler {
272272
reloadCh: make(chan chan error),
273273
options: o,
274274
versionInfo: o.Version,
275-
birth: time.Now(),
275+
birth: time.Now().UTC(),
276276
cwd: cwd,
277277
flagsMap: o.Flags,
278278

@@ -780,7 +780,7 @@ func (h *Handler) status(w http.ResponseWriter, r *http.Request) {
780780
case "prometheus_config_last_reload_successful":
781781
status.ReloadConfigSuccess = toFloat64(mF) != 0
782782
case "prometheus_config_last_reload_success_timestamp_seconds":
783-
status.LastConfigTime = time.Unix(int64(toFloat64(mF)), 0)
783+
status.LastConfigTime = time.Unix(int64(toFloat64(mF)), 0).UTC()
784784
}
785785
}
786786
db := h.tsdb()
@@ -829,7 +829,7 @@ func (h *Handler) runtimeInfo() (api_v1.RuntimeInfo, error) {
829829
case "prometheus_config_last_reload_successful":
830830
status.ReloadConfigSuccess = toFloat64(mF) != 0
831831
case "prometheus_config_last_reload_success_timestamp_seconds":
832-
status.LastConfigTime = time.Unix(int64(toFloat64(mF)), 0)
832+
status.LastConfigTime = time.Unix(int64(toFloat64(mF)), 0).UTC()
833833
}
834834
}
835835
return status, nil

0 commit comments

Comments
 (0)