Skip to content

Commit 2da6713

Browse files
authored
Merge pull request #1039 from otan-cockroach/timetz_fix
encode: fix TimeTZ with second offsets
2 parents 99af95f + ad47bab commit 2da6713

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

encode.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,17 @@ func appendEscapedText(buf []byte, text string) []byte {
200200
func mustParse(f string, typ oid.Oid, s []byte) time.Time {
201201
str := string(s)
202202

203-
// check for a 30-minute-offset timezone
204-
if (typ == oid.T_timestamptz || typ == oid.T_timetz) &&
205-
str[len(str)-3] == ':' {
206-
f += ":00"
203+
// Check for a minute and second offset in the timezone.
204+
if typ == oid.T_timestamptz || typ == oid.T_timetz {
205+
for i := 3; i <= 6; i += 3 {
206+
if str[len(str)-i] == ':' {
207+
f += ":00"
208+
continue
209+
}
210+
break
211+
}
207212
}
213+
208214
// Special case for 24:00 time.
209215
// Unfortunately, golang does not parse 24:00 as a proper time.
210216
// In this case, we want to try "round to the next day", to differentiate.

encode_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ var timeTests = []struct {
5959
time.FixedZone("", -(7*60*60+42*60)))},
6060
{"2001-02-03 04:05:06-07:30:09", time.Date(2001, time.February, 3, 4, 5, 6, 0,
6161
time.FixedZone("", -(7*60*60+30*60+9)))},
62+
{"2001-02-03 04:05:06+07:30:09", time.Date(2001, time.February, 3, 4, 5, 6, 0,
63+
time.FixedZone("", +(7*60*60+30*60+9)))},
6264
{"2001-02-03 04:05:06+07", time.Date(2001, time.February, 3, 4, 5, 6, 0,
6365
time.FixedZone("", 7*60*60))},
6466
{"0011-02-03 04:05:06 BC", time.Date(-10, time.February, 3, 4, 5, 6, 0, time.FixedZone("", 0))},
@@ -251,6 +253,8 @@ func TestTimeWithTimezone(t *testing.T) {
251253
}{
252254
{"11:59:59+00:00", time.Date(0, 1, 1, 11, 59, 59, 0, time.UTC)},
253255
{"11:59:59+04:00", time.Date(0, 1, 1, 11, 59, 59, 0, time.FixedZone("+04", 4*60*60))},
256+
{"11:59:59+04:01:02", time.Date(0, 1, 1, 11, 59, 59, 0, time.FixedZone("+04:01:02", 4*60*60+1*60+2))},
257+
{"11:59:59-04:01:02", time.Date(0, 1, 1, 11, 59, 59, 0, time.FixedZone("-04:01:02", -(4*60*60+1*60+2)))},
254258
{"24:00+00", time.Date(0, 1, 2, 0, 0, 0, 0, time.UTC)},
255259
{"24:00Z", time.Date(0, 1, 2, 0, 0, 0, 0, time.UTC)},
256260
{"24:00-04:00", time.Date(0, 1, 2, 0, 0, 0, 0, time.FixedZone("-04", -4*60*60))},

0 commit comments

Comments
 (0)