introduce implicit converters for UnixTime & UnixTimeSpan

This commit is contained in:
ig 2023-03-20 13:25:19 +01:00
parent c3b1484e99
commit 17c2a959e6
4 changed files with 14 additions and 5 deletions

View File

@ -3,4 +3,7 @@ namespace InnovEnergy.Lib.Time.Unix;
public readonly partial struct UnixTime public readonly partial struct UnixTime
{ {
public DateTime ToUtcDateTime() => DateTime.UnixEpoch + TimeSpan.FromSeconds(Ticks); public DateTime ToUtcDateTime() => DateTime.UnixEpoch + TimeSpan.FromSeconds(Ticks);
public static implicit operator DateTime(UnixTime unixTimeSpan) => unixTimeSpan.ToUtcDateTime();
public static implicit operator UnixTime(DateTime dateTime) => FromUtcDateTime(dateTime);
} }

View File

@ -0,0 +1,9 @@
namespace InnovEnergy.Lib.Time.Unix;
public readonly partial struct UnixTimeSpan
{
public TimeSpan ToTimeSpan() => TimeSpan.FromSeconds(Ticks);
public static implicit operator TimeSpan(UnixTimeSpan unixTimeSpan) => unixTimeSpan.ToTimeSpan();
public static implicit operator UnixTimeSpan(TimeSpan timeSpan) => FromTimeSpan(timeSpan);
}

View File

@ -4,8 +4,5 @@ public readonly partial struct UnixTimeSpan
{ {
public UInt32 Ticks { get; } public UInt32 Ticks { get; }
public TimeSpan ToTimeSpan()
{
return TimeSpan.FromSeconds(Ticks);
}
} }

View File

@ -1,6 +1,6 @@
namespace InnovEnergy.Lib.Time.Unix; namespace InnovEnergy.Lib.Time.Unix;
public static class UnixTimeDeltaExtensions public static class UnixTimeSpanExtensions
{ {
public static UnixTimeSpan Seconds(this Int32 s) => UnixTimeSpan.FromSeconds(s); public static UnixTimeSpan Seconds(this Int32 s) => UnixTimeSpan.FromSeconds(s);
public static UnixTimeSpan Minutes(this Int32 m) => UnixTimeSpan.FromMinutes(m); public static UnixTimeSpan Minutes(this Int32 m) => UnixTimeSpan.FromMinutes(m);