]> Cypherpunks repositories - gostls13.git/commitdiff
time: fix time zone parsing when format includes time zone seconds
authorDarin Krauss <darinkrauss@gmail.com>
Mon, 1 Jul 2024 17:25:28 +0000 (13:25 -0400)
committerGopher Robot <gobot@golang.org>
Tue, 2 Jul 2024 20:01:50 +0000 (20:01 +0000)
The current implementation fails to parse a time string with a "Z"
time zone using a time format that includes time zone seconds. This
fix correctly parses the "Z" time zone for any Z-base time format
that includes seconds (i.e. "Z070000" or "Z07:00:00").

Fixes #68263

Change-Id: Idf8fa06b5f96383f050c4ffbd2bc5804fd408650
Reviewed-on: https://go-review.googlesource.com/c/go/+/595897
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
src/time/format.go
src/time/format_test.go

index 6488ec8abaa13cc4a52506e459930ba70c0d2340..c9e68b3eb25417710aea1bc46d5dbd29ec9e3774 100644 (file)
@@ -1203,12 +1203,14 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
                        default:
                                err = errBad
                        }
-               case stdISO8601TZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ShortTZ, stdISO8601ColonSecondsTZ, stdNumTZ, stdNumShortTZ, stdNumColonTZ, stdNumSecondsTz, stdNumColonSecondsTZ:
-                       if (std == stdISO8601TZ || std == stdISO8601ShortTZ || std == stdISO8601ColonTZ) && len(value) >= 1 && value[0] == 'Z' {
+               case stdISO8601TZ, stdISO8601ShortTZ, stdISO8601ColonTZ, stdISO8601SecondsTZ, stdISO8601ColonSecondsTZ:
+                       if len(value) >= 1 && value[0] == 'Z' {
                                value = value[1:]
                                z = UTC
                                break
                        }
+                       fallthrough
+               case stdNumTZ, stdNumShortTZ, stdNumColonTZ, stdNumSecondsTz, stdNumColonSecondsTZ:
                        var sign, hour, min, seconds string
                        if std == stdISO8601ColonTZ || std == stdNumColonTZ {
                                if len(value) < 6 {
index 4b598f6bdf80e95b47d66a1777b2f607a048b659..2537c765968ee2b3532daa3cdbd540a6d0783d44 100644 (file)
@@ -336,6 +336,23 @@ var parseTests = []ParseTest{
        {"", "2006-002 15:04:05", "2010-035 21:00:57", false, false, 1, 0},
        {"", "200600201 15:04:05", "201003502 21:00:57", false, false, 1, 0},
        {"", "200600204 15:04:05", "201003504 21:00:57", false, false, 1, 0},
+
+       // Time zone offsets
+       {"", "2006-01-02T15:04:05Z07", "2010-02-04T21:00:57Z", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z07", "2010-02-04T21:00:57+08", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z07", "2010-02-04T21:00:57-08", true, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z0700", "2010-02-04T21:00:57Z", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z0700", "2010-02-04T21:00:57+0800", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z0700", "2010-02-04T21:00:57-0800", true, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z07:00", "2010-02-04T21:00:57Z", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z07:00", "2010-02-04T21:00:57+08:00", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z07:00", "2010-02-04T21:00:57-08:00", true, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z070000", "2010-02-04T21:00:57Z", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z070000", "2010-02-04T21:00:57+080000", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z070000", "2010-02-04T21:00:57-080000", true, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z07:00:00", "2010-02-04T21:00:57Z", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z07:00:00", "2010-02-04T21:00:57+08:00:00", false, false, 1, 0},
+       {"", "2006-01-02T15:04:05Z07:00:00", "2010-02-04T21:00:57-08:00:00", true, false, 1, 0},
 }
 
 func TestParse(t *testing.T) {