1
0
Fork 0

Simplify time format setup

This commit is contained in:
Gregory Eremin 2018-11-18 20:22:22 +01:00
parent 36c3d2add1
commit be37d25444
2 changed files with 29 additions and 21 deletions

View File

@ -3,7 +3,6 @@ package mysql
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"strings"
"time" "time"
) )
@ -189,14 +188,29 @@ func DecodeTime2(data []byte, dec uint16) (string, int) {
} }
var ( var (
fracTimeFormat []string fracTimeFormat = [...]string{
"2006-01-02T15:04:05Z",
"2006-01-02T15:04:05.0Z",
"2006-01-02T15:04:05.00Z",
"2006-01-02T15:04:05.000Z",
"2006-01-02T15:04:05.0000Z",
"2006-01-02T15:04:05.00000Z",
"2006-01-02T15:04:05.000000Z",
}
zeroTimes = [...]string{
"0000-00-00T00:00:00Z",
"0000-00-00T00:00:00.0Z",
"0000-00-00T00:00:00.00Z",
"0000-00-00T00:00:00.000Z",
"0000-00-00T00:00:00.0000Z",
"0000-00-00T00:00:00.00000Z",
"0000-00-00T00:00:00.000000Z",
}
) )
// FracTime is a help structure wrapping Golang Time. // FracTime is a help structure wrapping Golang Time.
type FracTime struct { type FracTime struct {
time.Time time.Time
// Dec must in [0, 6]
Dec int Dec int
} }
@ -205,21 +219,6 @@ func (t FracTime) String() string {
} }
func formatZeroTime(frac int, dec int) string { func formatZeroTime(frac int, dec int) string {
if dec == 0 { // We are going to ignore frac/dec distinction here
return "0000-00-00T00:00:00Z" return zeroTimes[dec]
}
s := fmt.Sprintf("0000-00-00T00:00:00.%06dZ", frac)
// dec must < 6, if frac is 924000, but dec is 3, we must output 924 here.
return s[0 : len(s)-(6-dec)]
}
func init() {
fracTimeFormat = make([]string, 7)
fracTimeFormat[0] = "2006-01-02T15:04:05Z"
for i := 1; i <= 6; i++ {
fracTimeFormat[i] = fmt.Sprintf("2006-01-02T15:04:05.%sZ", strings.Repeat("0", i))
}
} }

View File

@ -23,6 +23,7 @@ func TestDate(t *testing.T) {
defer tbl.drop(t) defer tbl.drop(t)
vals := []string{ vals := []string{
"0000-00-00",
"1000-01-01", "1000-01-01",
"1234-05-06", "1234-05-06",
"1500-01-01", "1500-01-01",
@ -61,6 +62,7 @@ func TestTimestamp(t *testing.T) {
defer tbl.drop(t) defer tbl.drop(t)
vals := []string{ vals := []string{
"0000-00-00T00:00:00Z",
// This is the lowest I could get // This is the lowest I could get
// Spec says 1970-01-01 00:00:01 should be supported // Spec says 1970-01-01 00:00:01 should be supported
"1970-01-01T01:00:01Z", "1970-01-01T01:00:01Z",
@ -81,6 +83,7 @@ func TestTimestamp(t *testing.T) {
func TestDatetime(t *testing.T) { func TestDatetime(t *testing.T) {
inputs := map[string][]string{ inputs := map[string][]string{
"0": { "0": {
"0000-00-00T00:00:00Z",
"1000-01-01T00:00:00Z", "1000-01-01T00:00:00Z",
"1975-01-01T00:00:01Z", "1975-01-01T00:00:01Z",
"1985-01-01T00:00:01Z", "1985-01-01T00:00:01Z",
@ -90,6 +93,7 @@ func TestDatetime(t *testing.T) {
"9999-12-31T23:59:59Z", "9999-12-31T23:59:59Z",
}, },
"1": { "1": {
"0000-00-00T00:00:00.0Z",
"1000-01-01T00:00:00.1Z", "1000-01-01T00:00:00.1Z",
"1975-01-01T00:00:01.1Z", "1975-01-01T00:00:01.1Z",
"1985-01-01T00:00:01.1Z", "1985-01-01T00:00:01.1Z",
@ -99,6 +103,7 @@ func TestDatetime(t *testing.T) {
"9999-12-31T23:59:59.1Z", "9999-12-31T23:59:59.1Z",
}, },
"2": { "2": {
"0000-00-00T00:00:00.00Z",
"1000-01-01T00:00:00.22Z", "1000-01-01T00:00:00.22Z",
"1975-01-01T00:00:01.22Z", "1975-01-01T00:00:01.22Z",
"1985-01-01T00:00:01.22Z", "1985-01-01T00:00:01.22Z",
@ -108,6 +113,7 @@ func TestDatetime(t *testing.T) {
"9999-12-31T23:59:59.22Z", "9999-12-31T23:59:59.22Z",
}, },
"3": { "3": {
"0000-00-00T00:00:00.000Z",
"1000-01-01T00:00:00.333Z", "1000-01-01T00:00:00.333Z",
"1975-01-01T00:00:01.333Z", "1975-01-01T00:00:01.333Z",
"1985-01-01T00:00:01.333Z", "1985-01-01T00:00:01.333Z",
@ -117,6 +123,7 @@ func TestDatetime(t *testing.T) {
"9999-12-31T23:59:59.333Z", "9999-12-31T23:59:59.333Z",
}, },
"4": { "4": {
"0000-00-00T00:00:00.0000Z",
"1000-01-01T00:00:00.4444Z", "1000-01-01T00:00:00.4444Z",
"1975-01-01T00:00:01.4444Z", "1975-01-01T00:00:01.4444Z",
"1985-01-01T00:00:01.4444Z", "1985-01-01T00:00:01.4444Z",
@ -126,6 +133,7 @@ func TestDatetime(t *testing.T) {
"9999-12-31T23:59:59.4444Z", "9999-12-31T23:59:59.4444Z",
}, },
"5": { "5": {
"0000-00-00T00:00:00.00000Z",
"1000-01-01T00:00:00.55555Z", "1000-01-01T00:00:00.55555Z",
"1975-01-01T00:00:01.55555Z", "1975-01-01T00:00:01.55555Z",
"1985-01-01T00:00:01.55555Z", "1985-01-01T00:00:01.55555Z",
@ -135,6 +143,7 @@ func TestDatetime(t *testing.T) {
"9999-12-31T23:59:59.55555Z", "9999-12-31T23:59:59.55555Z",
}, },
"6": { "6": {
"0000-00-00T00:00:00.000000Z",
"1000-01-01T00:00:00.666666Z", "1000-01-01T00:00:00.666666Z",
"1975-01-01T00:00:01.666666Z", "1975-01-01T00:00:01.666666Z",
"1985-01-01T00:00:01.666666Z", "1985-01-01T00:00:01.666666Z",