From d423c919cb3c466c1d172b4cef281c0a0e3e78cd Mon Sep 17 00:00:00 2001 From: Villy G Date: Wed, 19 Dec 2018 16:19:40 -0500 Subject: [PATCH] Fix fraction parsing in PartialDateTime with cultures (#224) (#263) * Fix fraction parsing in PartialDateTime with cultures (#224) --- .../Models/PartialDateTimeTests.cs | 30 ++++++++++++++++++- .../Models/PartialDateTime.cs | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Health.Fhir.Core.UnitTests/Models/PartialDateTimeTests.cs b/src/Microsoft.Health.Fhir.Core.UnitTests/Models/PartialDateTimeTests.cs index 51fefd2fc1..d61fbf553e 100644 --- a/src/Microsoft.Health.Fhir.Core.UnitTests/Models/PartialDateTimeTests.cs +++ b/src/Microsoft.Health.Fhir.Core.UnitTests/Models/PartialDateTimeTests.cs @@ -5,12 +5,14 @@ using System; using System.Collections.Generic; +using System.Globalization; +using System.Threading; using Microsoft.Health.Fhir.Core.Models; using Xunit; namespace Microsoft.Health.Fhir.Core.UnitTests.Models { - public class PartialDateTimeTests + public class PartialDateTimeTests : IDisposable { private const string ParamNameYear = "year"; private const string ParamNameMonth = "month"; @@ -33,6 +35,13 @@ public class PartialDateTimeTests private PartialDateTimeBuilder _builder = new PartialDateTimeBuilder(); + private CultureInfo _originalCulture; + + public PartialDateTimeTests() + { + _originalCulture = Thread.CurrentThread.CurrentCulture; + } + public static IEnumerable GetParameterPreviousParamNullData() { yield return new object[] { ParamNameDay, null, 15, 20, 30, 30, 0.500230m, 60, ParamNameMonth }; // Day cannot be specified if month is not specified. @@ -388,6 +397,7 @@ public void GivenAPartialDateTimeWithMissingComponents_WhenToDateTimeOffsetIsCal [InlineData("2018-01-25T03:01:58+05:30", "2018-01-25T03:01:58+05:30")] [InlineData("2018-01-09T00:23:25.0+10:00", "2018-01-09T00:23:25.0000000+10:00")] [InlineData("2018-01-09T00:23:25.1234567-01:00", "2018-01-09T00:23:25.1234567-01:00")] + [InlineData("2018-11-29T18:30:27.911+01:00", "2018-11-29T18:30:27.9110000+01:00")] public void GivenAValidPartialDateTime_WhenToStringIsCalled_ThenCorrectStringShouldBeReturned(string input, string expected) { PartialDateTime dateTime = PartialDateTime.Parse(input); @@ -396,6 +406,24 @@ public void GivenAValidPartialDateTime_WhenToStringIsCalled_ThenCorrectStringSho Assert.Equal(expected, dateTime.ToString()); } + [Theory] + [InlineData("de-DE", "2018-11-29T18:30:27.911+01:00", "2018-11-29T18:30:27,9110000+01:00")] + [InlineData("en-GB", "2018-11-29T18:30:27.911+01:00", "2018-11-29T18:30:27.9110000+01:00")] + [InlineData("en-US", "2018-11-29T18:30:27.911+01:00", "2018-11-29T18:30:27.9110000+01:00")] + public void GivenACulture_WhenToStringisCalled_ThenCorrectStringShouldBeReturned(string culture, string input, string expected) + { + System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture); + PartialDateTime dateTime = PartialDateTime.Parse(input); + + Assert.NotNull(dateTime); + Assert.Equal(expected, dateTime.ToString()); + } + + public void Dispose() + { + Thread.CurrentThread.CurrentCulture = _originalCulture; + } + private class PartialDateTimeBuilder { public PartialDateTimeBuilder() diff --git a/src/Microsoft.Health.Fhir.Core/Models/PartialDateTime.cs b/src/Microsoft.Health.Fhir.Core/Models/PartialDateTime.cs index bce5f31128..78733a71b6 100644 --- a/src/Microsoft.Health.Fhir.Core/Models/PartialDateTime.cs +++ b/src/Microsoft.Health.Fhir.Core/Models/PartialDateTime.cs @@ -260,7 +260,7 @@ public static PartialDateTime Parse(string s) if (!string.IsNullOrEmpty(fractionInString)) { - fraction = decimal.Parse(fractionInString); + fraction = decimal.Parse(fractionInString, CultureInfo.InvariantCulture); } TimeSpan? utcOffset = null;