1515package types
1616
1717import (
18- "fmt"
1918 "math"
2019 "reflect"
2120 "strconv"
@@ -263,7 +262,7 @@ func (t TimespanType_) Promote() sql.Type {
263262}
264263
265264// SQL implements Type interface.
266- func (t TimespanType_ ) SQL (ctx * sql.Context , dest []byte , v interface {}) (sqltypes.Value , error ) {
265+ func (t TimespanType_ ) SQL (_ * sql.Context , dest []byte , v interface {}) (sqltypes.Value , error ) {
267266 if v == nil {
268267 return sqltypes .NULL , nil
269268 }
@@ -272,7 +271,7 @@ func (t TimespanType_) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltyp
272271 return sqltypes.Value {}, err
273272 }
274273
275- val := AppendAndSliceString (dest , ti .String ())
274+ val := AppendAndSliceBytes (dest , ti .Bytes ())
276275
277276 return sqltypes .MakeTrusted (sqltypes .Time , val ), nil
278277}
@@ -463,15 +462,76 @@ func (t Timespan) timespanToUnits() (isNegative bool, hours int16, minutes int8,
463462
464463// String returns the Timespan formatted as a string (such as for display purposes).
465464func (t Timespan ) String () string {
465+ return string (t .Bytes ())
466+ }
467+
468+ func (t Timespan ) Bytes () []byte {
466469 isNegative , hours , minutes , seconds , microseconds := t .timespanToUnits ()
467- sign := ""
470+ ret := make ([]byte , 1000 )
471+ i := 0
468472 if isNegative {
469- sign = "-"
473+ ret [0 ] = '-'
474+ i ++
475+ }
476+
477+ var tmpBuf []byte
478+ // hours
479+ if hours == 0 {
480+ ret [i ] = '0'
481+ ret [i + 1 ] = '0'
482+ i += 2
483+ } else if hours < 10 {
484+ ret [i ] = '0'
485+ i ++
486+ } else {
487+ tmpBuf = strconv .AppendInt (ret [i :i ], int64 (hours ), 10 )
488+ i += len (tmpBuf )
489+ }
490+ ret [i ] = ':'
491+ i ++
492+
493+ if minutes == 0 {
494+ ret [i ] = '0'
495+ ret [i + 1 ] = '0'
496+ i += 2
497+ } else if minutes < 10 {
498+ ret [i ] = '0'
499+ i ++
500+ } else {
501+ tmpBuf = strconv .AppendInt (ret [i :i ], int64 (minutes ), 10 )
502+ i += len (tmpBuf )
503+ }
504+ ret [i ] = ':'
505+ i ++
506+
507+ if seconds == 0 {
508+ ret [i ] = '0'
509+ ret [i + 1 ] = '0'
510+ i += 2
511+ } else if seconds < 10 {
512+ ret [i ] = '0'
513+ i ++
514+ } else {
515+ tmpBuf = strconv .AppendInt (ret [i :i ], int64 (seconds ), 10 )
516+ i += len (tmpBuf )
470517 }
471- if microseconds == 0 {
472- return fmt .Sprintf ("%v%02d:%02d:%02d" , sign , hours , minutes , seconds )
518+ if microseconds > 0 {
519+ ret [i ] = '.'
520+ i ++
521+ if microseconds == 0 {
522+ ret [i ] = '0'
523+ ret [i + 1 ] = '0'
524+ i += 2
525+ } else if microseconds < 10 {
526+ ret [i ] = '0'
527+ i ++
528+ } else {
529+ tmpBuf = strconv .AppendInt (ret [i :i ], int64 (microseconds ), 10 )
530+ i += len (tmpBuf )
531+ }
473532 }
474- return fmt .Sprintf ("%v%02d:%02d:%02d.%06d" , sign , hours , minutes , seconds , microseconds )
533+
534+ return ret [:i ]
475535}
476536
477537// AsMicroseconds returns the Timespan in microseconds.
0 commit comments