1
0
Fork 0

Format time per RFC 3339

This commit is contained in:
Gregory Eremin 2018-11-18 20:03:38 +01:00
parent c0a153a532
commit 3cae959b1b
2 changed files with 60 additions and 60 deletions

View File

@ -202,10 +202,10 @@ func (t FracTime) String() string {
func formatZeroTime(frac int, dec int) string {
if dec == 0 {
return "0000-00-00 00:00:00"
return "0000-00-00T00:00:00Z"
}
s := fmt.Sprintf("0000-00-00 00:00:00.%06d", frac)
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)]
@ -213,9 +213,9 @@ func formatZeroTime(frac int, dec int) string {
func init() {
fracTimeFormat = make([]string, 7)
fracTimeFormat[0] = "2006-01-02 15:04:05"
fracTimeFormat[0] = "2006-01-02T15:04:05Z"
for i := 1; i <= 6; i++ {
fracTimeFormat[i] = fmt.Sprintf("2006-01-02 15:04:05.%s", strings.Repeat("0", i))
fracTimeFormat[i] = fmt.Sprintf("2006-01-02T15:04:05.%sZ", strings.Repeat("0", i))
}
}

View File

@ -63,13 +63,13 @@ func TestTimestamp(t *testing.T) {
vals := []string{
// This is the lowest I could get
// Spec says 1970-01-01 00:00:01 should be supported
"1970-01-01 01:00:01",
"1975-01-01 00:00:01",
"1985-01-01 00:00:01",
"1999-12-31 23:59:59",
"2018-11-08 19:26:00",
"2038-01-19 03:14:07",
"2038-01-19 04:14:07", // Should be outside supported range? 2038-01-19 03:14:07
"1970-01-01T01:00:01Z",
"1975-01-01T00:00:01Z",
"1985-01-01T00:00:01Z",
"1999-12-31T23:59:59Z",
"2018-11-08T19:26:00Z",
"2038-01-19T03:14:07Z",
"2038-01-19T04:14:07Z", // Should be outside supported range? 2038-01-19 03:14:07
}
for _, v := range vals {
t.Run(v, func(t *testing.T) {
@ -81,67 +81,67 @@ func TestTimestamp(t *testing.T) {
func TestDatetime(t *testing.T) {
inputs := map[string][]string{
"0": {
"1000-01-01 00:00:00",
"1975-01-01 00:00:01",
"1985-01-01 00:00:01",
"1999-12-31 23:59:59",
"2018-11-08 19:26:00",
"2038-01-19 03:14:07",
"9999-12-31 23:59:59",
"1000-01-01T00:00:00Z",
"1975-01-01T00:00:01Z",
"1985-01-01T00:00:01Z",
"1999-12-31T23:59:59Z",
"2018-11-08T19:26:00Z",
"2038-01-19T03:14:07Z",
"9999-12-31T23:59:59Z",
},
"1": {
"1000-01-01 00:00:00.1",
"1975-01-01 00:00:01.1",
"1985-01-01 00:00:01.1",
"1999-12-31 23:59:59.1",
"2018-11-08 19:26:00.1",
"2038-01-19 03:14:07.1",
"9999-12-31 23:59:59.1",
"1000-01-01T00:00:00.1Z",
"1975-01-01T00:00:01.1Z",
"1985-01-01T00:00:01.1Z",
"1999-12-31T23:59:59.1Z",
"2018-11-08T19:26:00.1Z",
"2038-01-19T03:14:07.1Z",
"9999-12-31T23:59:59.1Z",
},
"2": {
"1000-01-01 00:00:00.22",
"1975-01-01 00:00:01.22",
"1985-01-01 00:00:01.22",
"1999-12-31 23:59:59.22",
"2018-11-08 19:26:00.22",
"2038-01-19 03:14:07.22",
"9999-12-31 23:59:59.22",
"1000-01-01T00:00:00.22Z",
"1975-01-01T00:00:01.22Z",
"1985-01-01T00:00:01.22Z",
"1999-12-31T23:59:59.22Z",
"2018-11-08T19:26:00.22Z",
"2038-01-19T03:14:07.22Z",
"9999-12-31T23:59:59.22Z",
},
"3": {
"1000-01-01 00:00:00.333",
"1975-01-01 00:00:01.333",
"1985-01-01 00:00:01.333",
"1999-12-31 23:59:59.333",
"2018-11-08 19:26:00.333",
"2038-01-19 03:14:07.333",
"9999-12-31 23:59:59.333",
"1000-01-01T00:00:00.333Z",
"1975-01-01T00:00:01.333Z",
"1985-01-01T00:00:01.333Z",
"1999-12-31T23:59:59.333Z",
"2018-11-08T19:26:00.333Z",
"2038-01-19T03:14:07.333Z",
"9999-12-31T23:59:59.333Z",
},
"4": {
"1000-01-01 00:00:00.4444",
"1975-01-01 00:00:01.4444",
"1985-01-01 00:00:01.4444",
"1999-12-31 23:59:59.4444",
"2018-11-08 19:26:00.4444",
"2038-01-19 03:14:07.4444",
"9999-12-31 23:59:59.4444",
"1000-01-01T00:00:00.4444Z",
"1975-01-01T00:00:01.4444Z",
"1985-01-01T00:00:01.4444Z",
"1999-12-31T23:59:59.4444Z",
"2018-11-08T19:26:00.4444Z",
"2038-01-19T03:14:07.4444Z",
"9999-12-31T23:59:59.4444Z",
},
"5": {
"1000-01-01 00:00:00.55555",
"1975-01-01 00:00:01.55555",
"1985-01-01 00:00:01.55555",
"1999-12-31 23:59:59.55555",
"2018-11-08 19:26:00.55555",
"2038-01-19 03:14:07.55555",
"9999-12-31 23:59:59.55555",
"1000-01-01T00:00:00.55555Z",
"1975-01-01T00:00:01.55555Z",
"1985-01-01T00:00:01.55555Z",
"1999-12-31T23:59:59.55555Z",
"2018-11-08T19:26:00.55555Z",
"2038-01-19T03:14:07.55555Z",
"9999-12-31T23:59:59.55555Z",
},
"6": {
"1000-01-01 00:00:00.666666",
"1975-01-01 00:00:01.666666",
"1985-01-01 00:00:01.666666",
"1999-12-31 23:59:59.666666",
"2018-11-08 19:26:00.666666",
"2038-01-19 03:14:07.666666",
"9999-12-31 23:59:59.666666",
"1000-01-01T00:00:00.666666Z",
"1975-01-01T00:00:01.666666Z",
"1985-01-01T00:00:01.666666Z",
"1999-12-31T23:59:59.666666Z",
"2018-11-08T19:26:00.666666Z",
"2038-01-19T03:14:07.666666Z",
"9999-12-31T23:59:59.666666Z",
},
}
for length, vals := range inputs {