Skip to content

Commit 49f3f8a

Browse files
author
Chung, Chun-Yi
committed
[Enhance] Support DateTime convert to byte[] through by Int64
1 parent 6e6b902 commit 49f3f8a

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

IsTo.Tests/To/ToOfGenericToArray.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,17 @@ public void ByDictionary()
400400
//Assert.True(result.SequenceEqual(expect));
401401
}
402402

403+
[Fact]
404+
public void FromDateTimeToByteArray()
405+
{
406+
var value = new DateTime(
407+
1971, 8, 31, 11, 22, 33, 44
408+
);
409+
var result = value.To<byte[]>();
410+
var expect = BitConverter.GetBytes(value.ToBinary());
411+
Assert.True(result.SequenceEqual(expect));
412+
}
413+
403414
[Fact]
404415
public void FromBooleanToByteArray()
405416
{

IsTo.Tests/To/ToOfGenericToDateTime.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// kevinjong 2016-02-11 - Creation
44

55
using System;
6+
using System.Linq;
67
using Xunit;
78

89
namespace IsTo.Tests
@@ -79,8 +80,17 @@ public void FromInt64()
7980
var value = expect.ToBinary(); //621880825530440000L;
8081
var result = value.To<DateTime>();
8182
Assert.True(result == expect);
83+
}
8284

83-
85+
[Fact]
86+
public void FromByteArray()
87+
{
88+
var value = new DateTime(
89+
1971, 8, 31, 11, 22, 33, 44
90+
);
91+
var expect = BitConverter.GetBytes(value.ToBinary());
92+
var result = value.To<byte[]>();
93+
Assert.True(result.SequenceEqual(expect));
8494
}
8595
}
8696
}

IsTo/To/TryFrom/TryFromDateTime.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ private static bool TryFromDateTime(
1919

2020
switch(to.Category) {
2121
case TypeCategory.Array:
22+
if(to.IsBytes) {
23+
result = value.ToBinary().To<byte[]>();
24+
return true;
25+
}
2226
return TryToArray(
2327
from,
2428
to,

0 commit comments

Comments
 (0)