@@ -36,6 +36,8 @@ const ZeroTimestampDatetimeStr = "0000-00-00 00:00:00"
3636
3737const MinDatetimeStringLength = 8 // length of "2000-1-1"
3838
39+ const MaxDatetimePrecision = 6
40+
3941var (
4042 // ErrConvertingToTime is thrown when a value cannot be converted to a Time
4143 ErrConvertingToTime = errors .NewKind ("Incorrect datetime value: '%v'" )
@@ -105,13 +107,13 @@ var (
105107 // DatetimeDefaultPrecision is a date and a time without a specified precision
106108 DatetimeDefaultPrecision = MustCreateDatetimeType (sqltypes .Datetime , 0 )
107109 // DatetimeMaxPrecision is a date and a time with maximum precision
108- DatetimeMaxPrecision = MustCreateDatetimeType (sqltypes .Datetime , 6 )
110+ DatetimeMaxPrecision = MustCreateDatetimeType (sqltypes .Datetime , MaxDatetimePrecision )
109111 // Timestamp is a UNIX timestamp with default precision (no fractional seconds).
110112 Timestamp = MustCreateDatetimeType (sqltypes .Timestamp , 0 )
111113 // TimestampMaxPrecision is a UNIX timestamp with maximum precision
112- TimestampMaxPrecision = MustCreateDatetimeType (sqltypes .Timestamp , 6 )
114+ TimestampMaxPrecision = MustCreateDatetimeType (sqltypes .Timestamp , MaxDatetimePrecision )
113115 // DatetimeMaxRange is a date and a time with maximum precision and maximum range.
114- DatetimeMaxRange = MustCreateDatetimeType (sqltypes .Datetime , 6 )
116+ DatetimeMaxRange = MustCreateDatetimeType (sqltypes .Datetime , MaxDatetimePrecision )
115117
116118 datetimeValueType = reflect .TypeOf (time.Time {})
117119)
@@ -128,7 +130,7 @@ var _ sql.CollationCoercible = datetimeType{}
128130func CreateDatetimeType (baseType query.Type , precision int ) (sql.DatetimeType , error ) {
129131 switch baseType {
130132 case sqltypes .Date , sqltypes .Datetime , sqltypes .Timestamp :
131- if precision < 0 || precision > 6 {
133+ if precision < 0 || precision > MaxDatetimePrecision {
132134 return nil , fmt .Errorf ("precision must be between 0 and 6, got %d" , precision )
133135 }
134136 return datetimeType {
@@ -221,7 +223,7 @@ func ConvertToTime(ctx context.Context, v interface{}, t datetimeType) (time.Tim
221223 }
222224
223225 // Round the date to the precision of this type
224- if t .precision < 6 {
226+ if t .precision < MaxDatetimePrecision {
225227 truncationDuration := time .Second / time .Duration (precisionConversion [t .precision ])
226228 res = res .Round (truncationDuration )
227229 } else {
0 commit comments