Skip to content

Commit c6dd179

Browse files
quentinmitcparkins
authored andcommitted
Fix handling of the %z strptime format. (open-telemetry#29929)
**Description:** `strptime(3)` says that `%z` is "an RFC-822/ISO 8601 standard timezone specification", but the previous code did not allow the string "Z" to signify UTC time, as required by ISO 8601. **Link to tracking Issue:** none **Testing:** Added unit tests for both numeric and "Z" time zones. **Documentation:** none
1 parent 17f3ca1 commit c6dd179

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.chloggen/time-zulu.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: time
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: The `%z` strptime format now correctly parses `Z` as a valid timezone
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues:
14+
- 29929
15+
16+
# (Optional) One or more lines of additional information to render under the primary note.
17+
# These lines will be padded with 2 spaces and then inserted directly into the document.
18+
# Use pipe (|) for multiline entries.
19+
subtext: |
20+
`strptime(3)` says that `%z` is "an RFC-822/ISO 8601 standard
21+
timezone specification", but the previous code did not allow the
22+
string "Z" to signify UTC time, as required by ISO 8601. Now, both
23+
`+0000` and `Z` are recognized as UTC times in all components that
24+
handle `strptime` format strings.
25+
26+
# If your change doesn't affect end users or the exported elements of any package,
27+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
28+
# Optional: The change log or logs in which this entry should be included.
29+
# e.g. '[user]' or '[user, api]'
30+
# Include 'user' if the change is relevant to end users.
31+
# Include 'api' if there is a change to a library API.
32+
# Default: '[user]'
33+
change_logs: [user]

internal/coreinternal/timeutils/internal/ctimefmt/ctimefmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var ctimeSubstitutes = map[string]string{
4444
"%f": "999999",
4545
"%s": "99999999",
4646
"%Z": "MST",
47-
"%z": "-0700",
47+
"%z": "Z0700",
4848
"%w": "-070000",
4949
"%i": "-07",
5050
"%j": "-07:00",

internal/coreinternal/timeutils/internal/ctimefmt/ctimefmt_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,25 @@ func TestParse(t *testing.T) {
5454
t.Errorf("Given: %v, expected: %v", dt, dt2)
5555
}
5656
}
57+
58+
func TestZulu(t *testing.T) {
59+
format := "%Y-%m-%dT%H:%M:%S.%L%z"
60+
// These time should all parse as UTC.
61+
for _, input := range []string{
62+
"2019-01-02T15:04:05.666666Z",
63+
"2019-01-02T15:04:05.666666-0000",
64+
"2019-01-02T15:04:05.666666+0000",
65+
} {
66+
t.Run(input, func(t *testing.T) {
67+
dt, err := Parse(format, input)
68+
if err != nil {
69+
t.Error(err)
70+
} else if dt.UnixNano() != dt1.UnixNano() {
71+
// We compare the unix nanoseconds because Go has a subtle parsing difference between "Z" and "+0000".
72+
// The former returns a Time with the UTC timezone, the latter returns a Time with a 0000 time zone offset.
73+
// (See Go's documentation for `time.Parse`.)
74+
t.Errorf("Given: %v, expected: %v", dt, dt1)
75+
}
76+
})
77+
}
78+
}

0 commit comments

Comments
 (0)