Skip to content

Commit 033e3a2

Browse files
committed
Added support for ISO 8601 date strings with "Z" appended.
For example: "2019-07-13T16:48:05Z"
1 parent a82764f commit 033e3a2

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

nosqldb/data_ops_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,10 @@ func (suite *DataOpsTestSuite) TestTimestampDataType() {
12961296
tsValue interface{} // Note: specify a string or time.Time value
12971297
shouldSucceed bool
12981298
}{
1299+
{"2019-07-13T16:48:05.123456789Z", true},
1300+
{"2019-07-13T16:48:05Z", true},
12991301
{"2019-07-13T16:48:05.123456789", true},
1302+
{"2019-07-13T16:48:05", true},
13001303
// Use Unix Epoch time
13011304
{epoch, true},
13021305
// 1970-01-01T00:00:00.999999999
@@ -1433,6 +1436,9 @@ func (suite *DataOpsTestSuite) runTimestampTest(table string, ts interface{}, pu
14331436
case string:
14341437
strVal := ts.(string)
14351438
tsTimeVal, err = time.Parse(types.ISO8601Layout, strVal)
1439+
if err != nil {
1440+
tsTimeVal, err = time.Parse(types.ISO8601ZLayout, strVal)
1441+
}
14361442
suite.Require().NoErrorf(err, "failed to parse %q into time.Time, got error: %v", strVal, err)
14371443

14381444
case time.Time:
@@ -1517,7 +1523,7 @@ func (suite *DataOpsTestSuite) runTimestampTest(table string, ts interface{}, pu
15171523
expect = tsTimeVal.Round(time.Nanosecond * d)
15181524
}
15191525

1520-
suite.Truef(expect.Equal(actual), "unexpect timestamp value returned, expect %v, got %v.", expect, actual)
1526+
suite.Truef(expect.Equal(actual), "unexpected timestamp value returned, expect %v, got %v.", expect, actual)
15211527
}
15221528
}
15231529

nosqldb/internal/proto/binary/reader.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ func (r *Reader) ReadFieldValue() (types.FieldValue, error) {
298298
if s == nil {
299299
return nil, errors.New("binary.Reader: invalid Timestamp value")
300300
}
301-
return time.Parse(types.ISO8601Layout, *s)
301+
v, err := time.Parse(types.ISO8601Layout, *s)
302+
if err == nil {
303+
return v, nil
304+
}
305+
return time.Parse(types.ISO8601ZLayout, *s)
302306

303307
case types.Number:
304308
s, err := r.ReadString()

nosqldb/types/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,15 @@ func (ttl TimeToLive) ToDuration() time.Duration {
199199
return time.Duration(numOfHours) * time.Hour
200200
}
201201

202+
// NOTE: the date values in these are not random! They reference specific
203+
// fields based on their numerical strings. For example, "2006" is the
204+
// definition for stdLongYear. These do not follow C-style formatting.
205+
// See the consts in https://golang.org/src/time/format.go for more detail.
206+
202207
// ISO8601Layout represents the ISO 8601 format of Go's reference time.
203208
const ISO8601Layout = "2006-01-02T15:04:05.999999999"
209+
// ISO8601ZLayout includes literal "Z"
210+
const ISO8601ZLayout = "2006-01-02T15:04:05.999999999Z"
204211

205212
// FieldRange defines a range of values to be used in a Client.MultiDelete()
206213
// operation, as specified in MultiDeleteRequest.FieldRange.

0 commit comments

Comments
 (0)