feat: Add GetSystemStartTime, to pull the time the system started up

This commit is contained in:
Drew Tingen
2020-11-19 10:05:07 -05:00
committed by Chris Cameron
parent 8fa1de991b
commit ccb961fc2e
2 changed files with 49 additions and 14 deletions

View File

@@ -238,23 +238,25 @@ namespace ICD.Common.Utils
public static TimeSpan GetSystemUptime() public static TimeSpan GetSystemUptime()
{ {
if (s_SystemUptimeStartTimeUtc == null) if (s_SystemUptimeStartTimeUtc == null)
{ UpdateSystemStartTime();
string uptime = GetSystemUptimeFeedback();
Match match = Regex.Match(uptime, UPTIME_REGEX);
if (!match.Success)
return default(TimeSpan);
int days = int.Parse(match.Groups["days"].Value); if (s_SystemUptimeStartTimeUtc != null)
int hours = int.Parse(match.Groups["hours"].Value); return IcdEnvironment.GetUtcTime() - s_SystemUptimeStartTimeUtc.Value;
int minutes = int.Parse(match.Groups["minutes"].Value); return default(TimeSpan);
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); /// <summary>
s_SystemUptimeStartTimeUtc = IcdEnvironment.GetUtcTime() - span; /// 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> /// <summary>
@@ -286,6 +288,23 @@ namespace ICD.Common.Utils
#endregion #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> /// <summary>
/// Gets the result from the ramfree console command. /// Gets the result from the ramfree console command.
/// </summary> /// </summary>

View File

@@ -6,6 +6,8 @@ namespace ICD.Common.Utils
{ {
public static partial class ProcessorUtils public static partial class ProcessorUtils
{ {
private static DateTime? s_SystemStartTime;
#region Properties #region Properties
/// <summary> /// <summary>
@@ -145,6 +147,20 @@ namespace ICD.Common.Utils
return TimeSpan.FromMilliseconds(Environment.TickCount); 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> /// <summary>
/// Gets the uptime /// Gets the uptime
/// </summary> /// </summary>