Simplify time format setup
This commit is contained in:
		
							parent
							
								
									36c3d2add1
								
							
						
					
					
						commit
						be37d25444
					
				@ -3,7 +3,6 @@ package mysql
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/binary"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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",
 | 
			
		||||
@ -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",
 | 
			
		||||
@ -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",
 | 
			
		||||
@ -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",
 | 
			
		||||
@ -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",
 | 
			
		||||
@ -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",
 | 
			
		||||
@ -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",
 | 
			
		||||
@ -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",
 | 
			
		||||
@ -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",
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user