Remove UnixTime.csproj, use DateTimeUtils instead

This commit is contained in:
ig 2023-09-14 14:20:30 +02:00
parent 8f876a37bd
commit 3ce40b936a
25 changed files with 176 additions and 284 deletions

View File

@ -134,7 +134,7 @@ public static class Program
var company = x.LastOrDefault()?.Split('(').FirstOrDefault()?.Trim() ?? ""; var company = x.LastOrDefault()?.Split('(').FirstOrDefault()?.Trim() ?? "";
var sn = x.LastOrDefault()?.Split('(').LastOrDefault()?.Trim("() ".ToCharArray()) ?? ""; var sn = x.LastOrDefault()?.Split('(').LastOrDefault()?.Trim("() ".ToCharArray()) ?? "";
var created = i.Created.ToUtcDateTime().ToString("dd.MM.yyyy HH:mm"); var created = i.Created.DateTimeFromUnixTime().ToString("dd.MM.yyyy HH:mm");
var line = new[] var line = new[]
{ {

View File

@ -18,7 +18,6 @@
<ProjectReference Include="../../Lib/Devices/Trumpf/SystemControl/SystemControl.csproj" /> <ProjectReference Include="../../Lib/Devices/Trumpf/SystemControl/SystemControl.csproj" />
<ProjectReference Include="../../Lib/Devices/Trumpf/TruConvertAc/TruConvertAc.csproj" /> <ProjectReference Include="../../Lib/Devices/Trumpf/TruConvertAc/TruConvertAc.csproj" />
<ProjectReference Include="../../Lib/Devices/Trumpf/TruConvertDc/TruConvertDc.csproj" /> <ProjectReference Include="../../Lib/Devices/Trumpf/TruConvertDc/TruConvertDc.csproj" />
<ProjectReference Include="../../Lib/Time/Time.csproj" />
<ProjectReference Include="../../Lib/Units/Units.csproj" /> <ProjectReference Include="../../Lib/Units/Units.csproj" />
<ProjectReference Include="../../Lib/Utils/Utils.csproj" /> <ProjectReference Include="../../Lib/Utils/Utils.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,4 @@
using InnovEnergy.Lib.Time.Unix; using InnovEnergy.Lib.Utils;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace InnovEnergy.App.SaliMax; namespace InnovEnergy.App.SaliMax;
@ -56,7 +56,7 @@ public class CustomLogger : ILogger
File.Delete(logFiles.First()); File.Delete(logFiles.First());
// Rename the current log file with a timestamp // Rename the current log file with a timestamp
var logFileBackupPath = Path.Combine(logFileDir, $"{logFileBaseName}_{UnixTime.Now}{logFileExt}"); var logFileBackupPath = Path.Combine(logFileDir, $"{logFileBaseName}_{DateTime.Now.ToUnixTime()}{logFileExt}");
File.Move(_LogFilePath, logFileBackupPath); File.Move(_LogFilePath, logFileBackupPath);
} }
} }

View File

@ -16,18 +16,18 @@ using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc.DataTypes;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc; using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc.Control; using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc.Control;
using InnovEnergy.Lib.Protocols.Modbus.Channels; using InnovEnergy.Lib.Protocols.Modbus.Channels;
using InnovEnergy.Lib.Time.Unix;
using InnovEnergy.Lib.Units; using InnovEnergy.Lib.Units;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using static InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes.SystemConfig; using static InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes.SystemConfig;
#pragma warning disable IL2026 #pragma warning disable IL2026
namespace InnovEnergy.App.SaliMax; namespace InnovEnergy.App.SaliMax;
internal static class Program internal static class Program
{ {
private static readonly UnixTimeSpan UpdateInterval = UnixTimeSpan.FromSeconds(2); private static readonly TimeSpan UpdateInterval = TimeSpan.FromSeconds(2);
private static readonly IReadOnlyList<Byte> BatteryNodes; private static readonly IReadOnlyList<Byte> BatteryNodes;
@ -148,9 +148,9 @@ internal static class Program
while (true) while (true)
{ {
await Observable await Observable
.Interval(UpdateInterval.ToTimeSpan()) .Interval(UpdateInterval)
.Select(_ => RunIteration()) .Select(_ => RunIteration())
.SelectMany(r => UploadCsv(r, UnixTime.Now.RoundTo(UpdateInterval))) .SelectMany(r => UploadCsv(r, DateTime.UtcNow))
.SelectError() .SelectError()
.ToTask(); .ToTask();
} }
@ -185,7 +185,7 @@ internal static class Program
record.ControlConstants(); record.ControlConstants();
record.ControlSystemState(); record.ControlSystemState();
$"{UnixTime.Now} : {record.StateMachine.State}: {record.StateMachine.Message}".WriteLine().LogInfo(); $"{record.StateMachine.State}: {record.StateMachine.Message}".LogInfo();
var essControl = record.ControlEss().WriteLine().LogInfo(); var essControl = record.ControlEss().WriteLine().LogInfo();
@ -313,20 +313,17 @@ internal static class Program
sc.ResetAlarmsAndWarnings = true; sc.ResetAlarmsAndWarnings = true;
} }
private static async Task<Boolean> UploadCsv(StatusRecord status, UnixTime timeStamp) private static async Task<Boolean> UploadCsv(StatusRecord status, DateTime timeStamp)
{ {
var s3Config = status.Config.S3; var s3Config = status.Config.S3;
var csv = status.ToCsv().LogInfo();
if (s3Config is null) if (s3Config is null)
return false; return false;
var csv = status.ToCsv();
var s3Path = timeStamp + ".csv"; var s3Path = timeStamp + ".csv";
var request = s3Config.CreatePutRequest(s3Path); var request = s3Config.CreatePutRequest(s3Path);
var response = await request.PutAsync(new StringContent(csv)); var response = await request.PutAsync(new StringContent(csv));
// This is temporary for Wittman
//await File.WriteAllTextAsync("/var/www/html/status.csv", csv.SplitLines().Where(l => !l.Contains("Secret")).JoinLines());
if (response.StatusCode != 200) if (response.StatusCode != 200)
{ {
Console.WriteLine("ERROR: PUT"); Console.WriteLine("ERROR: PUT");

View File

@ -2,7 +2,7 @@ namespace InnovEnergy.App.SaliMax.SystemConfig;
public class AcDcConfig public class AcDcConfig
{ {
public required Double MaxDcLinkVoltage { get; set; } public required Double MaxDcLinkVoltage { get; init; }
public required Double MinDcLinkVoltage { get; set; } public required Double MinDcLinkVoltage { get; init; }
public required Double ReferenceDcLinkVoltage { get; set; } public required Double ReferenceDcLinkVoltage { get; init; }
} }

View File

@ -1,5 +1,4 @@
using System.Text.Json; using System.Text.Json;
using InnovEnergy.Lib.Time.Unix;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using static System.Text.Json.JsonSerializer; using static System.Text.Json.JsonSerializer;
@ -27,7 +26,6 @@ public class Config //TODO: let IE choose from config files (Json) and connect t
public required Double MaxBatteryDischargingCurrent { get; set; } public required Double MaxBatteryDischargingCurrent { get; set; }
public required Double MaxDcPower { get; set; } public required Double MaxDcPower { get; set; }
public required Double MaxChargeBatteryVoltage { get; set; } public required Double MaxChargeBatteryVoltage { get; set; }
public required Double MinDischargeBatteryVoltage { get; set; } public required Double MinDischargeBatteryVoltage { get; set; }
@ -49,16 +47,16 @@ public class Config //TODO: let IE choose from config files (Json) and connect t
{ {
AcDc = new () AcDc = new ()
{ {
MinDcLinkVoltage = 690, MinDcLinkVoltage = 690,
ReferenceDcLinkVoltage = 750, ReferenceDcLinkVoltage = 750,
MaxDcLinkVoltage = 810, MaxDcLinkVoltage = 810,
}, },
DcDc = new () DcDc = new ()
{ {
UpperDcLinkVoltage = 50, UpperDcLinkVoltage = 50,
LowerDcLinkVoltage = 50, LowerDcLinkVoltage = 50,
ReferenceDcLinkVoltage = 750, ReferenceDcLinkVoltage = 750,
}, },
}, },
@ -66,25 +64,26 @@ public class Config //TODO: let IE choose from config files (Json) and connect t
{ {
AcDc = new () AcDc = new ()
{ {
MinDcLinkVoltage = 720, MinDcLinkVoltage = 720,
ReferenceDcLinkVoltage = 750, ReferenceDcLinkVoltage = 750,
MaxDcLinkVoltage = 810, MaxDcLinkVoltage = 810,
}, },
DcDc = new () DcDc = new ()
{ {
UpperDcLinkVoltage = 50, UpperDcLinkVoltage = 50,
LowerDcLinkVoltage = 50, LowerDcLinkVoltage = 50,
ReferenceDcLinkVoltage = 750, ReferenceDcLinkVoltage = 750,
}, },
}, },
MaxBatteryChargingCurrent = 210, MaxBatteryChargingCurrent = 210,
MaxBatteryDischargingCurrent = 210, MaxBatteryDischargingCurrent = 210,
MaxDcPower = 10000, MaxDcPower = 10000,
MaxChargeBatteryVoltage = 57,
MinDischargeBatteryVoltage = 0,
MaxChargeBatteryVoltage = 57,
MinDischargeBatteryVoltage = 0,
Devices = new () Devices = new ()
{ {
TruConvertAcIp = new() { Host = "localhost", Port = 5001}, TruConvertAcIp = new() { Host = "localhost", Port = 5001},
@ -111,16 +110,16 @@ public class Config //TODO: let IE choose from config files (Json) and connect t
{ {
AcDc = new () AcDc = new ()
{ {
MinDcLinkVoltage = 690, MinDcLinkVoltage = 690,
ReferenceDcLinkVoltage = 750, ReferenceDcLinkVoltage = 750,
MaxDcLinkVoltage = 810, MaxDcLinkVoltage = 810,
}, },
DcDc = new () DcDc = new ()
{ {
UpperDcLinkVoltage = 50, UpperDcLinkVoltage = 50,
LowerDcLinkVoltage = 50, LowerDcLinkVoltage = 50,
ReferenceDcLinkVoltage = 750, ReferenceDcLinkVoltage = 750,
}, },
}, },
@ -128,26 +127,26 @@ public class Config //TODO: let IE choose from config files (Json) and connect t
{ {
AcDc = new () AcDc = new ()
{ {
MinDcLinkVoltage = 720, MinDcLinkVoltage = 720,
ReferenceDcLinkVoltage = 750, ReferenceDcLinkVoltage = 750,
MaxDcLinkVoltage = 780, MaxDcLinkVoltage = 780,
}, },
DcDc = new () DcDc = new ()
{ {
UpperDcLinkVoltage = 20, UpperDcLinkVoltage = 20,
LowerDcLinkVoltage = 20, LowerDcLinkVoltage = 20,
ReferenceDcLinkVoltage = 750, ReferenceDcLinkVoltage = 750,
}, },
}, },
MaxBatteryChargingCurrent = 210, MaxBatteryChargingCurrent = 210,
MaxBatteryDischargingCurrent = 210, MaxBatteryDischargingCurrent = 210,
MaxDcPower = 10000, MaxDcPower = 10000,
MaxChargeBatteryVoltage = 57, MaxChargeBatteryVoltage = 57,
MinDischargeBatteryVoltage = 0, MinDischargeBatteryVoltage = 0,
S3 = new() S3 = new()
{ {
Bucket = "saliomameiringen", Bucket = "saliomameiringen",

View File

@ -2,7 +2,7 @@ namespace InnovEnergy.App.SaliMax.SystemConfig;
public class DcDcConfig public class DcDcConfig
{ {
public required Double LowerDcLinkVoltage { get; set; } public required Double LowerDcLinkVoltage { get; init; }
public required Double ReferenceDcLinkVoltage { get; set; } public required Double ReferenceDcLinkVoltage { get; init; }
public required Double UpperDcLinkVoltage { get; set; } public required Double UpperDcLinkVoltage { get; init; }
} }

View File

@ -2,6 +2,6 @@ namespace InnovEnergy.App.SaliMax.SystemConfig;
public class DevicesConfig public class DevicesConfig
{ {
public required AcDcConfig AcDc { get; set; } public required AcDcConfig AcDc { get; init; }
public required DcDcConfig DcDc { get; set; } public required DcDcConfig DcDc { get; init; }
} }

View File

@ -285,7 +285,7 @@ public static class Topology
return Switch.Open("K3"); return Switch.Open("K3");
} }
private static TextBlock CreateDcDcColumn(this StatusRecord status, ActivePower? l) private static TextBlock CreateDcDcColumn(this StatusRecord status, ActivePower? p)
{ {
var dc48Voltage = status.DcDc.Dc.Battery.Voltage.ToDisplayString(); var dc48Voltage = status.DcDc.Dc.Battery.Voltage.ToDisplayString();
@ -293,7 +293,7 @@ public static class Topology
.AlignLeft(dc48Voltage) .AlignLeft(dc48Voltage)
.TitleBox("DC/DC"); .TitleBox("DC/DC");
var busFlow = Flow.Horizontal(l); var busFlow = Flow.Horizontal(p);
return TextBlock.AlignCenterVertical(busBox, busFlow); return TextBlock.AlignCenterVertical(busBox, busFlow);
} }
@ -371,6 +371,7 @@ public static class Topology
return batteryAvgBox; // TODO: individualBatteries hidden atm return batteryAvgBox; // TODO: individualBatteries hidden atm
#pragma warning disable CS0162
var batteryBoxes = bat var batteryBoxes = bat
.Devices .Devices
.Select(CreateBatteryBox) .Select(CreateBatteryBox)
@ -386,6 +387,8 @@ public static class Topology
batteryAvgBox , batteryAvgBox ,
individualBatteries individualBatteries
); );
#pragma warning enable CS0162
} }
private static TextBlock CreateAveragedBatteryBox(Battery48TlRecords? bat) private static TextBlock CreateAveragedBatteryBox(Battery48TlRecords? bat)

View File

@ -9,4 +9,8 @@
<RootNamespace>InnovEnergy.Lib.Time</RootNamespace> <RootNamespace>InnovEnergy.Lib.Time</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Utils\Utils.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -1,7 +1,7 @@
namespace InnovEnergy.Lib.Time.Unix; // namespace InnovEnergy.Lib.Time.Unix;
//
public readonly partial struct UnixTime : IComparable<UnixTime>, IEquatable<UnixTime> // public readonly partial struct UnixTime : IComparable<UnixTime>, IEquatable<UnixTime>
{ // {
Int32 IComparable<UnixTime>.CompareTo(UnixTime other) => Ticks.CompareTo(other.Ticks); // Int32 IComparable<UnixTime>.CompareTo(UnixTime other) => Ticks.CompareTo(other.Ticks);
Boolean IEquatable<UnixTime>.Equals(UnixTime other) => Ticks == other.Ticks; // Boolean IEquatable<UnixTime>.Equals(UnixTime other) => Ticks == other.Ticks;
} // }

View File

@ -1,20 +1,19 @@
namespace InnovEnergy.Lib.Time.Unix; // namespace InnovEnergy.Lib.Time.Unix;
//
public readonly partial struct UnixTime // public readonly partial struct UnixTime
{ // {
private UnixTime(UInt32 ticks) => Ticks = ticks; // public static UnixTime FromTicks(Int32 ticks) => new UnixTime { Ticks = ticks };
//
public static UnixTime FromTicks(UInt32 ticks) => new UnixTime(ticks); // public static UnixTime FromUtcDateTime(DateTime dateTime)
public static UnixTime FromTicks(Int32 ticks) => new UnixTime((UInt32) ticks); // {
// var timeSinceEpoch = dateTime - DateTime.UnixEpoch;
public static UnixTime FromUtcDateTime(DateTime dateTime) // var ticks = (Int32) timeSinceEpoch.TotalSeconds;
{ //
var timeSinceEpoch = dateTime - DateTime.UnixEpoch; // return new UnixTime{ Ticks = ticks};
var ticks = (UInt32) timeSinceEpoch.TotalSeconds; // }
//
return new UnixTime(ticks); // public static UnixTime Epoch => new UnixTime { Ticks = 0 };
} // public static UnixTime Now => FromUtcDateTime(DateTime.UtcNow);
//
public static UnixTime Epoch => new UnixTime(0); //
public static UnixTime Now => FromUtcDateTime(DateTime.UtcNow); // }
}

View File

@ -1,9 +1,9 @@
namespace InnovEnergy.Lib.Time.Unix; // 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 DateTime(UnixTime unixTimeSpan) => unixTimeSpan.ToUtcDateTime();
public static implicit operator UnixTime(DateTime dateTime) => FromUtcDateTime(dateTime); // public static implicit operator UnixTime(DateTime dateTime) => FromUtcDateTime(dateTime);
} // }

View File

@ -1,20 +1,27 @@
namespace InnovEnergy.Lib.Time.Unix; // using InnovEnergy.Lib.Utils;
//
public readonly partial struct UnixTime // namespace InnovEnergy.Lib.Time.Unix;
{ //
public static UnixTime operator -(UnixTime a, UnixTimeSpan b) => FromTicks(a.Ticks - b.Ticks); // public readonly partial struct UnixTime
public static UnixTime operator +(UnixTime a, UnixTimeSpan b) => FromTicks(a.Ticks + b.Ticks); // {
public static UnixTime operator +(UnixTimeSpan a, UnixTime b) => FromTicks(a.Ticks + b.Ticks); // public static UnixTime operator -(UnixTime a, TimeSpan b) => FromTicks(a.Ticks - GetUnixTicks(b));
// public static UnixTime operator +(UnixTime a, TimeSpan b) => FromTicks(a.Ticks + GetUnixTicks(b));
public static UInt32 operator /(UnixTime a, UnixTimeSpan b) => a.Ticks / b.Ticks; // public static UnixTime operator +(TimeSpan a, UnixTime b) => FromTicks(GetUnixTicks(a) + b.Ticks);
public static UInt32 operator %(UnixTime a, UnixTimeSpan b) => a.Ticks % b.Ticks; //
// public static Double operator /(UnixTime a, TimeSpan b) => a.Ticks.ConvertTo<Double>() / b.Ticks;
public static Boolean operator < (UnixTime l, UnixTime r) => l.Ticks < r.Ticks; // public static Int32 operator %(UnixTime a, TimeSpan b) => a.Ticks % GetUnixTicks(b);
public static Boolean operator > (UnixTime l, UnixTime r) => l.Ticks > r.Ticks; //
public static Boolean operator <= (UnixTime l, UnixTime r) => l.Ticks <= r.Ticks; // public static Boolean operator < (UnixTime l, UnixTime r) => l.Ticks < r.Ticks;
public static Boolean operator >= (UnixTime l, UnixTime r) => l.Ticks >= r.Ticks; // public static Boolean operator > (UnixTime l, UnixTime r) => l.Ticks > r.Ticks;
public static Boolean operator == (UnixTime l, UnixTime r) => l.Ticks == r.Ticks; // public static Boolean operator <= (UnixTime l, UnixTime r) => l.Ticks <= r.Ticks;
public static Boolean operator != (UnixTime l, UnixTime r) => l.Ticks != r.Ticks; // public static Boolean operator >= (UnixTime l, UnixTime r) => l.Ticks >= r.Ticks;
// public static Boolean operator == (UnixTime l, UnixTime r) => l.Ticks == r.Ticks;
public static UnixTimeSpan operator -(UnixTime a, UnixTime b) => UnixTimeSpan.FromTicks(a.Ticks - b.Ticks); // public static Boolean operator != (UnixTime l, UnixTime r) => l.Ticks != r.Ticks;
} //
// public static TimeSpan operator -(UnixTime a, UnixTime b) => TimeSpan.FromSeconds(a.Ticks - b.Ticks);
//
// private static Int32 GetUnixTicks(TimeSpan ts)
// {
// return (Int32) (ts.Ticks / 10000000L);
// }
// }

View File

@ -1,8 +1,8 @@
namespace InnovEnergy.Lib.Time.Unix; // namespace InnovEnergy.Lib.Time.Unix;
//
public readonly partial struct UnixTime // public readonly partial struct UnixTime
{ // {
public override Boolean Equals(Object? obj) => obj is UnixTime other && Ticks == other.Ticks; // public override Boolean Equals(Object? obj) => obj is UnixTime other && Ticks == other.Ticks;
public override Int32 GetHashCode() => (Int32) (Ticks & 0x7F_FF_FF_FF); // public override Int32 GetHashCode() => (Int32) (Ticks & 0x7F_FF_FF_FF);
public override String ToString() => Ticks.ToString(); // public override String ToString() => Ticks.ToString();
} // }

View File

@ -1,14 +1,6 @@
// namespace InnovEnergy.Lib.Time.Unix;
//
// ReSharper disable ArrangeStaticMemberQualifier // public readonly partial struct UnixTime
// {
namespace InnovEnergy.Lib.Time.Unix; // public required Int32 Ticks { get; init; }
// }
public readonly partial struct UnixTime
{
// IMPORTANT: init is necessary for JSON deserializer
// ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global
public UInt32 Ticks { get; init; }
public UnixTime RoundTo(UnixTimeSpan span) => Epoch + this / span * span;
}

View File

@ -1,7 +0,0 @@
namespace InnovEnergy.Lib.Time.Unix;
public readonly partial struct UnixTimeSpan : IComparable<UnixTimeSpan>, IEquatable<UnixTimeSpan>
{
Int32 IComparable<UnixTimeSpan>.CompareTo(UnixTimeSpan other) => Ticks.CompareTo(other.Ticks);
Boolean IEquatable<UnixTimeSpan>.Equals(UnixTimeSpan other) => Ticks == other.Ticks;
}

View File

@ -1,26 +0,0 @@
namespace InnovEnergy.Lib.Time.Unix;
public readonly partial struct UnixTimeSpan
{
private UnixTimeSpan(UInt32 ticks) => Ticks = ticks;
public static UnixTimeSpan FromTicks(UInt32 ticks) => new UnixTimeSpan(ticks);
public static UnixTimeSpan FromTimeSpan(TimeSpan timeSpan)
{
var ticks = (UInt32) timeSpan.TotalSeconds;
return FromTicks(ticks);
}
public static UnixTimeSpan FromSeconds(UInt32 s) => FromTicks (s);
public static UnixTimeSpan FromMinutes(UInt32 m) => FromSeconds(m * 60);
public static UnixTimeSpan FromHours (UInt32 h) => FromMinutes(h * 60);
public static UnixTimeSpan FromDays (UInt32 d) => FromHours (d * 24);
public static UnixTimeSpan FromWeeks (UInt32 w) => FromDays (w * 7);
public static UnixTimeSpan FromSeconds(Int32 s) => FromSeconds((UInt32) s);
public static UnixTimeSpan FromMinutes(Int32 m) => FromMinutes((UInt32) m);
public static UnixTimeSpan FromHours (Int32 h) => FromHours ((UInt32) h);
public static UnixTimeSpan FromDays (Int32 d) => FromDays ((UInt32) d);
public static UnixTimeSpan FromWeeks (Int32 w) => FromWeeks ((UInt32) w);
}

View File

@ -1,9 +0,0 @@
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

@ -1,29 +0,0 @@
namespace InnovEnergy.Lib.Time.Unix;
public readonly partial struct UnixTimeSpan
{
public static Boolean operator < (UnixTimeSpan l, UnixTimeSpan r) => l.Ticks < r.Ticks;
public static Boolean operator > (UnixTimeSpan l, UnixTimeSpan r) => l.Ticks > r.Ticks;
public static Boolean operator <= (UnixTimeSpan l, UnixTimeSpan r) => l.Ticks <= r.Ticks;
public static Boolean operator >= (UnixTimeSpan l, UnixTimeSpan r) => l.Ticks >= r.Ticks;
public static Boolean operator == (UnixTimeSpan l, UnixTimeSpan r) => l.Ticks == r.Ticks;
public static Boolean operator != (UnixTimeSpan l, UnixTimeSpan r) => l.Ticks != r.Ticks;
public static UnixTimeSpan operator +(UnixTimeSpan a, UnixTimeSpan b) => new UnixTimeSpan(a.Ticks + b.Ticks);
public static UnixTimeSpan operator -(UnixTimeSpan a, UnixTimeSpan b) => new UnixTimeSpan(a.Ticks - b.Ticks);
public static UnixTimeSpan operator *(UnixTimeSpan a, UInt32 b) => new UnixTimeSpan(a.Ticks * b);
public static UnixTimeSpan operator *(UnixTimeSpan a, Int32 b) => new UnixTimeSpan(a.Ticks * (UInt32) b);
public static UnixTimeSpan operator *(UInt32 a, UnixTimeSpan b) => new UnixTimeSpan(a * b.Ticks);
public static UnixTimeSpan operator *(Int32 a, UnixTimeSpan b) => new UnixTimeSpan((UInt32) a * b.Ticks);
public static UnixTimeSpan operator /(UnixTimeSpan a, UInt32 b) => new UnixTimeSpan(a.Ticks / b);
public static UnixTimeSpan operator /(UnixTimeSpan a, Int32 b) => new UnixTimeSpan(a.Ticks / (UInt32)b);
public static UInt32 operator /(UnixTimeSpan a, UnixTimeSpan b) => a.Ticks / b.Ticks;
public static UnixTimeSpan operator %(UnixTimeSpan a, UInt32 b) => new UnixTimeSpan(a.Ticks % b);
public static UnixTimeSpan operator %(UnixTimeSpan a, Int32 b) => new UnixTimeSpan(a.Ticks % (UInt32)b);
public static UInt32 operator %(UnixTimeSpan a, UnixTimeSpan b) => a.Ticks % b.Ticks;
}

View File

@ -1,50 +0,0 @@
using System.Text;
namespace InnovEnergy.Lib.Time.Unix;
public readonly partial struct UnixTimeSpan
{
public override String ToString()
{
var sb = new StringBuilder();
var dt = ToTimeSpan();
var weeks = dt.Days / 7;
var days = dt.Days % 7;
if (weeks > 0)
{
sb.Append(weeks);
sb.Append("w ");
}
if (days > 0)
{
sb.Append(days);
sb.Append("d ");
}
if (dt.Hours > 0)
{
sb.Append(dt.Hours);
sb.Append("h ");
}
if (dt.Minutes > 0)
{
sb.Append(dt.Minutes);
sb.Append("m ");
}
if (dt.Seconds > 0)
{
sb.Append(dt.Seconds);
sb.Append("s");
}
return sb.ToString().Trim();
}
public override Boolean Equals(Object? obj) => obj is UnixTime other && Ticks == other.Ticks;
public override Int32 GetHashCode() => (Int32) (Ticks & 0x7F_FF_FF_FF);
public static UnixTimeSpan Forever { get; } = FromTicks(UInt32.MaxValue) / 1.Weeks() * 1.Weeks();
}

View File

@ -1,8 +0,0 @@
namespace InnovEnergy.Lib.Time.Unix;
public readonly partial struct UnixTimeSpan
{
public UInt32 Ticks { get; }
}

View File

@ -1,16 +0,0 @@
namespace InnovEnergy.Lib.Time.Unix;
public static class UnixTimeSpanExtensions
{
public static UnixTimeSpan Seconds(this Int32 s) => UnixTimeSpan.FromSeconds(s);
public static UnixTimeSpan Minutes(this Int32 m) => UnixTimeSpan.FromMinutes(m);
public static UnixTimeSpan Hours (this Int32 h) => UnixTimeSpan.FromHours (h);
public static UnixTimeSpan Days (this Int32 d) => UnixTimeSpan.FromDays (d);
public static UnixTimeSpan Weeks (this Int32 w) => UnixTimeSpan.FromWeeks (w);
public static UnixTimeSpan Seconds(this UInt32 s) => UnixTimeSpan.FromSeconds(s);
public static UnixTimeSpan Minutes(this UInt32 m) => UnixTimeSpan.FromMinutes(m);
public static UnixTimeSpan Hours (this UInt32 h) => UnixTimeSpan.FromHours (h);
public static UnixTimeSpan Days (this UInt32 d) => UnixTimeSpan.FromDays (d);
public static UnixTimeSpan Weeks (this UInt32 w) => UnixTimeSpan.FromWeeks (w);
}

View File

@ -8,7 +8,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="../Utils/Utils.csproj" /> <ProjectReference Include="../Utils/Utils.csproj" />
<ProjectReference Include="../Time/Time.csproj" />
</ItemGroup> </ItemGroup>
<!-- <Target Name="PreBuild" BeforeTargets="PreBuildEvent">--> <!-- <Target Name="PreBuild" BeforeTargets="PreBuildEvent">-->

View File

@ -0,0 +1,38 @@
namespace InnovEnergy.Lib.Utils;
public static class DateTimeUtils
{
public static DateTime Epoch { get; } = new DateTime
(
year: 1970,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0,
DateTimeKind.Utc
);
public static Int64 ToUnixTime(this DateTime dateTime)
{
var timeSinceEpoch = dateTime.ToUniversalTime() - Epoch;
return timeSinceEpoch.Ticks / TimeSpan.TicksPerSecond;
}
public static DateTime DateTimeFromUnixTime(this IConvertible unixTime)
{
var epochTicks = Epoch.Ticks + unixTime.ConvertTo<Double>() * TimeSpan.TicksPerSecond;
return new DateTime(epochTicks.ConvertTo<Int64>(), DateTimeKind.Utc);
}
public static DateTime Round(this DateTime date, TimeSpan interval)
{
var i = interval.Ticks;
var t = date.Ticks;
var intervalTicks = (t + i / 2 + 1) / i * i;
return new DateTime(intervalTicks, date.Kind);
}
}