Repository navigation
Get-DbaUptime - Let the instance measure its uptime - #10852
andreasjordan wants to merge 4 commits into
Conversation
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>
potatoqualitee
left a comment
There was a problem hiding this comment.
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>
|
Confirmed, thanks. Fixed in The fallback test no longer repeats the subtraction: it compares with the uptime that Lab, SQL03 (W. Europe Standard Time, change on 2026-10-25): This text was created by Claude and reviewed by Andreas Jordan. |
potatoqualitee
left a comment
There was a problem hiding this comment.
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>
|
Confirmed, thanks. Fixed in An offset-free start in that hour cannot be told apart, so this is a convention: the help of 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 Lab, SQL03 (W. Europe Standard Time, start 2026-10-25 02:45): This text was created by Claude and reviewed by Andreas Jordan. |
(do Get-DbaUptime, Test-DbaFeatureSupport)
Part of #10833 (found by the sweep for siblings of #10847 and #10848)
Problem
SqlUptimewasNew-TimeSpan -Start $SQLStartTime.ToUniversalTime() -End $nowutc.$SQLStartTimeis 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$nowutccame from that computer's clock. When the computer and the instance are in different time zones,SqlUptimeandSinceSqlStartare off by the whole difference, for example five hours for the setup of #10818.Change
ms_ticks - sqlserver_start_time_ms_ticksfromsys.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 simplerGETDATE()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_infoneeds 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:CONVERT(datetime2(3), <SqlStartTime>) AT TIME ZONE CURRENT_TIMEZONE_ID()up toSYSDATETIMEOFFSET().AT TIME ZONEapplies 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, andAT TIME ZONEtakes the first. The query counts it as the second, in standard time, asConvertTimeToUtcof .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.SYSUTCDATETIME()of the instance.Test-DbaFeatureSupportgets the ruleCurrentTimeZoneIdfor 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.SqlUptimesay where the value comes from, andSqlUptimenames 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.LastBootTimefrom 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 withMock Get-Date -ModuleName dbatools, the same exception the clock test of #10820 uses, and creates a SQL login without any permissions:SqlUptimematches the uptime thatsys.dm_os_sys_inforeports, within two minutessys.dm_os_sys_info(asserted, so that the next test really takes the fallback)SqlUptimematches the uptime thatsys.dm_os_sys_inforeports, within two minutesNew 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.
SqlUptimematches the start converted with the rules of that time zone in .NET, within two minutesSqlUptimematches the start converted with the time zone of this computerNew 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 forCurrentTimeZoneId, its first version in the independent list, the Azure SQL Database case, and a test that asksInstanceSingleitself whether it knowsCURRENT_TIMEZONE_ID().Lab (SQL03, W. Europe Standard Time, SQL Server 2025 as
InstanceMulti1, SQL Server 2019 asInstanceSingle):Get-DbaUptime.Tests.ps113 passed,Test-DbaFeatureSupport.Tests.ps142 passed anddbatools.Tests.ps187 passed and 6 skipped, each on Windows PowerShell 5.1 and on PowerShell 7.6. Also green on SQL04 (SQL Server 2022 asInstanceMulti1, 2025 asInstanceSingle):Get-DbaUptime.Tests.ps113 passed on 5.1 and 7.6. With theGet-DbaUptime.ps1ofdevelopment, 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 ofdevelopmentis right when both time zones are the same. With theGet-DbaUptime.ps1ofcf0ab8b83, the two tests of the second context fail, 3,601 seconds off, the hour of the change on 2026-10-25. With theGet-DbaUptime.ps1of07310bc7b, 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-DbaFeatureSupportis unchanged since07310bc7b, so its numbers are from that round.)After review
Daylight saving time for a login without VIEW SERVER STATE (
07310bc7b). The review ofcf0ab8b83found that the fallback subtracted two local times of the instance, the creation of tempdb andGETDATE(), 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 withsys.dm_os_sys_info, and the new context covers the change itself.A start in the repeated hour (
0507999ac). The review of07310bc7bfound thatAT TIME ZONEgives 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 ofConvertTimeToUtcfor 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 ofSqlUptimesays 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
CimSessionand never removes it. Lab hosts answer over WinRM, so that path cannot be covered here.🤖 Generated with Claude Code