< Summary

Information
Class: NtpServiceLibrary.Win32SystemTime
Assembly: NtpServiceLibrary
File(s): D:\a\ntp-service\ntp-service\NtpServiceLibrary\WindowsTime.cs
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 113
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Set(...)100%44100%
Get()100%22100%
GetLocal()100%22100%

File(s)

D:\a\ntp-service\ntp-service\NtpServiceLibrary\WindowsTime.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel;
 3using System.Runtime.InteropServices;
 4
 5namespace NtpServiceLibrary
 6{
 7    public interface ISystemTimeProvider
 8    {
 9        bool SetSystemTime(ref Win32SystemTime.SystemTime st);
 10        bool GetSystemTime(ref Win32SystemTime.SystemTime st);
 11        bool GetLocalTime(ref Win32SystemTime.SystemTime st);
 12    }
 13
 14    public class SystemTimeProvider : ISystemTimeProvider
 15    {
 16        [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "SetSystemTime")]
 17        private static extern bool _SetSystemTime(ref Win32SystemTime.SystemTime st);
 18
 19        [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetSystemTime")]
 20        private static extern bool _GetSystemTime(ref Win32SystemTime.SystemTime st);
 21
 22        [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetLocalTime")]
 23        private static extern bool _GetLocalTime(ref Win32SystemTime.SystemTime st);
 24
 25        public bool SetSystemTime(ref Win32SystemTime.SystemTime st) => _SetSystemTime(ref st);
 26        public bool GetSystemTime(ref Win32SystemTime.SystemTime st) => _GetSystemTime(ref st);
 27        public bool GetLocalTime(ref Win32SystemTime.SystemTime st) => _GetLocalTime(ref st);
 28    }
 29    /// <summary>
 30    /// Provides static methods for interacting with the Windows system time.
 31    /// </summary>
 32    public class Win32SystemTime
 33    {
 34        private readonly ISystemTimeProvider _provider;
 1435        public Win32SystemTime(ISystemTimeProvider provider)
 1436        {
 1437            _provider = provider;
 1438        }
 39
 40        [StructLayout(LayoutKind.Sequential)]
 41        public struct SystemTime
 42        {
 43            public ushort Year;
 44            public ushort Month;
 45            public ushort DayOfWeek;
 46            public ushort Day;
 47            public ushort Hour;
 48            public ushort Minute;
 49            public ushort Second;
 50            public ushort Millisecond;
 51        }
 52
 53        /// <summary>
 54        /// Sets the system time (UTC).
 55        /// </summary>
 56        /// <param name="dt">The UTC time to set.</param>
 57        public void Set(DateTime dt)
 858        {
 859            if (dt.Kind != DateTimeKind.Utc)
 460            {
 461                throw new ArgumentException("SetSystemTime requires DateTimeKind.Utc", nameof(dt));
 62            }
 63
 464            SystemTime systemTime = new SystemTime
 465            {
 466                Year = (ushort)dt.Year,
 467                Month = (ushort)dt.Month,
 468                Day = (ushort)dt.Day,
 469                DayOfWeek = (ushort)dt.DayOfWeek,
 470                Hour = (ushort)dt.Hour,
 471                Minute = (ushort)dt.Minute,
 472                Second = (ushort)dt.Second,
 473                Millisecond = (ushort)dt.Millisecond
 474            };
 75
 476            if (!_provider.SetSystemTime(ref systemTime))
 277            {
 278                int err = Marshal.GetLastWin32Error();
 279                throw new InvalidOperationException($"SetSystemTime failed: {new Win32Exception(err).Message}", new Win3
 80            }
 281        }
 82
 83        /// <summary>
 84        /// Gets the current system time (UTC).
 85        /// </summary>
 86        /// <returns>The current UTC time.</returns>
 87        public DateTime Get()
 488        {
 489            SystemTime systemTime = new SystemTime();
 490            if (!_provider.GetSystemTime(ref systemTime))
 291            {
 292                int err = Marshal.GetLastWin32Error();
 293                throw new InvalidOperationException($"GetSystemTime failed: {new Win32Exception(err).Message}", new Win3
 94            }
 295            return new DateTime(systemTime.Year, systemTime.Month, systemTime.Day, systemTime.Hour, systemTime.Minute, s
 296        }
 97
 98        /// <summary>
 99        /// Gets the current local system time.
 100        /// </summary>
 101        /// <returns>The current local time.</returns>
 102        public DateTime GetLocal()
 4103        {
 4104            SystemTime systemTime = new SystemTime();
 4105            if (!_provider.GetLocalTime(ref systemTime))
 2106            {
 2107                int err = Marshal.GetLastWin32Error();
 2108                throw new InvalidOperationException($"GetLocalTime failed: {new Win32Exception(err).Message}", new Win32
 109            }
 2110            return new DateTime(systemTime.Year, systemTime.Month, systemTime.Day, systemTime.Hour, systemTime.Minute, s
 2111        }
 112    }
 113}