Skip to content

Commit

Permalink
Merge pull request #613 from gjdonkers/datetime-deserializer
Browse files Browse the repository at this point in the history
Added the deserialize fix and test for the UTC DateTime
  • Loading branch information
aaubry committed Jun 12, 2021
2 parents 9def445 + a980a34 commit 3bea68e
Show file tree
Hide file tree
Showing 2 changed files with 20 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,7 @@ 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);
value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
break;

default:
Expand Down

0 comments on commit 3bea68e

Please sign in to comment.