-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By default, BclTicks (100ns) representation is used. Provides also a Nanosecond precision as opt-in. resolves: #19
- Loading branch information
1 parent
88c2ded
commit 3df4ebd
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
MessagePack.NodaTime.Tests/NodaTimeTests/DurationMessagePackFormatterTest.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,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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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