-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format DateTime strings as correct ISO8601 (#296)
Fixes #295 Turns out that the default string formatting of DateTime objects doesn't conform to ISO8601, so even when the object is saved as UTC; the app does not recognize it as UTC.
- Loading branch information
Showing
5 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace CoffeeCard.Library.Utils | ||
{ | ||
public class DateTimeConverter : JsonConverter<DateTime> | ||
{ | ||
private const string DateFormatIso8601 = "yyyy-MM-ddTHH:mm:ss.fffZ"; | ||
|
||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return DateTime.Parse(reader.GetString()!, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToUniversalTime().ToString(DateFormatIso8601, CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
coffeecard/CoffeeCard.Tests.Unit/Utils/DateTimeConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Text.Json; | ||
using CoffeeCard.Library.Utils; | ||
using Xunit; | ||
|
||
namespace CoffeeCard.Tests.Unit.Utils | ||
{ | ||
public class DateTimeConverterTests | ||
{ | ||
private readonly JsonSerializerOptions _options; | ||
|
||
public DateTimeConverterTests() | ||
{ | ||
_options = new JsonSerializerOptions(); | ||
_options.Converters.Add(new DateTimeConverter()); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_ShouldFormatDateTimeCorrectly() | ||
{ | ||
// Arrange | ||
var dateTime = new DateTime(2022, 1, 9, 21, 3, 52, millisecond: 123, DateTimeKind.Utc); | ||
var expectedJson = "\"2022-01-09T21:03:52.123Z\""; | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(dateTime, _options); | ||
|
||
// Assert | ||
Assert.Equal(expectedJson, json); | ||
} | ||
|
||
[Fact] | ||
public void Deserialize_ShouldParseDateTimeCorrectly() | ||
{ | ||
// Arrange | ||
var json = "\"2022-01-09T21:03:52Z\""; | ||
var expectedDateTime = new DateTime(2022, 1, 9, 21, 3, 52, DateTimeKind.Utc); | ||
|
||
// Act | ||
var dateTime = JsonSerializer.Deserialize<DateTime>(json, _options); | ||
|
||
// Assert | ||
Assert.Equal(expectedDateTime, dateTime); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_Deserialize_RoundTrip_ShouldMatchOriginalDateTime() | ||
{ | ||
// Arrange | ||
var originalDateTime = new DateTime(2022, 1, 9, 21, 3, 52, DateTimeKind.Utc); | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(originalDateTime, _options); | ||
var deserializedDateTime = JsonSerializer.Deserialize<DateTime>(json, _options); | ||
|
||
// Assert | ||
Assert.Equal(originalDateTime, deserializedDateTime); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters