System.Diagnostics.Process.BootTimeToDateTime C# (CSharp) Method

BootTimeToDateTime() static private method

Computes a time based on a number of ticks since boot.
static private BootTimeToDateTime ( System.TimeSpan timespanAfterBoot ) : System.DateTime
timespanAfterBoot System.TimeSpan The timespan since boot.
return System.DateTime
        internal static DateTime BootTimeToDateTime(TimeSpan timespanAfterBoot)
        {
            // Use the uptime and the current time to determine the absolute boot time. This implementation is relying on the 
            // implementation detail that Stopwatch.GetTimestamp() uses a value based on time since boot.
            DateTime bootTime = DateTime.UtcNow - TimeSpan.FromSeconds(Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency);

            // And use that to determine the absolute time for timespan.
            DateTime dt = bootTime + timespanAfterBoot;

            // The return value is expected to be in the local time zone.
            // It is converted here (rather than starting with DateTime.Now) to avoid DST issues.
            return dt.ToLocalTime();
        }

Usage Example

Esempio n. 1
0
 private DateTime GetStartTime() => Process.BootTimeToDateTime(Process.TicksToTimeSpan(GetStat().starttime));