Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,41 @@ public class GetUptimeCommand : PSCmdlet
/// </summary>
protected override void ProcessRecord()
{
// Get-Uptime throw if IsHighResolution = false
// because stopwatch.GetTimestamp() return DateTime.UtcNow.Ticks
// instead of ticks from system startup.
// InternalTestHooks.StopwatchIsNotHighResolution is used as test hook.
if (Stopwatch.IsHighResolution && !InternalTestHooks.StopwatchIsNotHighResolution)
switch (ParameterSetName)
{
TimeSpan uptime = TimeSpan.FromSeconds(Stopwatch.GetTimestamp() / Stopwatch.Frequency);

switch (ParameterSetName)
{
case TimespanParameterSet:
// return TimeSpan of time since the system started up
WriteObject(uptime);
break;
case SinceParameterSet:
// return Datetime when the system started up
WriteObject(DateTime.Now.Subtract(uptime));
break;
}
}
else
{
WriteDebug("System.Diagnostics.Stopwatch.IsHighResolution returns 'False'.");
Exception exc = new NotSupportedException(GetUptimeStrings.GetUptimePlatformIsNotSupported);
ThrowTerminatingError(new ErrorRecord(exc, "GetUptimePlatformIsNotSupported", ErrorCategory.NotImplemented, null));
case TimespanParameterSet:
ProcessTimespanParameterSet();
break;
case SinceParameterSet:
ProcessSinceParameterSet();
break;
}
}

/// <summary>
/// Process the Timespan parameter set.
/// </summary>
/// <remarks>
/// Outputs the time of the last system boot as a <see cref="TimeSpan"/>.
/// </remarks>
private void ProcessTimespanParameterSet()
{
TimeSpan result = TimeSpan.FromMilliseconds(Environment.TickCount64);
WriteObject(result);
}

/// <summary>
/// Process the Since parameter set.
/// </summary>
/// <remarks>
/// Outputs the time elapsed since the last system boot as a <see cref="DateTime"/>.
/// </remarks>
private void ProcessSinceParameterSet()
{
DateTime result = DateTime.Now.Subtract(TimeSpan.FromMilliseconds(Environment.TickCount64));
WriteObject(result);
}

/// <summary>
/// Parameter set name for Timespan OutputType.
/// </summary>
Expand Down

This file was deleted.

2 changes: 0 additions & 2 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,8 +1644,6 @@ public static class InternalTestHooks

// It's useful to test that we don't depend on the ScriptBlock and AST objects and can use a re-parsed version.
internal static bool IgnoreScriptBlockCache;
// Simulate 'System.Diagnostics.Stopwatch.IsHighResolution is false' to test Get-Uptime throw
internal static bool StopwatchIsNotHighResolution;
internal static bool DisableGACLoading;
internal static bool SetConsoleWidthToZero;
internal static bool SetConsoleHeightToZero;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Get-Uptime" -Tags "CI" {
BeforeAll {
$IsHighResolution = [system.diagnostics.stopwatch]::IsHighResolution
# Skip Get-Uptime test if IsHighResolution = false
# because stopwatch.GetTimestamp() return DateTime.UtcNow.Ticks
# instead of ticks from system startup
if ( ! $IsHighResolution )
{
$origDefaults = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues['it:skip'] = $true
}
}
AfterAll {
if ( ! $IsHighResolution ){
$global:PSDefaultParameterValues = $origDefaults
}
}
It "Get-Uptime return timespan (default -Timespan)" {
$upt = Get-Uptime
$upt | Should -BeOfType Timespan
Expand All @@ -25,15 +9,4 @@ Describe "Get-Uptime" -Tags "CI" {
$upt = Get-Uptime -Since
$upt | Should -BeOfType DateTime
}
It "Get-Uptime throw if IsHighResolution == false" {
# Enable the test hook
[system.management.automation.internal.internaltesthooks]::SetTestHook('StopwatchIsNotHighResolution', $true)

try {
{ Get-Uptime } | Should -Throw -ErrorId "GetUptimePlatformIsNotSupported,Microsoft.PowerShell.Commands.GetUptimeCommand"
} finally {
# Disable the test hook
[system.management.automation.internal.internaltesthooks]::SetTestHook('StopwatchIsHighResolutionIsFalse', $false)
}
}
}