Skip to content

Commit

Permalink
golangci-lint: support durationcheck (#30027)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangggong authored Dec 14, 2021
1 parent 4b48e55 commit 950a274
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ linters:
- rowserrcheck
- unconvert
- makezero
- durationcheck

linters-settings:
staticcheck:
Expand Down
8 changes: 4 additions & 4 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,16 @@ func dumpBinaryTime(dur time.Duration) (data []byte) {
dur = -dur
}
days := dur / (24 * time.Hour)
dur -= days * 24 * time.Hour
dur -= days * 24 * time.Hour //nolint:durationcheck
data[2] = byte(days)
hours := dur / time.Hour
dur -= hours * time.Hour
dur -= hours * time.Hour //nolint:durationcheck
data[6] = byte(hours)
minutes := dur / time.Minute
dur -= minutes * time.Minute
dur -= minutes * time.Minute //nolint:durationcheck
data[7] = byte(minutes)
seconds := dur / time.Second
dur -= seconds * time.Second
dur -= seconds * time.Second //nolint:durationcheck
data[8] = byte(seconds)
if dur == 0 {
data[0] = 8
Expand Down
3 changes: 2 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,8 @@ func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex

failpoint.Inject("mockStmtSlow", func(val failpoint.Value) {
if strings.Contains(stmtNode.Text(), "/* sleep */") {
time.Sleep(time.Duration(val.(int)) * time.Millisecond)
v, _ := val.(int)
time.Sleep(time.Duration(v) * time.Millisecond)
}
})

Expand Down
22 changes: 11 additions & 11 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ const (
// MaxDuration is the maximum for duration.
MaxDuration int64 = 838*10000 + 59*100 + 59
// MinTime is the minimum for mysql time type.
MinTime = -gotime.Duration(838*3600+59*60+59) * gotime.Second
MinTime = -(838*gotime.Hour + 59*gotime.Minute + 59*gotime.Second)
// MaxTime is the maximum for mysql time type.
MaxTime = gotime.Duration(838*3600+59*60+59) * gotime.Second
MaxTime = 838*gotime.Hour + 59*gotime.Minute + 59*gotime.Second
// ZeroDatetimeStr is the string representation of a zero datetime.
ZeroDatetimeStr = "0000-00-00 00:00:00"
// ZeroDateStr is the string representation of a zero date.
Expand Down Expand Up @@ -466,7 +466,7 @@ func (t Time) ConvertToDuration() (Duration, error) {
hour, minute, second := t.Clock()
frac := t.Microsecond() * 1000

d := gotime.Duration(hour*3600+minute*60+second)*gotime.Second + gotime.Duration(frac)
d := gotime.Duration(hour*3600+minute*60+second)*gotime.Second + gotime.Duration(frac) //nolint:durationcheck
// TODO: check convert validation
return Duration{Duration: d, Fsp: t.Fsp()}, nil
}
Expand Down Expand Up @@ -579,7 +579,7 @@ func RoundFrac(t gotime.Time, fsp int8) (gotime.Time, error) {
if err != nil {
return t, errors.Trace(err)
}
return t.Round(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond), nil
return t.Round(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond), nil //nolint:durationcheck
}

// TruncateFrac truncates fractional seconds precision with new fsp and returns a new one.
Expand All @@ -589,7 +589,7 @@ func TruncateFrac(t gotime.Time, fsp int8) (gotime.Time, error) {
if _, err := CheckFsp(int(fsp)); err != nil {
return t, err
}
return t.Truncate(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond), nil
return t.Truncate(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond), nil //nolint:durationcheck
}

// ToPackedUint encodes Time to a packed uint64 value.
Expand Down Expand Up @@ -1270,7 +1270,7 @@ func AdjustYear(y int64, adjustZero bool) (int64, error) {
// NewDuration construct duration with time.
func NewDuration(hour, minute, second, microsecond int, fsp int8) Duration {
return Duration{
Duration: gotime.Duration(hour)*gotime.Hour + gotime.Duration(minute)*gotime.Minute + gotime.Duration(second)*gotime.Second + gotime.Duration(microsecond)*gotime.Microsecond,
Duration: gotime.Duration(hour)*gotime.Hour + gotime.Duration(minute)*gotime.Minute + gotime.Duration(second)*gotime.Second + gotime.Duration(microsecond)*gotime.Microsecond, //nolint:durationcheck
Fsp: fsp,
}
}
Expand Down Expand Up @@ -1490,7 +1490,7 @@ func (d Duration) RoundFrac(fsp int8, loc *gotime.Location) (Duration, error) {
}

n := gotime.Date(0, 0, 0, 0, 0, 0, 0, tz)
nd := n.Add(d.Duration).Round(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond).Sub(n)
nd := n.Add(d.Duration).Round(gotime.Duration(math.Pow10(9-int(fsp))) * gotime.Nanosecond).Sub(n) //nolint:durationcheck
return Duration{Duration: nd, Fsp: fsp}, nil
}

Expand Down Expand Up @@ -1711,7 +1711,7 @@ func matchDuration(str string, fsp int8) (Duration, error) {
return Duration{t, fsp}, ErrTruncatedWrongVal.GenWithStackByArgs("time", str)
}

d := gotime.Duration(hhmmss[0]*3600+hhmmss[1]*60+hhmmss[2])*gotime.Second + gotime.Duration(frac)*gotime.Microsecond
d := gotime.Duration(hhmmss[0]*3600+hhmmss[1]*60+hhmmss[2])*gotime.Second + gotime.Duration(frac)*gotime.Microsecond //nolint:durationcheck
if negative {
d = -d
}
Expand Down Expand Up @@ -1800,11 +1800,11 @@ func splitDuration(t gotime.Duration) (int, int, int, int, int) {
}

hours := t / gotime.Hour
t -= hours * gotime.Hour
t -= hours * gotime.Hour //nolint:durationcheck
minutes := t / gotime.Minute
t -= minutes * gotime.Minute
t -= minutes * gotime.Minute //nolint:durationcheck
seconds := t / gotime.Second
t -= seconds * gotime.Second
t -= seconds * gotime.Second //nolint:durationcheck
fraction := t / gotime.Microsecond

return sign, int(hours), int(minutes), int(seconds), int(fraction)
Expand Down
2 changes: 1 addition & 1 deletion util/execdetails/execdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ func FormatDuration(d time.Duration) string {
if unit == time.Nanosecond {
return d.String()
}
integer := (d / unit) * unit
integer := (d / unit) * unit //nolint:durationcheck
decimal := float64(d%unit) / float64(unit)
if d < 10*unit {
decimal = math.Round(decimal*100) / 100
Expand Down

0 comments on commit 950a274

Please sign in to comment.