Skip to content

Commit

Permalink
feat: add support for Duration type
Browse files Browse the repository at this point in the history
By default, BclTicks (100ns) representation is used. Provides also a Nanosecond precision as opt-in.

resolves: #19
  • Loading branch information
AndreaCuneo committed Sep 4, 2023
1 parent 88c2ded commit 3df4ebd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) ARK LTD. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for
// license information.
using MessagePack.NodaTime.Tests.Helpers;
using NodaTime;
using System;
using Xunit;

namespace MessagePack.NodaTime.Tests
{
[Collection("ResolverCollection")]
public class DurationMessagePackFormatterTest
{
[Fact]
public void DurationTest()
{
var d = Duration.FromDays(1);
Assert.Equal(TestTools.Convert(d), d);
}

[Fact]
public void DurationArrayTest()
{
var p = new Duration[]
{
Duration.FromDays(1),
Duration.FromNanoseconds(100),
};
Assert.Equal(TestTools.Convert(p), p);
}
}
}
46 changes: 46 additions & 0 deletions MessagePack.NodaTime/DurationMessagePackFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) ARK LTD. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for
// license information.
using MessagePack.Formatters;

using NodaTime;
using NodaTime.Text;

using System;

namespace MessagePack.NodaTime
{
public sealed class DurationAsNanosecondsMessagePackFormatter : IMessagePackFormatter<Duration>
{
public static readonly DurationAsNanosecondsMessagePackFormatter Instance = new DurationAsNanosecondsMessagePackFormatter();

public Duration Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
var s = reader.ReadInt64();

return Duration.FromNanoseconds(s);
}

public void Serialize(ref MessagePackWriter writer, Duration value, MessagePackSerializerOptions options)
{
writer.Write(value.ToInt64Nanoseconds());
}
}

public sealed class DurationAsBclTicksMessagePackFormatter : IMessagePackFormatter<Duration>
{
public static readonly DurationAsBclTicksMessagePackFormatter Instance = new DurationAsBclTicksMessagePackFormatter();

public Duration Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
var s = reader.ReadInt64();

return Duration.FromTicks(s);
}

public void Serialize(ref MessagePackWriter writer, Duration value, MessagePackSerializerOptions options)
{
writer.Write(value.BclCompatibleTicks);
}
}
}
2 changes: 2 additions & 0 deletions MessagePack.NodaTime/NodatimeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal static class NodatimeResolverGetFormatterHelper

{typeof(Offset), OffsetMessagePackFormatter.Instance},
{typeof(Period), PeriodAsIntArrayMessagePackFormatter.Instance},
{typeof(Duration), DurationAsBclTicksMessagePackFormatter.Instance },

{typeof(OffsetDateTime), OffsetDateTimeMessagePackFormatter.Instance},
{typeof(ZonedDateTime), ZonedDateTimeMessagePackFormatter.Instance},
Expand All @@ -55,6 +56,7 @@ internal static class NodatimeResolverGetFormatterHelper
{typeof(Offset?), new NullableFormatter<Offset>() },
{typeof(OffsetDateTime?), new NullableFormatter<OffsetDateTime>() },
{typeof(ZonedDateTime?), new NullableFormatter<ZonedDateTime>() },
{typeof(Duration?), new NullableFormatter<Duration>() },
};

internal static object? GetFormatter(Type t)
Expand Down

0 comments on commit 3df4ebd

Please sign in to comment.