Skip to content

Commit

Permalink
Rename NewTimeFromSeconds func and define datetime.MaxHours
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Apr 20, 2024
1 parent d62a741 commit 442c248
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
11 changes: 7 additions & 4 deletions go/mysql/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ type DateTime struct {
Time Time
}

const DefaultPrecision = 6
const (
DefaultPrecision = 6
MaxHours = 838
)

func (t Time) AppendFormat(b []byte, prec uint8) []byte {
if t.Neg() {
Expand Down Expand Up @@ -719,7 +722,7 @@ func NewTimeFromStd(t time.Time) Time {
}
}

func NewTimeFromSecondsDecimal(seconds decimal.Decimal) Time {
func NewTimeFromSeconds(seconds decimal.Decimal) Time {
var neg bool
if seconds.Cmp(decimal.NewFromInt(0)) < 0 {
neg = true
Expand All @@ -734,8 +737,8 @@ func NewTimeFromSecondsDecimal(seconds decimal.Decimal) Time {
min := sec.Div(decimal.NewFromInt(60), 0)
_, sec = sec.QuoRem(decimal.NewFromInt(60), 0)

if h.Cmp(decimal.NewFromInt(839)) >= 0 {
h := uint16(838)
if h.Cmp(decimal.NewFromInt(MaxHours)) > 0 {
h := uint16(MaxHours)
if neg {
h |= negMask
}
Expand Down
2 changes: 1 addition & 1 deletion go/mysql/datetime/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func ParseTimeInt64(i int64) (t Time, ok bool) {
return t, false
}

if i > 838 {
if i > MaxHours {
return t, false
}
t.hour = uint16(i)
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/evalengine/compiler_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4109,7 +4109,7 @@ func (asm *assembler) Fn_SEC_TO_TIME_D() {
prec := int(e.prec)

sec := newEvalDecimalWithPrec(e.toDecimal(), int32(prec))
env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalTime(datetime.NewTimeFromSecondsDecimal(sec.dec), prec)
env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalTime(datetime.NewTimeFromSeconds(sec.dec), prec)
return 1
}, "FN SEC_TO_TIME TEMPORAL(SP-1)")
}
Expand All @@ -4119,7 +4119,7 @@ func (asm *assembler) Fn_SEC_TO_TIME_d() {
e := env.vm.stack[env.vm.sp-1].(*evalDecimal)
prec := min(evalDecimalPrecision(e), datetime.DefaultPrecision)

env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalTime(datetime.NewTimeFromSecondsDecimal(e.dec), int(prec))
env.vm.stack[env.vm.sp-1] = env.vm.arena.newEvalTime(datetime.NewTimeFromSeconds(e.dec), int(prec))
return 1
}, "FN SEC_TO_TIME DECIMAL(SP-1)")
}
Expand Down
8 changes: 4 additions & 4 deletions go/vt/vtgate/evalengine/fn_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,12 +891,12 @@ func (call *builtinMakedate) compile(c *compiler) (ctype, error) {

func clampHourMinute(h, m int64) (int64, int64, bool, bool) {
var clamped bool
if h > 838 || h < -838 {
if h > datetime.MaxHours || h < -datetime.MaxHours {
clamped = true
if h > 0 {
h = 838
h = datetime.MaxHours
} else {
h = -838
h = -datetime.MaxHours
}
m = 59
}
Expand Down Expand Up @@ -1409,7 +1409,7 @@ func (b *builtinSecToTime) eval(env *ExpressionEnv) (eval, error) {
}

prec = min(int(evalDecimalPrecision(e)), prec)
return newEvalTime(datetime.NewTimeFromSecondsDecimal(e.dec), prec), nil
return newEvalTime(datetime.NewTimeFromSeconds(e.dec), prec), nil
}

func (call *builtinSecToTime) compile(c *compiler) (ctype, error) {
Expand Down

0 comments on commit 442c248

Please sign in to comment.