Skip to content

Commit

Permalink
Make efficient use of decimal APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Apr 23, 2024
1 parent 442c248 commit b9e929d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions go/mysql/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,22 +722,26 @@ func NewTimeFromStd(t time.Time) Time {
}
}

var (
decSecondsInHour = decimal.NewFromInt(3600)
decMinutesInHour = decimal.NewFromInt(60)
decMaxHours = decimal.NewFromInt(MaxHours)
)

func NewTimeFromSeconds(seconds decimal.Decimal) Time {
var neg bool
if seconds.Cmp(decimal.NewFromInt(0)) < 0 {
if seconds.Sign() < 0 {
neg = true
seconds = seconds.Mul(decimal.NewFromInt(-1))
seconds = seconds.Abs()
}

sec, frac := seconds.QuoRem(decimal.New(1, 0), 0)
ns := frac.Mul(decimal.New(1, 9))

h := sec.Div(decimal.NewFromInt(3600), 0)
_, sec = sec.QuoRem(decimal.NewFromInt(3600), 0)
min := sec.Div(decimal.NewFromInt(60), 0)
_, sec = sec.QuoRem(decimal.NewFromInt(60), 0)
h, sec := sec.QuoRem(decSecondsInHour, 0)
min, sec := sec.QuoRem(decMinutesInHour, 0)

if h.Cmp(decimal.NewFromInt(MaxHours)) > 0 {
if h.Cmp(decMaxHours) > 0 {
h := uint16(MaxHours)
if neg {
h |= negMask
Expand Down

0 comments on commit b9e929d

Please sign in to comment.