41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
|
using System;
|
|||
|
|
|||
|
namespace SqlSugar.DistributedSystem.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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|