From 4848f51c05b06db166c83b685f12b351cb7991ef Mon Sep 17 00:00:00 2001 From: LateApexEarlySpeed <72254037+lateapexearlyspeed@users.noreply.github.com> Date: Tue, 12 Jan 2021 01:36:26 +0800 Subject: [PATCH] issue-46477 Fix issue about checking last char in datetime string. (#46614) * issue-46477 Fix issue about checking last char in datetime string. * issue-46477 Fix comment: Add more tests with timezone part. --- .../src/System/Globalization/DateTimeParse.cs | 1 + .../tests/System/DateTimeTests.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs index 6a712d8623092..75c5b0bf52e80 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs @@ -3002,6 +3002,7 @@ private static bool ParseISO8601(ref DateTimeRawInfo raw, ref __DTString str, Da result.SetBadDateTimeFailure(); return false; } + str.Index--; } else if (ch == 'Z' || ch == 'z') { diff --git a/src/libraries/System.Runtime/tests/System/DateTimeTests.cs b/src/libraries/System.Runtime/tests/System/DateTimeTests.cs index 09aacebf83d09..9a222860a3fd4 100644 --- a/src/libraries/System.Runtime/tests/System/DateTimeTests.cs +++ b/src/libraries/System.Runtime/tests/System/DateTimeTests.cs @@ -1041,6 +1041,18 @@ public static void Parse_InvalidArguments_Throws() Assert.Throws(() => DateTime.Parse("")); Assert.Throws(() => DateTime.Parse("", new MyFormatter())); Assert.Throws(() => DateTime.Parse("", new MyFormatter(), DateTimeStyles.NoCurrentDateDefault)); + + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000-07:00c")); + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000-07:00c", new MyFormatter())); + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000-07:00c", new MyFormatter(), DateTimeStyles.NoCurrentDateDefault)); + + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000+00:00#")); + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000+00:00#", new MyFormatter())); + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000+00:00#", new MyFormatter(), DateTimeStyles.NoCurrentDateDefault)); + + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000+00:00#\0")); + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000+00:00#\0", new MyFormatter())); + Assert.Throws(() => DateTime.Parse("2020-5-7T09:37:00.0000000+00:00#\0", new MyFormatter(), DateTimeStyles.NoCurrentDateDefault)); } [Theory] @@ -1711,6 +1723,11 @@ public static IEnumerable Parse_ValidInput_Succeeds_MemberData() yield return new object[] { "2 2 2Z", CultureInfo.InvariantCulture, TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2002, 2, 2, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local) }; yield return new object[] { "#10/10/2095#\0", CultureInfo.InvariantCulture, new DateTime(2095, 10, 10, 0, 0, 0) }; + yield return new object[] { "2020-5-7T09:37:00.0000000+00:00\0", CultureInfo.InvariantCulture, TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2020, 5, 7, 9, 37, 0, DateTimeKind.Utc), TimeZoneInfo.Local) }; + yield return new object[] { "#2020-5-7T09:37:00.0000000+00:00#", CultureInfo.InvariantCulture, TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2020, 5, 7, 9, 37, 0, DateTimeKind.Utc), TimeZoneInfo.Local) }; + yield return new object[] { "#2020-5-7T09:37:00.0000000+00:00#\0", CultureInfo.InvariantCulture, TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2020, 5, 7, 9, 37, 0, DateTimeKind.Utc), TimeZoneInfo.Local) }; + yield return new object[] { "2020-5-7T09:37:00.0000000+00:00", CultureInfo.InvariantCulture, TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2020, 5, 7, 9, 37, 0, DateTimeKind.Utc), TimeZoneInfo.Local) }; + if (PlatformDetection.IsNotInvariantGlobalization) { DateTime today = DateTime.Today;