In 2012, there was a question here on SO, whether .NET's DateTime are capable of recognizing leap seconds. [1] The answer was no.
The documentation is still explicitly stating that it is not. [2]
However, Windows Server 2019 and the Windows 10 October 2018 update made Windows itself leap second aware. [3]
This begs the question: is .NET inherently leap second aware now? More specifically: can I make my DateTime
structures leap second aware as well, by somehow opting in?
Edit:
From a MS Word document titled "Quest: Write a Leap Second Aware Application on Windows" [4] (emphasis by me):
Known issues: Some frameworks are known to calculate time incorrectly after a leap second occurs. For example, the .NET Framework uses its own internal logic to determine what time it is. Its logic does not account for leap seconds. So after a leap second is introduced to the Operating System the output of “System.DateTime.Now.ToString()” will be ahead by one second of the local system time. (We are working with the .NET framework team on this.)
And from [5]:
Some applications are known to calculate time incorrectly by assuming that there are always 60 seconds in a minute. Since leap seconds can change this behavior, they will improperly record the time during this event. For example (at the time of writing):
.NET Framework uses its own internal logic to determine what time it is and does not account for leap seconds. As a result, PowerShell, which relies on the .NET Framework, will not report the 61st second (number 60) when using Get-Date
Event Viewer: The date of the event will be incorrectly recorded. However, the event metadata will properly record the system time (showing the 60th second).
Note: These teams are working towards updating their software to use more appropriate math when handling leap seconds.
So it seems that .NET will be leap second aware some time in the future. Thus I will not post this as the solution.
[1] Are .Net's DateTime methods capable of recognising a Leap Second?
[3] https://support.microsoft.com/en-us/help/2722715/support-for-the-leap-second
[4] https://aka.ms/Dev-LeapSecond (MS Word)
[5] https://aka.ms/ITPro-LeapSecond (MS Word)
Source: https://github.com/dotnet/dotnet-api-docs/issues/966#issuecomment-434440807
Expanding on the accepted answer by providing some more details possibly of interest.
While UTC is aware of the occasional (and disputed [1]) insertion of a leap second, .NET's
DateTime
structure prior to Windows Server 2019 and the Windows 10 October 2018 update is not,[2] because Windows itself was neither.[3][4]But even with Window's kernel now being leap seconds aware, applications still are not, unless specific action is taken.[5] The .NET framework itself does not support leap seconds yet.[6]
However, Windows systems update their time via NTP (using UTC), which is leap seconds aware.[7] This could cause time discontinuities with Windows systems, because the Windows Time service working as a NTP client could wish to synchronize the system's time immediately upon next synchronization somewhen after the leap second, potentially causing the clock to jump backwards 1 second.[8]
Of course, the occurrence of a negative time value could (and on Linux systems did [9][10]) result in undesirable or unpredictable behavior, which is why Windows won't let this happen.
When calling the
DateTime.Now
methods, the leap second aware Windows API functionGetSystemTimeAsFileTime
is called. When obtaining the leap second 23:59:60 UTC, it is treated as a second occurence of 23:59:59 UTC without re-counting the 100 ns ticks, but keeping all 10 million ticks of that second at the maximum tick count possible instead, i.e., the timekeeping is effectively halted for a second.[11]Note, that entering 23:59:60 as a time in a
DateTime
structure causes .Net to perform checks as per the so far occured leap seconds. If it was one indeed, it will be accepted (but converted to 23:59:59).[11][1] International Earth Rotation and Reference Systems Service (IERS); Coordinated Universal Time (UTC) to retain "leap second" – https://www.iers.org/SharedDocs/Publikationen/EN/IERS/Publications/messages/IERS_Message_No_282.html
[2] Microsoft; DateTime.Ticks Property – https://docs.microsoft.com/en-us/dotnet/api/system.datetime.ticks?redirectedfrom=MSDN&view=netframework-4.8#System_DateTime_Ticks
[3] Microsoft; How the Windows Time service treats a leap second – https://support.microsoft.com/en-us/help/909614/how-the-windows-time-service-treats-a-leap-second
[4] Microsoft; Support for the leap second – https://support.microsoft.com/en-us/help/2722715/support-for-the-leap-second
[5] Microsoft; Leap Second Validation for Developers – https://aka.ms/Dev-LeapSecond
[6] Microsoft; Leap Second Validation for IT Pros – https://aka.ms/ITPro-LeapSecond
[7] D. Mills (University of Delaware); Leap Second Processing – https://www.eecis.udel.edu/~mills/ntp/html/leap.html
[8] Microsoft; How the Windows Time service treats a leap second – https://support.microsoft.com/en-us/help/909614/how-the-windows-time-service-treats-a-leap-second
[9] Wired; 'Leap Second' Bug Wreaks Havoc Across Web – https://www.wired.com/2012/07/leap-second-bug-wreaks-havoc-with-java-linux/
[10] /root.in; Leap second bug in linux kernel – https://www.slashroot.in/leap-second-bug-linux-kernel
[11] Github; Leap second statement is confusing – https://github.com/dotnet/dotnet-api-docs/issues/966#issuecomment-434440807