Skip to content

Apply numeric UTC offsets when parsing timestamps - #4007

Open
0z5a wants to merge 1 commit into
aws:mainfrom
0z5a:fix-datetime-numeric-utc-offset
Open

0z5a wants to merge 1 commit into
aws:mainfrom
0z5a:fix-datetime-numeric-utc-offset

Conversation

@0z5a

@0z5a 0z5a commented Nov 22, 2026

Copy link
Copy Markdown

Issue #, if available: #4006

Description of changes:

DateTime::ConvertTimestampStringToTimePoint uses TimeGM only when the parser decided the string is UTC. A numeric offset such as +01:00, +0530 or -0500 goes to std::mktime instead. That reads the fields as the process's local time, and the offset itself is never applied. The result is wrong by the gap between the offset in the string and the local zone. In the ISO 8601 parser, -00:00 and +0000 also take that path, because the UTC check only accepts Z and +00:00.

This change adds DateParser::GetNumericUtcOffset(), which reads +hh, +hhmm or +hh:mm (and -) from the zone text the parsers already keep. When the zone is a numeric offset, the conversion uses TimeGM(&timeStruct) - offset. Anything else, such as a zone name or a partial offset like +01:0, still goes through the existing mktime path with the same error log. ISO 8601, ISO 8601 basic, RFC 822 and AutoDetect all get this through the shared function.

A changelog fragment is added with tools/scripts/new-change.

How I found it

I was reading the date parsers and noticed that GetParsedTimezone() is filled in but nothing reads it. I then checked the generated Smithy DatetimeOffsets protocol tests under generated/protocol-tests/tests/output/*/tests/DatetimeOffsetsTest.cpp. They expect 2019-12-16T22:48:18-01:00 and 2019-12-17T00:48:18+01:00 to be epoch 1576540098, which the current code returns only when the offset happens to match the local zone.

Where this can show up in practice

  • Response members parsed by the JSON and XML deserializers (DateTime(str, ISO_8601) / AutoDetect) when a service or endpoint sends a timestamp with a non-zero offset.
  • Credential Expiration from credential_process and the container credentials endpoint (AWSCredentialsProvider.cpp, GeneralHTTPCredentialsProvider.cpp). These come from helper programs, which may write a local offset. A wrong expiry makes the SDK refresh too early, or keep credentials past their real expiry and get ExpiredToken.

In both cases the result also changes with the machine's TZ, so the same input can behave differently on two hosts.

Validation

I added DateTimeTest.TestParsingWithNonZeroUtcOffset. It compares 10 offset forms against 2019-12-16T23:48:18Z: ISO 8601 with -01:00, +01:00, +05:30, -00:00, +0000 and .000-10:00; ISO 8601 basic with +0100; RFC 822 with +0100 and -0500; and AutoDetect with +01:00.

I could not build the full SDK here. Instead, I compiled DateTimeCommon.cpp and platform/linux-shared/Time.cpp on their own against the core headers, with a small driver that runs the same 14 strings (the 10 above plus UTC controls). I ran it once with the source from main and once with this change, under three zones:

TZ main (1d07b20) this change
UTC 8 of 14 wrong 0 wrong
Europe/Berlin 6 of 14 wrong 0 wrong
America/New_York 9 of 14 wrong 0 wrong

The controls (Z, +00:00, RFC 822 GMT, ISO 8601 basic Z) are correct in both builds. +01:0 is still parsed through the old fallback as before. The changed lines pass clang-format-diff.py with the repo's .clang-format.

Not run: the full SDK build, the gtest binary for aws-cpp-sdk-core-tests, the protocol test binaries, clang-tidy, and anything on Windows. The Windows TimeGM was not exercised.

Check all that applies:

  • Did a review by yourself.
  • Added proper tests to cover this PR. (If tests are not applicable, explain.)
  • Checked if this PR is a breaking (APIs have been changed) change. No public API changes. Strings with a numeric offset now parse to the instant they name instead of a local-time reading.
  • Checked if this PR will not introduce cross-platform inconsistent behavior. The new path uses Aws::Time::TimeGM, which the UTC path already uses on every platform. It removes a dependency on the local zone.
  • Checked if this PR would require a ReadMe/Wiki update. It does not.

Check which platforms you have built SDK on to verify the correctness of this PR.

  • Linux
  • Windows
  • Android
  • MacOS
  • IOS
  • Other Platforms

I did not build the SDK on any platform. The standalone check above ran on macOS.

AI disclosure: I used an AI coding agent to read the parsers, draft the patch and test, and run the standalone check. I (Dezhen Lu) reviewed the change and the results.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

ConvertTimestampStringToTimePoint only used TimeGM for timestamps the
parser recognised as UTC. A numeric offset such as +01:00 or -0500 fell
through to std::mktime, which reads the fields as local time and never
applies the offset, so the result depended on the process's zone.

Read the parsed offset for ISO 8601, ISO 8601 basic and RFC 822 and
subtract it from the TimeGM result. Anything that is not a numeric
offset still takes the existing mktime path.

See aws#4006.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant