diff --git a/core/breaker/breaker.go b/core/breaker/breaker.go index 27b8d25b6..f26c81693 100644 --- a/core/breaker/breaker.go +++ b/core/breaker/breaker.go @@ -15,7 +15,6 @@ import ( const ( numHistoryReasons = 5 - timeFormat = "15:04:05" ) // ErrServiceUnavailable is returned when the Breaker state is open. @@ -261,7 +260,7 @@ type errorWindow struct { func (ew *errorWindow) add(reason string) { ew.lock.Lock() - ew.reasons[ew.index] = fmt.Sprintf("%s %s", time.Now().Format(timeFormat), reason) + ew.reasons[ew.index] = fmt.Sprintf("%s %s", time.Now().Format(time.TimeOnly), reason) ew.index = (ew.index + 1) % numHistoryReasons ew.count = min(ew.count+1, numHistoryReasons) ew.lock.Unlock() diff --git a/core/logx/rotatelogger.go b/core/logx/rotatelogger.go index fc9c4fc25..987eb5fcb 100644 --- a/core/logx/rotatelogger.go +++ b/core/logx/rotatelogger.go @@ -18,7 +18,6 @@ import ( ) const ( - dateFormat = "2006-01-02" hoursPerDay = 24 bufferSize = 100 defaultDirMode = 0o755 @@ -116,7 +115,7 @@ func (r *DailyRotateRule) OutdatedFiles() []string { } var buf strings.Builder - boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(dateFormat) + boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(time.DateOnly) buf.WriteString(r.filename) buf.WriteString(r.delimiter) buf.WriteString(boundary) @@ -425,7 +424,7 @@ func compressLogFile(file string) { } func getNowDate() string { - return time.Now().Format(dateFormat) + return time.Now().Format(time.DateOnly) } func getNowDateInRFC3339Format() string { diff --git a/core/logx/rotatelogger_test.go b/core/logx/rotatelogger_test.go index f370aa8cd..a82e89c38 100644 --- a/core/logx/rotatelogger_test.go +++ b/core/logx/rotatelogger_test.go @@ -52,7 +52,7 @@ func TestDailyRotateRuleOutdatedFiles(t *testing.T) { }) t.Run("temp files", func(t *testing.T) { - boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(time.DateOnly) f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) assert.NoError(t, err) _ = f1.Close() @@ -73,7 +73,7 @@ func TestDailyRotateRuleOutdatedFiles(t *testing.T) { func TestDailyRotateRuleShallRotate(t *testing.T) { var rule DailyRotateRule - rule.rotatedTime = time.Now().Add(time.Hour * 24).Format(dateFormat) + rule.rotatedTime = time.Now().Add(time.Hour * 24).Format(time.DateOnly) assert.True(t, rule.ShallRotate(0)) } @@ -117,12 +117,12 @@ func TestSizeLimitRotateRuleOutdatedFiles(t *testing.T) { }) t.Run("temp files", func(t *testing.T) { - boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(time.DateOnly) f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) assert.NoError(t, err) f2, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) assert.NoError(t, err) - boundary1 := time.Now().Add(time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + boundary1 := time.Now().Add(time.Hour * time.Duration(hoursPerDay) * 2).Format(time.DateOnly) f3, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary1) assert.NoError(t, err) t.Cleanup(func() { @@ -144,12 +144,12 @@ func TestSizeLimitRotateRuleOutdatedFiles(t *testing.T) { }) t.Run("no backups", func(t *testing.T) { - boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(time.DateOnly) f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) assert.NoError(t, err) f2, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) assert.NoError(t, err) - boundary1 := time.Now().Add(time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + boundary1 := time.Now().Add(time.Hour * time.Duration(hoursPerDay) * 2).Format(time.DateOnly) f3, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary1) assert.NoError(t, err) t.Cleanup(func() { @@ -319,7 +319,7 @@ func TestRotateLoggerWrite(t *testing.T) { } // the following write calls cannot be changed to Write, because of DATA RACE. logger.write([]byte(`foo`)) - rule.rotatedTime = time.Now().Add(-time.Hour * 24).Format(dateFormat) + rule.rotatedTime = time.Now().Add(-time.Hour * 24).Format(time.DateOnly) logger.write([]byte(`bar`)) logger.Close() logger.write([]byte(`baz`)) @@ -447,7 +447,7 @@ func TestRotateLoggerWithSizeLimitRotateRuleWrite(t *testing.T) { } // the following write calls cannot be changed to Write, because of DATA RACE. logger.write([]byte(`foo`)) - rule.rotatedTime = time.Now().Add(-time.Hour * 24).Format(dateFormat) + rule.rotatedTime = time.Now().Add(-time.Hour * 24).Format(time.DateOnly) logger.write([]byte(`bar`)) logger.Close() logger.write([]byte(`baz`)) diff --git a/core/stat/alert.go b/core/stat/alert.go index bfa3c8b67..a2863f07d 100644 --- a/core/stat/alert.go +++ b/core/stat/alert.go @@ -19,7 +19,6 @@ import ( const ( clusterNameKey = "CLUSTER_NAME" testEnv = "test.v" - timeFormat = "2006-01-02 15:04:05" ) var ( @@ -45,7 +44,7 @@ func Report(msg string) { if fn != nil { reported := lessExecutor.DoOrDiscard(func() { var builder strings.Builder - builder.WriteString(fmt.Sprintln(time.Now().Format(timeFormat))) + builder.WriteString(fmt.Sprintln(time.Now().Format(time.DateTime))) if len(clusterName) > 0 { builder.WriteString(fmt.Sprintf("cluster: %s\n", clusterName)) } diff --git a/tools/goctl/api/swagger/swagger.go b/tools/goctl/api/swagger/swagger.go index 5cc113921..9d76c5da2 100644 --- a/tools/goctl/api/swagger/swagger.go +++ b/tools/goctl/api/swagger/swagger.go @@ -294,7 +294,7 @@ func specExtensions(api apiSpec.Info) (spec.Extensions, *spec.Info) { ext := spec.Extensions{} ext.Add("x-goctl-version", version.BuildVersion) ext.Add("x-description", "This is a goctl generated swagger file.") - ext.Add("x-date", time.Now().Format("2006-01-02 15:04:05")) + ext.Add("x-date", time.Now().Format(time.DateTime)) ext.Add("x-github", "https://github.com/zeromicro/go-zero") ext.Add("x-go-zero-doc", "https://go-zero.dev/")