Skip to content

Commit

Permalink
Added the deserialize fix and test for the UTC DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
gjdonkers committed May 25, 2021
1 parent 9def445 commit 5653d3c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
19 changes: 19 additions & 0 deletions YamlDotNet.Test/Serialization/DeserializerTest.cs
Expand Up @@ -19,6 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Collections.Generic;
using FluentAssertions;
using Xunit;
Expand All @@ -34,6 +35,7 @@ public void Deserialize_YamlWithInterfaceTypeAndMapping_ReturnsModel()
{
var yaml = @"
name: Jack
momentOfBirth: 1983-04-21T20:21:03.0041599Z
cars:
- name: Mercedes
year: 2018
Expand All @@ -48,6 +50,13 @@ public void Deserialize_YamlWithInterfaceTypeAndMapping_ReturnsModel()

var person = sut.Deserialize<Person>(yaml);
person.Name.Should().Be("Jack");
person.MomentOfBirth.Kind.Should().Be(DateTimeKind.Utc);
person.MomentOfBirth.ToUniversalTime().Year.Should().Be(1983);
person.MomentOfBirth.ToUniversalTime().Month.Should().Be(4);
person.MomentOfBirth.ToUniversalTime().Day.Should().Be(21);
person.MomentOfBirth.ToUniversalTime().Hour.Should().Be(20);
person.MomentOfBirth.ToUniversalTime().Minute.Should().Be(21);
person.MomentOfBirth.ToUniversalTime().Second.Should().Be(3);
person.Cars.Should().HaveCount(2);
person.Cars[0].Name.Should().Be("Mercedes");
person.Cars[0].Spec.Should().BeNull();
Expand All @@ -60,6 +69,7 @@ public void Deserialize_YamlWithTwoInterfaceTypesAndMappings_ReturnsModel()
{
var yaml = @"
name: Jack
momentOfBirth: 1983-04-21T20:21:03.0041599Z
cars:
- name: Mercedes
year: 2018
Expand All @@ -81,6 +91,13 @@ public void Deserialize_YamlWithTwoInterfaceTypesAndMappings_ReturnsModel()

var person = sut.Deserialize<Person>(yaml);
person.Name.Should().Be("Jack");
person.MomentOfBirth.Kind.Should().Be(DateTimeKind.Utc);
person.MomentOfBirth.ToUniversalTime().Year.Should().Be(1983);
person.MomentOfBirth.ToUniversalTime().Month.Should().Be(4);
person.MomentOfBirth.ToUniversalTime().Day.Should().Be(21);
person.MomentOfBirth.ToUniversalTime().Hour.Should().Be(20);
person.MomentOfBirth.ToUniversalTime().Minute.Should().Be(21);
person.MomentOfBirth.ToUniversalTime().Second.Should().Be(3);
person.Cars.Should().HaveCount(2);
person.Cars[0].Name.Should().Be("Mercedes");
person.Cars[0].Spec.EngineType.Should().Be("V6");
Expand All @@ -94,6 +111,8 @@ public class Person
{
public string Name { get; private set; }

public DateTime MomentOfBirth { get; private set; }

public IList<ICar> Cars { get; private set; }
}

Expand Down
Expand Up @@ -91,7 +91,10 @@ bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func<IPars

case TypeCode.DateTime:
// TODO: This is probably incorrect. Use the correct regular expression.
value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture);
var style = scalar.Value.EndsWith("Z")
? DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal
: scalar.Value.Length > 27 ? DateTimeStyles.AssumeLocal : DateTimeStyles.None;
value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture, style);
break;

default:
Expand Down

0 comments on commit 5653d3c

Please sign in to comment.