-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 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
24 changes: 24 additions & 0 deletions
24
cs-ilp-core-codecs-test/Test/Org/Interledger/Encoding/Asn/TestInterledgerCodecs.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,24 @@ | ||
using System; | ||
using System.IO; | ||
using Xunit; | ||
|
||
using Org.Interledger.Encoding.Asn.Codecs; | ||
|
||
namespace Test.Org.Interledger.Encoding.Asn | ||
{ | ||
public class TestInterledgerCodecs | ||
{ | ||
[Fact] | ||
public void TestTimestamp() | ||
{ | ||
DateTime dateTime = DateTime.Now; | ||
|
||
AsnTimestampCodec codec = new AsnTimestampCodec(); | ||
codec.Encode(dateTime); | ||
Console.WriteLine(string.Format("encoded: {0}", codec.CharString)); | ||
|
||
DateTime decodedDateTime = codec.Decode(); | ||
Console.WriteLine(string.Format("decoded: {0}", decodedDateTime)); | ||
} | ||
} | ||
} |
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" /> | ||
<PackageReference Include="xunit" Version="2.2.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Test\" /> | ||
<Folder Include="Test\Org\" /> | ||
<Folder Include="Test\Org\Interledger\" /> | ||
<Folder Include="Test\Org\Interledger\Encoding\" /> | ||
<Folder Include="Test\Org\Interledger\Encoding\Asn\" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\cs-ilp-codec-framework\cs-ilp-codec-framework.csproj" /> | ||
<ProjectReference Include="..\cs-ilp-core\cs-ilp-core.csproj" /> | ||
<ProjectReference Include="..\cs-ilp-core-codecs\cs-ilp-core-codecs.csproj" /> | ||
</ItemGroup> | ||
</Project> |
48 changes: 48 additions & 0 deletions
48
cs-ilp-core-codecs/Org/Interledger/Encoding/Asn/Codecs/AsnTimestampCodec.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,48 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Org.Interledger.Encoding.Asn.Codecs | ||
{ | ||
public class AsnTimestampCodec : AsnPrintableStringBasedObjectCodecBase<DateTime> | ||
{ | ||
private string DateTimeFormatter { get { return "yyyyMMddHHmmssfff"; } } | ||
|
||
public AsnTimestampCodec() : base(new AsnSizeConstraint(17)) | ||
{ | ||
Regex regex = new Regex(this.GetRegex()); | ||
this.Validator = (string charString) => regex.IsMatch(charString); | ||
} | ||
|
||
public override DateTime Decode() | ||
{ | ||
DateTime dateTime; | ||
DateTime.TryParseExact(this.CharString, this.DateTimeFormatter, CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.None, out dateTime); | ||
if (dateTime != null) | ||
{ | ||
return dateTime; | ||
} | ||
else | ||
{ | ||
throw new FormatException(string.Format( | ||
"Interledger timestamps only support values in the format 'YYYYMMDDHHMMSSfff', value {0} is invalid.", this.CharString | ||
)); | ||
} | ||
} | ||
|
||
public override void Encode(DateTime value) | ||
{ | ||
this.CharString = value.ToString(this.DateTimeFormatter); | ||
} | ||
|
||
public override void Accept(IAsnObjectCodecVisitor visitor) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private string GetRegex() | ||
{ | ||
return "[0-9]{17}"; | ||
} | ||
} | ||
} |
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