初始化
This commit is contained in:
40
Common/SnowIdGenerate/TimeExtensions.cs
Normal file
40
Common/SnowIdGenerate/TimeExtensions.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace Snowflake
|
||||
{
|
||||
public static class System
|
||||
{
|
||||
public static Func<long> currentTimeFunc = InternalCurrentTimeMillis;
|
||||
|
||||
public static long CurrentTimeMillis()
|
||||
{
|
||||
return currentTimeFunc();
|
||||
}
|
||||
|
||||
public static IDisposable StubCurrentTime(Func<long> func)
|
||||
{
|
||||
currentTimeFunc = func;
|
||||
return new DisposableAction(() =>
|
||||
{
|
||||
currentTimeFunc = InternalCurrentTimeMillis;
|
||||
});
|
||||
}
|
||||
|
||||
public static IDisposable StubCurrentTime(long millis)
|
||||
{
|
||||
currentTimeFunc = () => millis;
|
||||
return new DisposableAction(() =>
|
||||
{
|
||||
currentTimeFunc = InternalCurrentTimeMillis;
|
||||
});
|
||||
}
|
||||
|
||||
private static readonly DateTime Jan1st1970 = new DateTime
|
||||
(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
private static long InternalCurrentTimeMillis()
|
||||
{
|
||||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user