Skip to content

Get-DbaUptime - Let the instance measure its uptime - #10852

Open
andreasjordan wants to merge 4 commits into
developmentfrom
get-dbauptime-instance-clock
Open

andreasjordan wants to merge 4 commits into
developmentfrom
get-dbauptime-instance-clock

Conversation

@andreasjordan

@andreasjordan andreasjordan commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #10833 (found by the sweep for siblings of #10847 and #10848)

Problem

SqlUptime was New-TimeSpan -Start $SQLStartTime.ToUniversalTime() -End $nowutc. $SQLStartTime is the creation date of tempdb, which the instance records in its own local time. ToUniversalTime() converted it with the time zone of the computer that runs the command, and $nowutc came from that computer's clock. When the computer and the instance are in different time zones, SqlUptime and SinceSqlStart are off by the whole difference, for example five hours for the setup of #10818.

Change

  • The uptime is ms_ticks - sqlserver_start_time_ms_ticks from sys.dm_os_sys_info, in milliseconds since the start of the instance. No clock can shift it, and neither can a daylight saving change since the start. That matters because the simpler GETDATE() minus the creation date would be one hour off after such a change, and the old code got that case right when both sides are in the same time zone.
  • sys.dm_os_sys_info needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE from SQL Server 2022 on). For a login without it, the uptime is counted from the creation of tempdb up to the clock of the instance, and a verbose message says which way was taken:
    • SQL Server 2022 and later, Azure SQL Database and Managed Instance: CONVERT(datetime2(3), <SqlStartTime>) AT TIME ZONE CURRENT_TIMEZONE_ID() up to SYSDATETIMEOFFSET(). AT TIME ZONE applies the offset that the time zone of the instance had at the start, so a daylight saving change since then counts. A start in the hour that repeats when the clocks go back has no offset that tells its two occurrences apart, and AT TIME ZONE takes the first. The query counts it as the second, in standard time, as ConvertTimeToUtc of .NET and the old code do: the second occurrence lies the change of the offset later, which the offset one day later gives, and it counts only when it has the same local time.
    • Before SQL Server 2022 the instance cannot name its time zone. The start is converted with the time zone of this computer, as before, which is right when the instance is in the same time zone, and the end is SYSUTCDATETIME() of the instance.
  • Test-DbaFeatureSupport gets the rule CurrentTimeZoneId for that choice. Its other callers ask for Query Store features only. An engine without a rule there, where the helper throws, takes the second way.
  • Help: description and SqlUptime say where the value comes from, and SqlUptime names the limit of the fallback: an instance that started in the first occurrence of the repeated hour ran one change of the clocks longer than reported. Both ways of the fallback write a verbose message when the start repeats.

Not changed: SqlStartTime, which is still the creation date of tempdb, and the Windows part. LastBootTime from CIM is converted to the local time of this computer, so comparing it with this computer's clock is correct.

Tests

New context "When the clock of this computer is ahead of the instance" in Get-DbaUptime.Tests.ps1. It moves only the clock of this computer five hours ahead with Mock Get-Date -ModuleName dbatools, the same exception the clock test of #10820 uses, and creates a SQL login without any permissions:

  • SqlUptime matches the uptime that sys.dm_os_sys_info reports, within two minutes
  • the new login cannot read sys.dm_os_sys_info (asserted, so that the next test really takes the fallback)
  • with that login, SqlUptime matches the uptime that sys.dm_os_sys_info reports, within two minutes

New context "When daylight saving time changed since the instance started": the server objects of a login without VIEW SERVER STATE report a creation of tempdb at the last noon of the past year at which the time zone had another offset than today. Everything else, the queries included, runs on the real instance.

  • with the time zone of the instance (skipped before SQL Server 2022), SqlUptime matches the start converted with the rules of that time zone in .NET, within two minutes
  • with a server object that claims SQL Server 2019, so that this way runs on every version, SqlUptime matches the start converted with the time zone of this computer
  • both check in the dbatools log which way was taken, because in the lab both time zones are the same and give the same uptime

New context "When the instance started in the hour that repeats when the clocks go back": the server objects of such a login report a creation of tempdb in the last quarter of an hour of the past year that repeated (quarters, so that a change of half an hour is found too). The same two ways, each compared with ConvertTimeToUtc, which counts such a time as standard time, and each checks the verbose message about the repeated hour.

Test-DbaFeatureSupport.Tests.ps1: rule cases for CurrentTimeZoneId, its first version in the independent list, the Azure SQL Database case, and a test that asks InstanceSingle itself whether it knows CURRENT_TIMEZONE_ID().

Lab (SQL03, W. Europe Standard Time, SQL Server 2025 as InstanceMulti1, SQL Server 2019 as InstanceSingle): Get-DbaUptime.Tests.ps1 13 passed, Test-DbaFeatureSupport.Tests.ps1 42 passed and dbatools.Tests.ps1 87 passed and 6 skipped, each on Windows PowerShell 5.1 and on PowerShell 7.6. Also green on SQL04 (SQL Server 2022 as InstanceMulti1, 2025 as InstanceSingle): Get-DbaUptime.Tests.ps1 13 passed on 5.1 and 7.6. With the Get-DbaUptime.ps1 of development, the two uptime tests of the first context fail, 17,983 seconds off, which is five hours; in the second context only the check of the way fails, because the conversion of development is right when both time zones are the same. With the Get-DbaUptime.ps1 of cf0ab8b83, the two tests of the second context fail, 3,601 seconds off, the hour of the change on 2026-10-25. With the Get-DbaUptime.ps1 of 07310bc7b, the test of the third context for the time zone of the instance fails, 3,599 seconds off for a start at 2026-10-25 02:45, and the one before SQL Server 2022 fails only at the new verbose message, because that way already counted the second occurrence. In a time zone without daylight saving time, such as UTC on the runners, there is no change to count, and these tests check the uptime only. (Test-DbaFeatureSupport is unchanged since 07310bc7b, so its numbers are from that round.)

After review

Daylight saving time for a login without VIEW SERVER STATE (07310bc7b). The review of cf0ab8b83 found that the fallback subtracted two local times of the instance, the creation of tempdb and GETDATE(), so a daylight saving change since the start was lost: 24 hours instead of 25 for an instance in Eastern time that started on 2026-10-31 12:00, asked on 2026-11-01 12:00. That broke the case the old code got right when both sides share a time zone. The fallback now converts the start with the time zone of the instance on SQL Server 2022 and later, and with the time zone of this computer before, as described under Change. The old fallback test repeated the faulty subtraction for its expected value; it now compares with sys.dm_os_sys_info, and the new context covers the change itself.

A start in the repeated hour (0507999ac). The review of 07310bc7b found that AT TIME ZONE gives a local time in the hour that repeats when the clocks go back the offset before the change. An instance in Eastern time that started in the second 2026-11-01 01:30 (-05:00) reported three hours at 03:30 instead of two, while the conversion of the old code counts such a time as standard time. A probe on SQL Server 2022 and 2025 confirmed it for W. Europe, Eastern and Lord Howe (a change of half an hour). The query now takes the second occurrence when the local time repeats, as described under Change; in the probe that gave the instant of ConvertTimeToUtc for all 64 combinations of 16 local times around both changes of 2026 and the four time zones W. Europe, Eastern, Lord Howe and UTC. An instance that really started in the first occurrence ran one change of the clocks longer than reported; the help of SqlUptime says so, and a verbose message names the case. The new context covers the second occurrence for both ways.

Found, not changed

The DCOM fallback for the boot time creates a CimSession and never removes it. Lab hosts answer over WinRM, so that path cannot be covered here.

🤖 Generated with Claude Code

SqlUptime was the creation date of tempdb, a time in the local time of
the instance, converted to UTC with the time zone of the computer that
runs the command, and subtracted from the clock of that computer. When
the two are in different time zones, SqlUptime was off by the whole
difference.

The uptime is now ms_ticks - sqlserver_start_time_ms_ticks from
sys.dm_os_sys_info, which no clock and no daylight saving change can
shift. A login without VIEW SERVER STATE cannot read the view; for it,
the uptime is counted from the creation of tempdb up to GETDATE() of
the instance. SqlStartTime and the Windows part are unchanged.

The new context mocks Get-Date of the module five hours ahead, as the
clock test of #10820 does, and checks both paths, the second with a new
login that is asserted to lack the permission. With the old code both
are five hours too long.

Part of #10833

(do Get-DbaUptime)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andreasjordan
andreasjordan marked this pull request as ready for review November 15, 2026 17:33

@potatoqualitee potatoqualitee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The low-permission fallback in public/Get-DbaUptime.ps1:113-114 introduces a daylight-saving regression. When sys.dm_os_sys_info is denied, it subtracts two server-local timestamps without their historical offsets. For an Eastern-time instance started at 2026-10-31 12:00 (-04:00) and queried at 2026-11-01 12:00 (-05:00), it reports 24 hours instead of 25; the spring transition reports 24 instead of 23. This regresses the previous UTC-based calculation when client and server share a time zone and returns incorrect SqlUptime and SinceSqlStart for low-permission monitoring accounts.

Please preserve elapsed-time semantics using a server-derived elapsed-time source or the instance's historical time-zone offsets, or explicitly surface the inability to calculate accurate uptime. Add a DST regression assertion: the current fallback test repeats the same offset-free subtraction for its expected value, so it passes despite this error.

…login without VIEW SERVER STATE

The review of cf0ab8b found that the way for a login without VIEW
SERVER STATE subtracted two local times of the instance, the creation of
tempdb and GETDATE(). Every daylight saving change since the start lies
between them, so an instance in Eastern time that started on 2026-10-31
12:00 reported 24 hours on 2026-11-01 12:00 instead of 25. Before, the
creation date was converted to UTC with the time zone of this computer,
which is right when both are in the same time zone.

SQL Server 2022 and later name their time zone with
CURRENT_TIMEZONE_ID(), and AT TIME ZONE converts SqlStartTime with the
offset that time zone had at that date. Before SQL Server 2022 the
instance cannot name it, and SqlStartTime is again converted with the
time zone of this computer; only the end of the count stays the clock
of the instance. Test-DbaFeatureSupport gets the rule CurrentTimeZoneId
(SQL Server 2022, Azure SQL Database, Managed Instance). Its other
callers ask for Query Store features only and are not affected. An
engine without a rule there takes the conversion that works everywhere.

The fallback test compared with the same subtraction. It now compares
with the uptime that sys.dm_os_sys_info measures. The new context gives
the server objects of such a login a creation of tempdb before the last
offset change of the time zone and checks both ways, the second with a
server object that claims SQL Server 2019, so that it runs on every
version. With the Get-DbaUptime.ps1 of cf0ab8b both fail by one hour
in the lab (W. Europe Standard Time, change on 2026-10-25). In a time
zone without daylight saving time, such as UTC on the runners, there is
no change to count.

Part of #10833

(do Get-DbaUptime, Test-DbaFeatureSupport)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andreasjordan

Copy link
Copy Markdown
Collaborator Author

Confirmed, thanks. Fixed in 07310bc7b: the fallback for a login without VIEW SERVER STATE subtracted two local times of the instance, so a daylight saving change since the start was lost. On SQL Server 2022 and later, Azure SQL Database and Managed Instance, it now converts SqlStartTime with AT TIME ZONE CURRENT_TIMEZONE_ID(), which applies the offset that the time zone of the instance had at that date. Before SQL Server 2022 the instance cannot name its time zone, so the start is converted with the time zone of this computer again, as on development, and only the end of the count stays the clock of the instance. That keeps your shared time zone case right on every version. The gate is a new rule CurrentTimeZoneId in Test-DbaFeatureSupport.

The fallback test no longer repeats the subtraction: it compares with the uptime that sys.dm_os_sys_info measures. The new context "When daylight saving time changed since the instance started" gives the server objects of a login without the permission a creation of tempdb at the last noon before an offset change of the time zone, and checks both ways, the second with a server object that claims SQL Server 2019. The queries run on the real instance.

Lab, SQL03 (W. Europe Standard Time, change on 2026-10-25): Get-DbaUptime.Tests.ps1 11 passed, Test-DbaFeatureSupport.Tests.ps1 42 passed and dbatools.Tests.ps1 87 passed and 6 skipped, on Windows PowerShell 5.1 and on PowerShell 7.6. Also green on SQL04 with SQL Server 2022 as InstanceMulti1. With the Get-DbaUptime.ps1 of cf0ab8b83, both new tests fail, 3,601 seconds off. On runners in UTC there is no change to count, so there they check the uptime only.


This text was created by Claude and reviewed by Andreas Jordan.

@potatoqualitee potatoqualitee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous noon-to-noon DST issue is fixed, but the new low-permission fallback introduces a repeated-hour regression in public/Get-DbaUptime.ps1:125-127.

On SQL Server 2022+ in Eastern Standard Time, restart SQL during the second 2026-11-01 01:30 (offset -05:00), then query uptime at 03:30 -05:00 using a login denied sys.dm_os_sys_info. Actual uptime is two hours. AT TIME ZONE assigns the offset-free tempdb creation timestamp the earlier -04:00 offset, so the new fallback reports three hours in both SqlUptime and SinceSqlStart. This remains an hour wrong after a restart during the overlap. The previous shared-time-zone ToUniversalTime calculation uses the standard-time convention and returns two hours for this input.

Microsoft documents that AT TIME ZONE selects the pre-transition offset during overlaps: https://learn.microsoft.com/en-us/sql/t-sql/queries/at-time-zone-transact-sql#remarks.

Please handle ambiguous starts explicitly: preserve the prior standard-time convention in the instance's time zone and disclose its limitation, or surface that exact uptime cannot be inferred from an offset-free timestamp. Add a regression case for the second occurrence of the repeated hour; the new noon-based tests exclude it.

Independently checked against the exact head/base source and documented conversion rules. All four changed files, tests, CI, and discussions were reviewed. CI: 20 successful checks, one pre-existing skip. No PR code was executed locally.

…rrence for a login without VIEW SERVER STATE

The review of 07310bc found that AT TIME ZONE gives a local time in
the hour that repeats when the clocks go back the offset before the
change, the first occurrence. The creation date of tempdb has no offset,
so an instance in Eastern time that started in the second 2026-11-01
01:30 (-05:00) reported three hours at 03:30 instead of two. The
conversion with the time zone of this computer, which the command used
before and still uses before SQL Server 2022, counts such a time as
standard time, the second occurrence.

The query now takes the second occurrence when the local time repeats:
it lies the change of the offset later, which the offset one day later
gives, and it is a start only when it has the same local time. A probe
on SQL Server 2022 and 2025 compared this with ConvertTimeToUtc of .NET
for 16 local times in W. Europe, Eastern, Lord Howe (a change of half
an hour) and UTC, around both changes of 2026: the same instant in all
64 cases, while AT TIME ZONE alone was an hour (half an hour) early in
the six repeated ones. Both ways write a verbose message when the start
repeats, and the help of SqlUptime tells that an instance that started
in the first occurrence ran that much longer.

The new context gives the server objects of such a login a creation of
tempdb in the last repeated quarter of an hour of the past year and
checks both ways. With the Get-DbaUptime.ps1 of 07310bc the test of
the way in the time zone of the instance fails by one hour in the lab
(W. Europe Standard Time, 2026-10-25 02:45). In a time zone without
such an hour, such as UTC on the runners, the tests check the uptime
only.

Part of #10833

(do Get-DbaUptime)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andreasjordan

Copy link
Copy Markdown
Collaborator Author

Confirmed, thanks. Fixed in 0507999ac: the creation date of tempdb has no offset, and for a local time in the hour that repeats when the clocks go back, AT TIME ZONE takes the first occurrence, with the offset before the change. A probe on SQL Server 2022 and 2025 showed it for W. Europe, Eastern and Lord Howe. The query now takes the second occurrence when the local time repeats, which is the standard-time convention of ConvertTimeToUtc and of the old code, and still the way before SQL Server 2022. The second occurrence lies the change of the offset later, which the offset one day later gives, and it counts only when it has the same local time. In the probe this gave the instant of .NET for all 64 combinations of 16 local times around both changes of 2026 and four time zones, the half-hour change of Lord Howe included. Your example, a start in the second 2026-11-01 01:30 in Eastern time, now gets -05:00, so the uptime at 03:30 is two hours.

An offset-free start in that hour cannot be told apart, so this is a convention: the help of SqlUptime says that an instance that really started in the first occurrence ran one change of the clocks longer than reported, and both ways of the fallback write a verbose message when the start repeats.

The new context "When the instance started in the hour that repeats when the clocks go back" gives the server objects of a login without VIEW SERVER STATE a creation of tempdb in the last repeated quarter of an hour of the past year, and checks both ways against ConvertTimeToUtc and the verbose message.

Lab, SQL03 (W. Europe Standard Time, start 2026-10-25 02:45): Get-DbaUptime.Tests.ps1 13 passed and dbatools.Tests.ps1 87 passed and 6 skipped, on Windows PowerShell 5.1 and on PowerShell 7.6. Get-DbaUptime.Tests.ps1 also passes on SQL04 with SQL Server 2022 as InstanceMulti1, on both. With the Get-DbaUptime.ps1 of 07310bc7b, the test of the way in the time zone of the instance fails, 3,599 seconds off; the one before SQL Server 2022 fails only at the new verbose message, because that way already counted the second occurrence. On runners in UTC there is no such hour, so there the tests check the uptime only.


This text was created by Claude and reviewed by Andreas Jordan.

(do Get-DbaUptime, Test-DbaFeatureSupport)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants