+ UDateTimeInt64:增加DateTimeInt64类,时间整形互转类
* 将相关日期起始修改为UTC1970/1/1
This commit is contained in:
parent
1b7babe869
commit
3b1a5b6649
BIN
Bin/SunnyUI.dll
BIN
Bin/SunnyUI.dll
Binary file not shown.
BIN
Bin/SunnyUI.pdb
BIN
Bin/SunnyUI.pdb
Binary file not shown.
Binary file not shown.
@ -93,8 +93,8 @@ namespace Sunny.UI
|
|||||||
get => item.Date;
|
get => item.Date;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (value < new DateTime(1753, 1, 1))
|
if (value < DateTimeEx.DateTimeBegin)
|
||||||
value = new DateTime(1753, 1, 1);
|
value = DateTimeEx.DateTimeBegin;
|
||||||
Text = value.ToString(dateFormat);
|
Text = value.ToString(dateFormat);
|
||||||
item.Date = value;
|
item.Date = value;
|
||||||
}
|
}
|
||||||
|
@ -113,8 +113,8 @@ namespace Sunny.UI
|
|||||||
get => item.Date;
|
get => item.Date;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (value < new DateTime(1753, 1, 1))
|
if (value < DateTimeEx.DateTimeBegin)
|
||||||
value = new DateTime(1753, 1, 1);
|
value = DateTimeEx.DateTimeBegin;
|
||||||
Text = value.ToString(dateFormat);
|
Text = value.ToString(dateFormat);
|
||||||
item.Date = value;
|
item.Date = value;
|
||||||
}
|
}
|
||||||
|
@ -253,18 +253,23 @@ namespace Sunny.UI
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 起始日期,2000-01-01 00:00:00起始
|
/// 起始日期,UTC时间,1970-01-01 00:00:00起始
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static DateTime DateTimeBegin = new DateTime(2000, 1, 1, 0, 0, 0);
|
public static DateTime DateTimeBegin = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 浮点转时间日期,以2000-01-01 00:00:00起始
|
/// 日期最大值
|
||||||
|
/// </summary>
|
||||||
|
public static DateTime DateTimeEnd = new DateTime(9999, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 浮点转时间日期,以UTC时间,1970-01-01 00:00:00起始
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="iDays">浮点</param>
|
/// <param name="iDays">浮点</param>
|
||||||
/// <returns>日期</returns>
|
/// <returns>日期</returns>
|
||||||
public static DateTime ToDateTime(this double iDays)
|
public static DateTime ToDateTime(this double iDays)
|
||||||
{
|
{
|
||||||
return DateTimeBegin.AddDays(iDays);
|
return TimeZone.CurrentTimeZone.ToLocalTime(DateTimeBegin.AddDays(iDays));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -429,8 +434,6 @@ namespace Sunny.UI
|
|||||||
return (time.TimeOfDay == timeToCompare.TimeOfDay);
|
return (time.TimeOfDay == timeToCompare.TimeOfDay);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly DateTime Date1970 = new DateTime(1970, 1, 1);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get seconds of UNIX area. This is the milliseconds since 1/1/1970
|
/// Get seconds of UNIX area. This is the milliseconds since 1/1/1970
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -441,7 +444,7 @@ namespace Sunny.UI
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
public static long SecondsSince1970(this DateTime datetime)
|
public static long SecondsSince1970(this DateTime datetime)
|
||||||
{
|
{
|
||||||
TimeSpan ts = datetime.Subtract(Date1970);
|
TimeSpan ts = datetime.ToUniversalTime().Subtract(DateTimeBegin);
|
||||||
return (long)ts.TotalSeconds;
|
return (long)ts.TotalSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -410,6 +410,7 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Units\UConcurrentDoubleKeyDictionary.cs" />
|
<Compile Include="Units\UConcurrentDoubleKeyDictionary.cs" />
|
||||||
<Compile Include="Units\UConcurrentGroupDictionary.cs" />
|
<Compile Include="Units\UConcurrentGroupDictionary.cs" />
|
||||||
|
<Compile Include="Units\UDateTimeInt64.cs" />
|
||||||
<Compile Include="Units\USnowflakeID.cs" />
|
<Compile Include="Units\USnowflakeID.cs" />
|
||||||
<Compile Include="Units\USuspendCtrlAltDel.cs" />
|
<Compile Include="Units\USuspendCtrlAltDel.cs" />
|
||||||
<Compile Include="Units\UThunder.cs" />
|
<Compile Include="Units\UThunder.cs" />
|
||||||
|
322
SunnyUI/Units/UDateTimeInt64.cs
Normal file
322
SunnyUI/Units/UDateTimeInt64.cs
Normal file
@ -0,0 +1,322 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Sunny.UI
|
||||||
|
{
|
||||||
|
public struct DateTimeInt64 : IComparable, IEquatable<DateTimeInt64>, IEquatable<long>, IEquatable<DateTime>
|
||||||
|
{
|
||||||
|
public long Value { get; set; }
|
||||||
|
|
||||||
|
public const long Jan1st1970Ticks = 621355968000000000; //Jan1st1970.Ticks;
|
||||||
|
public const long Dec31th9999Ticks = 3155378975999990000; //Dec31th9999.Ticks;
|
||||||
|
public const string DefaultFormatString = DateTimeEx.DateTimeFormatEx;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 返回当前时间的毫秒数, 这个毫秒其实就是自1970年1月1日0时起的毫秒数
|
||||||
|
/// </summary>
|
||||||
|
public static long CurrentDateTimeToTicks()
|
||||||
|
{
|
||||||
|
return (DateTime.UtcNow.Ticks - Jan1st1970Ticks) / 10000;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 返回指定时间的毫秒数, 这个毫秒其实就是自1970年1月1日0时起的毫秒数
|
||||||
|
/// </summary>
|
||||||
|
public static long DateTimeToTicks(DateTime datetime)
|
||||||
|
{
|
||||||
|
return (datetime.ToUniversalTime().Ticks - Jan1st1970Ticks) / 10000;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从一个代表自1970年1月1日0时起的毫秒数,转换为DateTime (北京时间)
|
||||||
|
/// </summary>
|
||||||
|
public static DateTime TicksToDateTime(long ticks)
|
||||||
|
{
|
||||||
|
return TimeZone.CurrentTimeZone.ToLocalTime(TicksToUTCDateTime(ticks));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从一个代表自1970年1月1日0时起的毫秒数,转换为DateTime (UTC时间)
|
||||||
|
/// </summary>
|
||||||
|
public static DateTime TicksToUTCDateTime(long ticks)
|
||||||
|
{
|
||||||
|
return new DateTime(ticks * 10000 + Jan1st1970Ticks);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTimeInt64(long ticks)
|
||||||
|
{
|
||||||
|
ticks = MakeValidDate(ticks);
|
||||||
|
Value = ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTimeInt64(DateTime dateTime)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(dateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTimeInt64(int year, int month, int day)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(new DateTime(year, month, day));
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTimeInt64(int year, int month, int day, int hour, int minute, int second)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(new DateTime(year, month, day, hour, minute, second));
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTimeInt64(int year, int month, int day, int hour, int minute, int second, int millisecond)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(new DateTime(year, month, day, hour, minute, second, millisecond));
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTimeInt64(DateTimeInt64 dtValue)
|
||||||
|
{
|
||||||
|
Value = dtValue.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsValidDate
|
||||||
|
{
|
||||||
|
get { return Value >= 0 && Value <= Dec31th9999Ticks - Jan1st1970Ticks; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTime DateTime
|
||||||
|
{
|
||||||
|
get { return TicksToDateTime(Value); }
|
||||||
|
set { Value = DateTimeToTicks(value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool CheckValidDate(long ticks)
|
||||||
|
{
|
||||||
|
return ticks >= 0 && ticks <= Dec31th9999Ticks - Jan1st1970Ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long MakeValidDate(long ticks)
|
||||||
|
{
|
||||||
|
if (ticks < 0)
|
||||||
|
ticks = 0;
|
||||||
|
if (ticks > Dec31th9999Ticks - Jan1st1970Ticks)
|
||||||
|
ticks = Dec31th9999Ticks - Jan1st1970Ticks;
|
||||||
|
|
||||||
|
return ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddMilliseconds(double milliseconds)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(DateTime.AddMilliseconds(milliseconds));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSeconds(double seconds)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(DateTime.AddSeconds(seconds));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddMinutes(double minutes)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(DateTime.AddMinutes(minutes));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddHours(double hours)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(DateTime.AddHours(hours));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddDays(double days)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(DateTime.AddDays(days));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddMonths(int months)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(DateTime.AddMonths(months));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddYears(int years)
|
||||||
|
{
|
||||||
|
Value = DateTimeToTicks(DateTime.AddYears(years));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long operator -(DateTimeInt64 dtValue1, DateTimeInt64 dtValue2)
|
||||||
|
{
|
||||||
|
return dtValue1.Value - dtValue2.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long operator +(DateTimeInt64 dtValue1, DateTimeInt64 dtValue2)
|
||||||
|
{
|
||||||
|
return dtValue1.Value + dtValue2.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DateTimeInt64 operator -(DateTimeInt64 dtValue1, long dtValue2)
|
||||||
|
{
|
||||||
|
dtValue1.Value -= dtValue2;
|
||||||
|
return dtValue1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DateTimeInt64 operator +(DateTimeInt64 dtValue1, long dtValue2)
|
||||||
|
{
|
||||||
|
dtValue1.Value += dtValue2;
|
||||||
|
return dtValue1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DateTimeInt64 operator ++(DateTimeInt64 dtValue)
|
||||||
|
{
|
||||||
|
dtValue.AddDays(1);
|
||||||
|
return dtValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DateTimeInt64 operator --(DateTimeInt64 dtValue)
|
||||||
|
{
|
||||||
|
dtValue.AddDays(-1);
|
||||||
|
return dtValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator long(DateTimeInt64 dtValue)
|
||||||
|
{
|
||||||
|
return dtValue.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator DateTimeInt64(long ticks)
|
||||||
|
{
|
||||||
|
return new DateTimeInt64(ticks);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator DateTimeInt64(DateTime dt)
|
||||||
|
{
|
||||||
|
return new DateTimeInt64(dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator DateTime(DateTimeInt64 dt)
|
||||||
|
{
|
||||||
|
return dt.DateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator ==(DateTimeInt64 dtValue1, DateTimeInt64 dtValue2)
|
||||||
|
{
|
||||||
|
return dtValue1.Value == dtValue2.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(DateTimeInt64 dtValue1, DateTimeInt64 dtValue2)
|
||||||
|
{
|
||||||
|
return dtValue1.Value != dtValue2.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator ==(DateTimeInt64 dtValue, long ticks)
|
||||||
|
{
|
||||||
|
return dtValue.Value == ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(DateTimeInt64 dtValue, long ticks)
|
||||||
|
{
|
||||||
|
return dtValue.Value != ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator <(DateTimeInt64 dtValue1, DateTimeInt64 dtValue2)
|
||||||
|
{
|
||||||
|
return dtValue1.Value < dtValue2.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator >(DateTimeInt64 dtValue1, DateTimeInt64 dtValue2)
|
||||||
|
{
|
||||||
|
return dtValue1.Value > dtValue2.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator <=(DateTimeInt64 dtValue1, DateTimeInt64 dtValue2)
|
||||||
|
{
|
||||||
|
return dtValue1.Value <= dtValue2.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator >=(DateTimeInt64 dtValue1, DateTimeInt64 dtValue2)
|
||||||
|
{
|
||||||
|
return dtValue1.Value >= dtValue2.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator <(DateTimeInt64 dtValue, long ticks)
|
||||||
|
{
|
||||||
|
return dtValue.Value < ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator >(DateTimeInt64 dtValue, long ticks)
|
||||||
|
{
|
||||||
|
return dtValue.Value > ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator <=(DateTimeInt64 dtValue, long ticks)
|
||||||
|
{
|
||||||
|
return dtValue.Value <= ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator >=(DateTimeInt64 dtValue, long ticks)
|
||||||
|
{
|
||||||
|
return dtValue.Value >= ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
{
|
||||||
|
if (obj is DateTimeInt64 dtValue)
|
||||||
|
{
|
||||||
|
return dtValue.Value == Value;
|
||||||
|
}
|
||||||
|
else if (obj is long longValue)
|
||||||
|
{
|
||||||
|
return longValue == Value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return Value.GetHashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
int IComparable.CompareTo(object obj)
|
||||||
|
{
|
||||||
|
if (!(obj is DateTimeInt64)) throw new ArgumentException();
|
||||||
|
return (Value).CompareTo(((DateTimeInt64)obj).Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ToString(long ticks)
|
||||||
|
{
|
||||||
|
return ToString(ticks, DefaultFormatString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return ToString(Value, DefaultFormatString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ToString(string fmtStr)
|
||||||
|
{
|
||||||
|
return ToString(Value, fmtStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToString(long dtValue, string fmtStr)
|
||||||
|
{
|
||||||
|
DateTime dt = TicksToDateTime(dtValue);
|
||||||
|
return dt.ToString(fmtStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(DateTimeInt64 dtValue)
|
||||||
|
{
|
||||||
|
return Value == dtValue.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(long ticks)
|
||||||
|
{
|
||||||
|
return Value == ticks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(DateTime datetime)
|
||||||
|
{
|
||||||
|
return Value == (new DateTimeInt64(datetime)).Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string DateTimeString => DateTime.DateTimeString();
|
||||||
|
|
||||||
|
public string DateString => DateTime.DateString();
|
||||||
|
|
||||||
|
public string TimeString => DateTime.TimeString();
|
||||||
|
|
||||||
|
public string HourMinuteString => DateTime.ToString("HH:mm");
|
||||||
|
}
|
||||||
|
}
|
@ -169,11 +169,10 @@ namespace Sunny.UI
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static SnowflakeIDDetail AnalyzeID(long Id)
|
public static SnowflakeIDDetail AnalyzeID(long Id)
|
||||||
{
|
{
|
||||||
DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
||||||
SnowflakeIDDetail result = new SnowflakeIDDetail();
|
SnowflakeIDDetail result = new SnowflakeIDDetail();
|
||||||
result.SnowflakeID = Id;
|
result.SnowflakeID = Id;
|
||||||
var timestamp = (Id >> timestampLeftShift);
|
var timestamp = (Id >> timestampLeftShift);
|
||||||
var time = Jan1st1970.AddMilliseconds(timestamp + twepoch);
|
var time = DateTimeEx.DateTimeBegin.AddMilliseconds(timestamp + twepoch);
|
||||||
result.DateTime = time.ToLocalTime();
|
result.DateTime = time.ToLocalTime();
|
||||||
result.DataCenterID = (Id ^ (timestamp << timestampLeftShift)) >> datacenterIdShift;
|
result.DataCenterID = (Id ^ (timestamp << timestampLeftShift)) >> datacenterIdShift;
|
||||||
result.WorkerID = (Id ^ ((timestamp << timestampLeftShift) | (result.DataCenterID << datacenterIdShift))) >> workerIdShift;
|
result.WorkerID = (Id ^ ((timestamp << timestampLeftShift) | (result.DataCenterID << datacenterIdShift))) >> workerIdShift;
|
||||||
@ -202,8 +201,7 @@ namespace Sunny.UI
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static long GetCurrentTimestamp()
|
private static long GetCurrentTimestamp()
|
||||||
{
|
{
|
||||||
DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
return (long)(DateTime.UtcNow - DateTimeEx.DateTimeBegin).TotalMilliseconds;
|
||||||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user