mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Add GetSystemStartTime, to pull the time the system started up
This commit is contained in:
committed by
Chris Cameron
parent
8fa1de991b
commit
ccb961fc2e
@@ -238,23 +238,25 @@ namespace ICD.Common.Utils
|
||||
public static TimeSpan GetSystemUptime()
|
||||
{
|
||||
if (s_SystemUptimeStartTimeUtc == null)
|
||||
{
|
||||
string uptime = GetSystemUptimeFeedback();
|
||||
Match match = Regex.Match(uptime, UPTIME_REGEX);
|
||||
if (!match.Success)
|
||||
return default(TimeSpan);
|
||||
UpdateSystemStartTime();
|
||||
|
||||
int days = int.Parse(match.Groups["days"].Value);
|
||||
int hours = int.Parse(match.Groups["hours"].Value);
|
||||
int minutes = int.Parse(match.Groups["minutes"].Value);
|
||||
int seconds = int.Parse(match.Groups["seconds"].Value);
|
||||
int milliseconds = int.Parse(match.Groups["milliseconds"].Value);
|
||||
if (s_SystemUptimeStartTimeUtc != null)
|
||||
return IcdEnvironment.GetUtcTime() - s_SystemUptimeStartTimeUtc.Value;
|
||||
return default(TimeSpan);
|
||||
}
|
||||
|
||||
TimeSpan span = new TimeSpan(days, hours, minutes, seconds, milliseconds);
|
||||
s_SystemUptimeStartTimeUtc = IcdEnvironment.GetUtcTime() - span;
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets the time the system was started
|
||||
/// DateTime that uptime starts
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static DateTime? GetSystemStartTime()
|
||||
{
|
||||
if (s_SystemUptimeStartTimeUtc == null)
|
||||
UpdateSystemStartTime();
|
||||
|
||||
return IcdEnvironment.GetUtcTime() - s_SystemUptimeStartTimeUtc.Value;
|
||||
return s_SystemUptimeStartTimeUtc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -286,6 +288,23 @@ namespace ICD.Common.Utils
|
||||
|
||||
#endregion
|
||||
|
||||
private static void UpdateSystemStartTime()
|
||||
{
|
||||
string uptime = GetSystemUptimeFeedback();
|
||||
Match match = Regex.Match(uptime, UPTIME_REGEX);
|
||||
if (!match.Success)
|
||||
return;
|
||||
|
||||
int days = int.Parse(match.Groups["days"].Value);
|
||||
int hours = int.Parse(match.Groups["hours"].Value);
|
||||
int minutes = int.Parse(match.Groups["minutes"].Value);
|
||||
int seconds = int.Parse(match.Groups["seconds"].Value);
|
||||
int milliseconds = int.Parse(match.Groups["milliseconds"].Value);
|
||||
|
||||
TimeSpan span = new TimeSpan(days, hours, minutes, seconds, milliseconds);
|
||||
s_SystemUptimeStartTimeUtc = IcdEnvironment.GetUtcTime() - span;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the result from the ramfree console command.
|
||||
/// </summary>
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace ICD.Common.Utils
|
||||
{
|
||||
public static partial class ProcessorUtils
|
||||
{
|
||||
private static DateTime? s_SystemStartTime;
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
@@ -145,6 +147,20 @@ namespace ICD.Common.Utils
|
||||
return TimeSpan.FromMilliseconds(Environment.TickCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the time the system was started
|
||||
/// DateTime that uptime starts
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static DateTime? GetSystemStartTime()
|
||||
{
|
||||
if (s_SystemStartTime == null)
|
||||
s_SystemStartTime = IcdEnvironment.GetUtcTime() - TimeSpan.FromMilliseconds(Environment.TickCount);
|
||||
|
||||
return s_SystemStartTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the uptime
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user