github.com/dolthub/go-mysql-server@v0.18.0/sql/planbuilder/dateparse/eval.go (about) 1 package dateparse 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 func evaluateDate(dt datetime) string { 9 var year, month, day int 10 11 if dt.year != nil { 12 year = int(*dt.year) 13 } 14 15 if dt.month != nil { 16 month = int(*dt.month) 17 } 18 19 if dt.day != nil { 20 day = int(*dt.day) 21 } 22 23 if dt.dayOfYear != nil { 24 // offset from Jan 1st by the specified number of days 25 dayOffsetted := time.Date(year, time.January, 0, 0, 0, 0, 0, time.Local).AddDate(0, 0, int(*dt.dayOfYear)) 26 month = int(dayOffsetted.Month()) 27 day = dayOffsetted.Day() 28 } 29 30 return fillWithZero(year, 4) + "-" + fillWithZero(month, 2) + "-" + fillWithZero(day, 2) 31 } 32 33 func evaluateTime(dt datetime) string { 34 var hours, minutes, seconds, milliseconds, microseconds, nanoseconds int 35 36 if dt.hours != nil { 37 if *dt.hours < 13 && dt.am != nil && !*dt.am { 38 *dt.hours += 12 39 } 40 hours = int(*dt.hours) 41 } 42 if dt.minutes != nil { 43 minutes = int(*dt.minutes) 44 } 45 if dt.seconds != nil { 46 seconds = int(*dt.seconds) 47 } 48 49 t := fillWithZero(hours, 2) + ":" + fillWithZero(minutes, 2) + ":" + fillWithZero(seconds, 2) 50 51 includeMicrosecond := false 52 if dt.milliseconds != nil { 53 milliseconds = int(*dt.milliseconds) 54 includeMicrosecond = true 55 } 56 if dt.microseconds != nil { 57 microseconds = int(*dt.microseconds) 58 includeMicrosecond = true 59 } 60 if dt.nanoseconds != nil { 61 nanoseconds = int(*dt.nanoseconds) 62 includeMicrosecond = true 63 } 64 65 // convert partial seconds to nanoseconds 66 nanosecondDuration := time.Microsecond*time.Duration(microseconds) + time.Millisecond*time.Duration(milliseconds) + time.Nanosecond*time.Duration(nanoseconds) 67 if includeMicrosecond { 68 t = t + "." + fillWithZero(int(nanosecondDuration), 6) 69 } 70 71 return t 72 } 73 74 func fillWithZero(n int, length int) string { 75 r := fmt.Sprintf("%d", n) 76 if len(r) > length { 77 r = "" 78 } 79 for len(r) < length { 80 r = fmt.Sprintf("0%s", r) 81 } 82 83 return r 84 }