Skip to content

Commit

Permalink
Fix date time serialization to use ISO8601 (#664)
Browse files Browse the repository at this point in the history
* Fix date time serialization to use ISO8601

* Update DateTime format usage to a consistent constant
  • Loading branch information
mtmk authored Nov 7, 2024
1 parent 04b2dc1 commit 7c00b41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
25 changes: 21 additions & 4 deletions src/NATS.Client.Core/INatsSerialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void Serialize(IBufferWriter<byte> bufferWriter, T value)
{
if (value is DateTime input)
{
if (Utf8Formatter.TryFormat(input, span, out var written))
if (Utf8Formatter.TryFormat(input, span, out var written, Formats.DateTimeIsoFormat))
{
bufferWriter.Advance(written);
}
Expand All @@ -133,7 +133,19 @@ public void Serialize(IBufferWriter<byte> bufferWriter, T value)
{
if (value is DateTimeOffset input)
{
if (Utf8Formatter.TryFormat(input, span, out var written))
bool result;
int written;
if (input.Offset == TimeSpan.Zero)
{
// This will make it place `Z` instead of `+00:00` at the end
result = Utf8Formatter.TryFormat(input.UtcDateTime, span, out written, Formats.DateTimeIsoFormat);
}
else
{
result = Utf8Formatter.TryFormat(input, span, out written, Formats.DateTimeIsoFormat);
}

if (result)
{
bufferWriter.Advance(written);
}
Expand Down Expand Up @@ -392,7 +404,7 @@ public void Serialize(IBufferWriter<byte> bufferWriter, T value)
if (buffer.Length == 0)
return default;

if (Utf8Parser.TryParse(span, out DateTime value, out _))
if (Utf8Parser.TryParse(span, out DateTime value, out _, 'O'))
{
return (T)(object)value;
}
Expand All @@ -405,7 +417,7 @@ public void Serialize(IBufferWriter<byte> bufferWriter, T value)
if (buffer.Length == 0)
return default;

if (Utf8Parser.TryParse(span, out DateTimeOffset value, out _))
if (Utf8Parser.TryParse(span, out DateTimeOffset value, out _, 'O'))
{
return (T)(object)value;
}
Expand Down Expand Up @@ -861,6 +873,11 @@ public INatsSerializer<T> Build()
}
}

internal static class Formats
{
public static readonly StandardFormat DateTimeIsoFormat = new(symbol: 'O');
}

internal sealed class NullBufferWriter : IBufferWriter<byte>
{
internal static readonly IBufferWriter<byte> Instance = new NullBufferWriter();
Expand Down
5 changes: 3 additions & 2 deletions tests/NATS.Client.Core2.Tests/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ public async Task NatsMemoryOwner_empty_payload_should_not_throw()
public void Utf8_serializer()
{
SerializeDeserialize<string>("foo", "foo");
SerializeDeserialize<DateTime>(DateTime.MinValue, "01/01/0001 00:00:00");
SerializeDeserialize<DateTimeOffset>(DateTimeOffset.MinValue, "01/01/0001 00:00:00 +00:00");
SerializeDeserialize<DateTime>(new DateTime(1970, 12, 31, 23, 42, 2, DateTimeKind.Utc), "1970-12-31T23:42:02.0000000Z");
SerializeDeserialize<DateTimeOffset>(new DateTimeOffset(1970, 12, 31, 23, 42, 2, TimeSpan.Zero), "1970-12-31T23:42:02.0000000Z");
SerializeDeserialize<DateTimeOffset>(new DateTimeOffset(1970, 12, 31, 23, 42, 2, TimeSpan.FromHours(1)), "1970-12-31T23:42:02.0000000+01:00");
SerializeDeserialize<Guid>(Guid.Empty, "00000000-0000-0000-0000-000000000000");
SerializeDeserialize<TimeSpan>(TimeSpan.Zero, "00:00:00");
SerializeDeserialize<bool>(true, "True");
Expand Down

0 comments on commit 7c00b41

Please sign in to comment.