Skip to content

Commit 006a3f4

Browse files
Added code that accounts for the 'Z' timezone separator in the ParseTimestamp func. (#1073)
Co-authored-by: Stephan van Zwienen <s.vanzwienen@zwinq.com>
1 parent da91844 commit 006a3f4

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

encode.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ func ParseTimestamp(currentLocation *time.Location, str string) (time.Time, erro
422422

423423
if remainderIdx < len(str) && str[remainderIdx] == '.' {
424424
fracStart := remainderIdx + 1
425-
fracOff := strings.IndexAny(str[fracStart:], "-+ ")
425+
fracOff := strings.IndexAny(str[fracStart:], "-+Z ")
426426
if fracOff < 0 {
427427
fracOff = len(str) - fracStart
428428
}
@@ -432,7 +432,7 @@ func ParseTimestamp(currentLocation *time.Location, str string) (time.Time, erro
432432
remainderIdx += fracOff + 1
433433
}
434434
if tzStart := remainderIdx; tzStart < len(str) && (str[tzStart] == '-' || str[tzStart] == '+') {
435-
// time zone separator is always '-' or '+' (UTC is +00)
435+
// time zone separator is always '-' or '+' or 'Z' (UTC is +00)
436436
var tzSign int
437437
switch c := str[tzStart]; c {
438438
case '-':
@@ -454,7 +454,11 @@ func ParseTimestamp(currentLocation *time.Location, str string) (time.Time, erro
454454
remainderIdx += 3
455455
}
456456
tzOff = tzSign * ((tzHours * 60 * 60) + (tzMin * 60) + tzSec)
457+
} else if tzStart < len(str) && str[tzStart] == 'Z' {
458+
// time zone Z separator indicates UTC is +00
459+
remainderIdx += 1
457460
}
461+
458462
var isoYear int
459463

460464
if isBC {

encode_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,43 @@ func TestAppendEscapedTextExistingBuffer(t *testing.T) {
828828
}
829829
}
830830

831+
var formatAndParseTimestamp = []struct {
832+
time time.Time
833+
expected string
834+
}{
835+
{time.Time{}, "0001-01-01 00:00:00Z"},
836+
{time.Date(2001, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", 0)), "2001-02-03 04:05:06.123456789Z"},
837+
{time.Date(2001, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", 2*60*60)), "2001-02-03 04:05:06.123456789+02:00"},
838+
{time.Date(2001, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", -6*60*60)), "2001-02-03 04:05:06.123456789-06:00"},
839+
{time.Date(2001, time.February, 3, 4, 5, 6, 0, time.FixedZone("", -(7*60*60+30*60+9))), "2001-02-03 04:05:06-07:30:09"},
840+
841+
{time.Date(1, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", 0)), "0001-02-03 04:05:06.123456789Z"},
842+
{time.Date(1, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", 2*60*60)), "0001-02-03 04:05:06.123456789+02:00"},
843+
{time.Date(1, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", -6*60*60)), "0001-02-03 04:05:06.123456789-06:00"},
844+
845+
{time.Date(0, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", 0)), "0001-02-03 04:05:06.123456789Z BC"},
846+
{time.Date(0, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", 2*60*60)), "0001-02-03 04:05:06.123456789+02:00 BC"},
847+
{time.Date(0, time.February, 3, 4, 5, 6, 123456789, time.FixedZone("", -6*60*60)), "0001-02-03 04:05:06.123456789-06:00 BC"},
848+
849+
{time.Date(1, time.February, 3, 4, 5, 6, 0, time.FixedZone("", -(7*60*60+30*60+9))), "0001-02-03 04:05:06-07:30:09"},
850+
{time.Date(0, time.February, 3, 4, 5, 6, 0, time.FixedZone("", -(7*60*60+30*60+9))), "0001-02-03 04:05:06-07:30:09 BC"},
851+
}
852+
853+
func TestFormatAndParseTimestamp(t *testing.T) {
854+
for _, val := range formatAndParseTimestamp {
855+
formattedTime := FormatTimestamp(val.time)
856+
parsedTime, err := ParseTimestamp(nil, string(formattedTime))
857+
858+
if err != nil {
859+
t.Errorf("invalid parsing, err: %v", err.Error())
860+
}
861+
862+
if val.time.UTC() != parsedTime.UTC() {
863+
t.Errorf("invalid parsing from formatted timestamp, got %v; expected %v", parsedTime.String(), val.time.String())
864+
}
865+
}
866+
}
867+
831868
func BenchmarkAppendEscapedText(b *testing.B) {
832869
longString := ""
833870
for i := 0; i < 100; i++ {

0 commit comments

Comments
 (0)