Skip to content

Commit

Permalink
Fixed MassTransit#5022 - assume UTC when no offset is present.
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed Apr 3, 2024
1 parent 06f96c3 commit c5d5b61
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace MassTransit.Initializers.TypeConverters
{
using System;
using System.Globalization;
using Internals;


Expand Down Expand Up @@ -55,7 +56,7 @@ public bool TryConvert(object input, out DateTime result)

public bool TryConvert(string input, out DateTime result)
{
if (DateTimeOffset.TryParse(input, out var value))
if (DateTimeOffset.TryParse(input, null, DateTimeStyles.AssumeUniversal, out var value))
{
result = value.Offset == TimeSpan.Zero ? value.UtcDateTime : value.LocalDateTime;
return true;
Expand Down
53 changes: 53 additions & 0 deletions tests/MassTransit.Tests/Serialization/DateTimeConverter_Specs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace MassTransit.Tests.Serialization;

using System;
using MassTransit.Initializers.TypeConverters;
using NUnit.Framework;


[TestFixture]
public class DateTimeConverter_Specs
{
[Test]
public void Should_convert_date_time_min_value()
{
var converter = new DateTimeTypeConverter();

var value = DateTime.MinValue;

Assert.That(converter.TryConvert(value, out string text));

Assert.That(converter.TryConvert(text, out var result));

Assert.That(result, Is.EqualTo(value));
}

[Test]
public void Should_convert_date_time_offset_min_value()
{
var converter = new DateTimeOffsetTypeConverter();

var value = DateTimeOffset.MinValue;

Assert.That(converter.TryConvert(value, out string text));

Assert.That(converter.TryConvert(text, out var result));

Assert.That(result, Is.EqualTo(value));
}

[Test]
public void Should_convert_date_time_min_value_to_offset()
{
var converter = new DateTimeTypeConverter();
var offsetConverter = new DateTimeOffsetTypeConverter();

var value = DateTime.MinValue.ToUniversalTime();

Assert.That(converter.TryConvert(value, out string text));

Assert.That(offsetConverter.TryConvert(text, out var result));

Assert.That(result.UtcDateTime, Is.EqualTo(value));
}
}

0 comments on commit c5d5b61

Please sign in to comment.