mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +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()
|
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>
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user