godev/cmd/worker: replace "2006-01-02" with time.DateOnly
The time.DateOnly is introduced in go1.20 unless there is a version
compatibility promise (like gopls), it's recommended to migrate to
time.DateOnly.
Change-Id: Idcf27e2205ae417ff16021789c80e2835bf766a8
Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/597696
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/cmd/gotelemetry/internal/view/view.go b/cmd/gotelemetry/internal/view/view.go
index dacb1dd..4d0881c 100644
--- a/cmd/gotelemetry/internal/view/view.go
+++ b/cmd/gotelemetry/internal/view/view.go
@@ -437,7 +437,7 @@
// parseReportDate parses the date string in the format
// used byt the telemetry report.
func parseReportDate(s string) (time.Time, error) {
- return time.Parse("2006-01-02", s)
+ return time.Parse(time.DateOnly, s)
}
// charts returns chartdata for a set of telemetry reports. It uses the config
@@ -580,7 +580,7 @@
log.Printf("skipping malformed %v: unexpected TimeEnd value %q", f.ID, f.Meta["TimeEnd"])
continue
}
- week := tb.Format("2006-01-02")
+ week := tb.Format(time.DateOnly)
if _, ok := reports[week]; !ok {
reports[week] = &telemetry.Report{Week: week}
}
diff --git a/cmd/gotelemetry/internal/view/view_test.go b/cmd/gotelemetry/internal/view/view_test.go
index c8e4b73..7de8239 100644
--- a/cmd/gotelemetry/internal/view/view_test.go
+++ b/cmd/gotelemetry/internal/view/view_test.go
@@ -100,7 +100,7 @@
func Test_reportsDomain(t *testing.T) {
mustParseDate := func(date string) time.Time {
- ts, err := time.Parse("2006-01-02", date)
+ ts, err := time.Parse(time.DateOnly, date)
if err != nil {
t.Fatalf("failed to parse date %q: %v", date, err)
}
diff --git a/godev/cmd/telemetrygodev/main.go b/godev/cmd/telemetrygodev/main.go
index d09492e..0149804 100644
--- a/godev/cmd/telemetrygodev/main.go
+++ b/godev/cmd/telemetrygodev/main.go
@@ -196,7 +196,7 @@
// validate validates the telemetry report data against the latest config.
func validate(r *telemetry.Report, cfg *tconfig.Config) error {
// TODO: reject/drop data arrived too early or too late.
- if _, err := time.Parse("2006-01-02", r.Week); err != nil {
+ if _, err := time.Parse(time.DateOnly, r.Week); err != nil {
return fmt.Errorf("invalid week %s", r.Week)
}
if !semver.IsValid(r.Config) {
diff --git a/godev/cmd/worker/main.go b/godev/cmd/worker/main.go
index 5b45d74..273d326 100644
--- a/godev/cmd/worker/main.go
+++ b/godev/cmd/worker/main.go
@@ -81,14 +81,14 @@
return func(w http.ResponseWriter, r *http.Request) error {
now := time.Now().UTC()
for i := 7; i > 0; i-- {
- date := now.AddDate(0, 0, -1*i).Format("2006-01-02")
+ date := now.AddDate(0, 0, -1*i).Format(time.DateOnly)
url := cfg.WorkerURL + "/merge/?date=" + date
if _, err := createHTTPTask(cfg, url); err != nil {
return err
}
}
for i := 8; i > 1; i-- {
- date := now.AddDate(0, 0, -1*i).Format("2006-01-02")
+ date := now.AddDate(0, 0, -1*i).Format(time.DateOnly)
url := cfg.WorkerURL + "/chart/?date=" + date
if _, err := createHTTPTask(cfg, url); err != nil {
return err
@@ -139,7 +139,7 @@
return func(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
date := r.URL.Query().Get("date")
- if _, err := time.Parse("2006-01-02", date); err != nil {
+ if _, err := time.Parse(time.DateOnly, date); err != nil {
return content.Error(err, http.StatusBadRequest)
}
it := s.Upload.Objects(ctx, date)
@@ -188,7 +188,7 @@
ctx := r.Context()
// TODO: use start date and end date to create a timeseries of data.
date := r.URL.Query().Get("date")
- if _, err := time.Parse("2006-01-02", date); err != nil {
+ if _, err := time.Parse(time.DateOnly, date); err != nil {
return content.Error(err, http.StatusBadRequest)
}
in, err := s.Merge.Object(date + ".json").NewReader(ctx)
diff --git a/internal/counter/file.go b/internal/counter/file.go
index 4fe9e57..56fa516 100644
--- a/internal/counter/file.go
+++ b/internal/counter/file.go
@@ -297,7 +297,7 @@
goVers,
runtime.GOOS,
runtime.GOARCH,
- f.timeBegin.Format("2006-01-02"),
+ f.timeBegin.Format(time.DateOnly),
FileVersion,
)
dir := telemetry.Default.LocalDir()
diff --git a/internal/counter/rotate_test.go b/internal/counter/rotate_test.go
index 4cc1542..edff256 100644
--- a/internal/counter/rotate_test.go
+++ b/internal/counter/rotate_test.go
@@ -75,7 +75,7 @@
// move into the future and rotate the file, remapping it
now = now.Add(7 * 24 * time.Hour)
f.rotate()
- if got, want := f.timeBegin.Format("2006-01-02"), now.Format("2006-01-02"); got != want {
+ if got, want := f.timeBegin.Format(time.DateOnly), now.Format(time.DateOnly); got != want {
t.Errorf("f.timeBegin = %q, want %q", got, want)
}
@@ -167,8 +167,8 @@
t.Fatalf("err=%v, len(fi) = %d, want 2", err, len(fi))
}
x := fi[0].Name()
- y := x[len(x)-len("2006-01-02")-len(".v1.count") : len(x)-len(".v1.count")]
- us, err := time.ParseInLocation("2006-01-02", y, time.UTC)
+ y := x[len(x)-len(time.DateOnly)-len(".v1.count") : len(x)-len(".v1.count")]
+ us, err := time.ParseInLocation(time.DateOnly, y, time.UTC)
if err != nil {
t.Fatal(err)
}
diff --git a/internal/regtest/regtest.go b/internal/regtest/regtest.go
index 11e567a..8e45344 100644
--- a/internal/regtest/regtest.go
+++ b/internal/regtest/regtest.go
@@ -52,7 +52,7 @@
// We are running the separate process that was spawned by RunProg.
fmt.Fprintf(os.Stderr, "running program %q\n", name)
if asofEnvVarValue != "" {
- asof, err := time.Parse("2006-01-02", asofEnvVarValue)
+ asof, err := time.Parse(time.DateOnly, asofEnvVarValue)
if err != nil {
log.Fatalf("error parsing asof time %q: %v", asof, err)
}
@@ -116,7 +116,7 @@
cmd := exec.Command(testBin, "-test.run", fmt.Sprintf("^%s$", testName))
cmd.Env = append(os.Environ(), telemetryDirEnvVar+"="+telemetryDir, entryPointEnvVar+"="+string(prog))
if !asof.IsZero() {
- cmd.Env = append(cmd.Env, asofEnvVar+"="+asof.Format("2006-01-02"))
+ cmd.Env = append(cmd.Env, asofEnvVar+"="+asof.Format(time.DateOnly))
}
return cmd.CombinedOutput()
}
diff --git a/internal/telemetry/dir.go b/internal/telemetry/dir.go
index 915b5ca..dd7a63c 100644
--- a/internal/telemetry/dir.go
+++ b/internal/telemetry/dir.go
@@ -102,9 +102,9 @@
return fmt.Errorf("cannot create a telemetry mode file: %w", err)
}
- asof := asofTime.UTC().Format("2006-01-02")
+ asof := asofTime.UTC().Format(time.DateOnly)
// Defensively guarantee that we can parse the asof time.
- if _, err := time.Parse("2006-01-02", asof); err != nil {
+ if _, err := time.Parse(time.DateOnly, asof); err != nil {
return fmt.Errorf("internal error: invalid mode date %q: %v", asof, err)
}
@@ -136,7 +136,7 @@
//
// If the modefile contains a date, return it.
if idx := strings.Index(mode, " "); idx >= 0 {
- d, err := time.Parse("2006-01-02", mode[idx+1:])
+ d, err := time.Parse(time.DateOnly, mode[idx+1:])
if err != nil {
d = time.Time{}
}
diff --git a/internal/upload/date.go b/internal/upload/date.go
index 22e4e8a..b054b46 100644
--- a/internal/upload/date.go
+++ b/internal/upload/date.go
@@ -19,7 +19,7 @@
// reports that are too old (21 days) are not uploaded
func (u *uploader) tooOld(date string, uploadStartTime time.Time) bool {
- t, err := time.Parse("2006-01-02", date)
+ t, err := time.Parse(time.DateOnly, date)
if err != nil {
u.logger.Printf("tooOld: %v", err)
return false
diff --git a/internal/upload/dates_test.go b/internal/upload/dates_test.go
index 513843e..9c99543 100644
--- a/internal/upload/dates_test.go
+++ b/internal/upload/dates_test.go
@@ -227,7 +227,7 @@
}
func mustParseDate(d string) time.Time {
- x, err := time.Parse("2006-01-02", d)
+ x, err := time.Parse(time.DateOnly, d)
if err != nil {
log.Fatalf("couldn't parse time %s", d)
}
@@ -236,13 +236,13 @@
// return a day more than 'old' before 'today'
func olderThan(t *testing.T, today string, old time.Duration, nm string) string {
- x, err := time.Parse("2006-01-02", today)
+ x, err := time.Parse(time.DateOnly, today)
if err != nil {
t.Errorf("%q not a day in test %s (%v)", today, nm, err)
return today // so test should fail
}
ans := x.Add(-old - 24*time.Hour)
- msg := ans.Format("2006-01-02")
+ msg := ans.Format(time.DateOnly)
return msg
}
diff --git a/internal/upload/reports.go b/internal/upload/reports.go
index 31bda70..8b17e32 100644
--- a/internal/upload/reports.go
+++ b/internal/upload/reports.go
@@ -26,7 +26,7 @@
return nil, nil // no reports
}
thisInstant := u.startTime
- today := thisInstant.Format("2006-01-02")
+ today := thisInstant.Format(time.DateOnly)
lastWeek := latestReport(todo.uploaded)
if lastWeek >= today { //should never happen
lastWeek = ""
diff --git a/internal/upload/upload.go b/internal/upload/upload.go
index aa0f4f0..5643241 100644
--- a/internal/upload/upload.go
+++ b/internal/upload/upload.go
@@ -16,7 +16,7 @@
var (
dateRE = regexp.MustCompile(`(\d\d\d\d-\d\d-\d\d)[.]json$`)
- dateFormat = "2006-01-02"
+ dateFormat = time.DateOnly
// TODO(rfindley): use dateFormat throughout.
)
@@ -41,7 +41,7 @@
// TODO(rfindley): use uploadReportDate here, once we've done a gopls release.
// first make sure it is not in the future
- today := thisInstant.Format("2006-01-02")
+ today := thisInstant.Format(time.DateOnly)
match := dateRE.FindStringSubmatch(fname)
if match == nil || len(match) < 2 {
u.logger.Printf("Report name %q missing date", filepath.Base(fname))
@@ -62,7 +62,7 @@
// try to upload the report, 'true' if successful
func (u *uploader) uploadReportContents(fname string, buf []byte) bool {
fdate := strings.TrimSuffix(filepath.Base(fname), ".json")
- fdate = fdate[len(fdate)-len("2006-01-02"):]
+ fdate = fdate[len(fdate)-len(time.DateOnly):]
newname := filepath.Join(u.dir.UploadDir(), fdate+".json")
diff --git a/start_test.go b/start_test.go
index 2a32d98..d5c8731 100644
--- a/start_test.go
+++ b/start_test.go
@@ -58,7 +58,7 @@
}
// Get the fake time used by all programs.
- asof, err := time.Parse("2006-01-02", mustGetEnv(asofEnv))
+ asof, err := time.Parse(time.DateOnly, mustGetEnv(asofEnv))
if err != nil {
log.Fatalf("parsing %s: %v", asofEnv, err)
}
@@ -101,7 +101,7 @@
cmd := exec.Command(exe, "** TestStart **") // this unused arg is just for ps(1)
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()
- cmd.Env = append(cmd.Env, asofEnv+"="+asof.Format("2006-01-02"))
+ cmd.Env = append(cmd.Env, asofEnv+"="+asof.Format(time.DateOnly))
cmd.Env = append(cmd.Env, telemetryDirEnv+"="+telemetryDir)
cmd.Env = append(cmd.Env, runStartEnv+"="+prog) // see TestMain
cmd.Env = append(cmd.Env, env...)