提交 1267c641 编写于 作者: N nulltoken

Added EpochHelper class to ease unix time/DateTimeOffset conversion.

上级 b6ea4b1f
using System;
using NUnit.Framework;
namespace libgit2sharp.Tests
{
[TestFixture]
public class EpochHelperFixture
{
[TestCase(0)]
[TestCase(17)]
public void ToDateDateTimeOffset_ShouldReturnAUtcBasedDateTimeOffset(Int32 secondsSinceEpoch)
{
DateTimeOffset when = EpochHelper.ToDateTimeOffset(secondsSinceEpoch);
Assert.AreEqual(TimeSpan.Zero, when.Offset);
Assert.AreEqual(DateTimeKind.Utc, when.UtcDateTime.Kind);
}
[TestCase(1291801952, "Wed, 08 Dec 2010 09:52:32 GMT")]
[TestCase(1234567890, "Fri, 13 Feb 2009 23:31:30 GMT")]
public void ToDateDateTimeOffset_ShouldCorrectlyConvert(Int32 secondsSinceEpoch, string expected)
{
DateTimeOffset when = EpochHelper.ToDateTimeOffset(secondsSinceEpoch);
var expectedDate = DateTimeOffset.Parse(expected);
Assert.AreEqual(expectedDate, when);
}
}
}
......@@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="EpochHelperFixture.cs" />
<Compile Include="RepositoryFixtures.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
......
using System;
namespace libgit2sharp
{
public static class EpochHelper
{
public static readonly long EpochTicks = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).Ticks;
public static DateTimeOffset ToDateTimeOffset(Int32 secondsSinceEpoch)
{
long ticks = secondsSinceEpoch * TimeSpan.TicksPerSecond + EpochTicks;
return new DateTimeOffset(ticks, TimeSpan.Zero);
}
}
}
\ No newline at end of file
......@@ -42,6 +42,7 @@
<ItemGroup>
<Compile Include="Blob.cs" />
<Compile Include="Commit.cs" />
<Compile Include="EpochHelper.cs" />
<Compile Include="ObjectId.cs" />
<Compile Include="Person.cs" />
<Compile Include="RawObject.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册