Skip to content

Commit

Permalink
Fix fraction parsing in PartialDateTime with cultures (#224) (#263)
Browse files Browse the repository at this point in the history
* Fix fraction parsing in PartialDateTime with cultures (#224)
  • Loading branch information
villyg authored and jackliums committed Dec 19, 2018
1 parent e975da6 commit d423c91
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -33,6 +35,13 @@ public class PartialDateTimeTests

private PartialDateTimeBuilder _builder = new PartialDateTimeBuilder();

private CultureInfo _originalCulture;

public PartialDateTimeTests()
{
_originalCulture = Thread.CurrentThread.CurrentCulture;
}

public static IEnumerable<object[]> GetParameterPreviousParamNullData()
{
yield return new object[] { ParamNameDay, null, 15, 20, 30, 30, 0.500230m, 60, ParamNameMonth }; // Day cannot be specified if month is not specified.
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Health.Fhir.Core/Models/PartialDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d423c91

Please sign in to comment.