-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Preserving millisecond precision in DateTime parser #3934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "bugfix", | ||
| "category": "aws-cpp-sdk-core", | ||
| "contributor": "averyk22", | ||
| "description": "Preserve millisecond precision when constructing DateTime from an ISO-8601 string." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -264,6 +264,20 @@ TEST_F(DateTimeTest, TestISO_8601ParsingNSPrecision) | |
| ASSERT_EQ(gmtDate, DateTime(utcOffsetVersion, DateFormat::ISO_8601)); | ||
| } | ||
|
|
||
| TEST_F(DateTimeTest, TestISO_8601ParsingPreservesMilliseconds) | ||
| { | ||
| DateTime gmtDate("2026-09-15T18:00:00.016Z", DateFormat::ISO_8601); | ||
| ASSERT_TRUE(gmtDate.WasParseSuccessful()); | ||
| ASSERT_EQ(16, gmtDate.Millis() % 1000); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| TEST_F(DateTimeTest, TestISO_8601MillisecondRoundTrip) | ||
| { | ||
| DateTime gmtDate("2026-09-15T18:00:00.016Z", DateFormat::ISO_8601); | ||
| ASSERT_TRUE(gmtDate.WasParseSuccessful()); | ||
| ASSERT_EQ("2026-09-15T18:00:00.016", gmtDate.ToGmtStringWithMs()); | ||
| } | ||
|
|
||
| TEST_F(DateTimeTest, TestISO_8601ParsingTooLowPrecision) | ||
| { | ||
| const char* gmtDateStr = "2002-10-02T08:05:09.12Z"; | ||
|
|
@@ -296,6 +310,20 @@ TEST_F(DateTimeTest, TestISO_8601BasicParsingMSPrecision) | |
| ASSERT_EQ(gmtDate, DateTime(utcOffsetVersion, DateFormat::ISO_8601_BASIC)); | ||
| } | ||
|
|
||
| TEST_F(DateTimeTest, TestISO_8601BasicParsingPreservesMilliseconds) | ||
| { | ||
| DateTime gmtDate("20021002T080509016Z", DateFormat::ISO_8601_BASIC); | ||
| ASSERT_TRUE(gmtDate.WasParseSuccessful()); | ||
| ASSERT_EQ(16, gmtDate.Millis() % 1000); | ||
| } | ||
|
|
||
| TEST_F(DateTimeTest, TestISO_8601BasicMillisecondRoundTrip) | ||
| { | ||
| DateTime gmtDate("20021002T080509016Z", DateFormat::ISO_8601_BASIC); | ||
| ASSERT_TRUE(gmtDate.WasParseSuccessful()); | ||
| ASSERT_EQ("2002-10-02T08:05:09.016", gmtDate.ToGmtStringWithMs()); | ||
| } | ||
|
|
||
| TEST_F(DateTimeTest, TestISO_8601Parsing_DOS_Stopped) | ||
| { | ||
| const char* gmtDateStr = "Weddkasdiweijbnawei8eriojngsdgasdgsdf1gasd8asdgfasdfgsdikweisdfksdnsdksdklasdfsdklasdfdfsdfsdfsdfsadfasdafsdfgjjfgghdfgsdfsfsdfsdfasdfsdfasdfsdfasdfsdf"; | ||
|
|
@@ -380,3 +408,21 @@ TEST_F(DateTimeTest, TestFormatAutoDetect) | |
| DateTime parsedBadDate(badDate, DateFormat::AutoDetect); | ||
| ASSERT_FALSE(parsedBadDate.WasParseSuccessful()); | ||
| } | ||
|
|
||
| TEST_F(DateTimeTest, TestAutoDetectPreservesMilliseconds) | ||
| { | ||
| const char* isoMsDate = "2026-09-15T18:00:00.016Z"; | ||
| DateTime parsed(isoMsDate, DateFormat::AutoDetect); | ||
| ASSERT_TRUE(parsed.WasParseSuccessful()); | ||
| ASSERT_EQ(16, parsed.Millis() % 1000); | ||
| ASSERT_EQ(DateTime(isoMsDate, DateFormat::ISO_8601), parsed); | ||
| } | ||
|
|
||
| TEST_F(DateTimeTest, TestAutoDetectBasicPreservesMilliseconds) | ||
| { | ||
| const char* basicMsDate = "20021002T080509016Z"; | ||
| DateTime parsed(basicMsDate, DateFormat::AutoDetect); | ||
| ASSERT_TRUE(parsed.WasParseSuccessful()); | ||
| ASSERT_EQ(16, parsed.Millis() % 1000); | ||
| ASSERT_EQ(DateTime(basicMsDate, DateFormat::ISO_8601_BASIC), parsed); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit/minor: Time is assigned by comparing to how long it has been since 1970, an edgecase of date before 1970 would render a negative number. if m_time is negative, % 1000 on a negative number stays negative.
Fix: floor when splitting into seconds + ms so the fraction is always 0–999
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if this is a valid case, can you create a string such that a negative value for a millisecond exists? the state machine should take care of that.