Skip to content

Commit

Permalink
Simplify time format setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Eremin committed Nov 18, 2018
1 parent c7e63e2 commit 775191a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
41 changes: 20 additions & 21 deletions mysql/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mysql
import (
"encoding/binary"
"fmt"
"strings"
"time"
)

Expand Down Expand Up @@ -189,14 +188,29 @@ func DecodeTime2(data []byte, dec uint16) (string, int) {
}

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.
type FracTime struct {
time.Time

// Dec must in [0, 6]
Dec int
}

Expand All @@ -205,21 +219,6 @@ func (t FracTime) String() string {
}

func formatZeroTime(frac int, dec int) string {
if dec == 0 {
return "0000-00-00T00:00:00Z"
}

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))
}
// We are going to ignore frac/dec distinction here
return zeroTimes[dec]
}
9 changes: 9 additions & 0 deletions tests/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestDate(t *testing.T) {
defer tbl.drop(t)

vals := []string{
"0000-00-00",
"1000-01-01",
"1234-05-06",
"1500-01-01",
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestTimestamp(t *testing.T) {
defer tbl.drop(t)

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

0 comments on commit 775191a

Please sign in to comment.