< Summary

Information
Class: NtpServiceLibrary.SystemTimeProvider
Assembly: NtpServiceLibrary
File(s): D:\a\ntp-service\ntp-service\NtpServiceLibrary\WindowsTime.cs
Line coverage
66%
Covered lines: 2
Uncovered lines: 1
Coverable lines: 3
Total lines: 113
Line coverage: 66.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SetSystemTime(...)100%210%
GetSystemTime(...)100%11100%
GetLocalTime(...)100%11100%

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
 025        public bool SetSystemTime(ref Win32SystemTime.SystemTime st) => _SetSystemTime(ref st);
 226        public bool GetSystemTime(ref Win32SystemTime.SystemTime st) => _GetSystemTime(ref st);
 227        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;
 35        public Win32SystemTime(ISystemTimeProvider provider)
 36        {
 37            _provider = provider;
 38        }
 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)
 58        {
 59            if (dt.Kind != DateTimeKind.Utc)
 60            {
 61                throw new ArgumentException("SetSystemTime requires DateTimeKind.Utc", nameof(dt));
 62            }
 63
 64            SystemTime systemTime = new SystemTime
 65            {
 66                Year = (ushort)dt.Year,
 67                Month = (ushort)dt.Month,
 68                Day = (ushort)dt.Day,
 69                DayOfWeek = (ushort)dt.DayOfWeek,
 70                Hour = (ushort)dt.Hour,
 71                Minute = (ushort)dt.Minute,
 72                Second = (ushort)dt.Second,
 73                Millisecond = (ushort)dt.Millisecond
 74            };
 75
 76            if (!_provider.SetSystemTime(ref systemTime))
 77            {
 78                int err = Marshal.GetLastWin32Error();
 79                throw new InvalidOperationException($"SetSystemTime failed: {new Win32Exception(err).Message}", new Win3
 80            }
 81        }
 82
 83        /// <summary>
 84        /// Gets the current system time (UTC).
 85        /// </summary>
 86        /// <returns>The current UTC time.</returns>
 87        public DateTime Get()
 88        {
 89            SystemTime systemTime = new SystemTime();
 90            if (!_provider.GetSystemTime(ref systemTime))
 91            {
 92                int err = Marshal.GetLastWin32Error();
 93                throw new InvalidOperationException($"GetSystemTime failed: {new Win32Exception(err).Message}", new Win3
 94            }
 95            return new DateTime(systemTime.Year, systemTime.Month, systemTime.Day, systemTime.Hour, systemTime.Minute, s
 96        }
 97
 98        /// <summary>
 99        /// Gets the current local system time.
 100        /// </summary>
 101        /// <returns>The current local time.</returns>
 102        public DateTime GetLocal()
 103        {
 104            SystemTime systemTime = new SystemTime();
 105            if (!_provider.GetLocalTime(ref systemTime))
 106            {
 107                int err = Marshal.GetLastWin32Error();
 108                throw new InvalidOperationException($"GetLocalTime failed: {new Win32Exception(err).Message}", new Win32
 109            }
 110            return new DateTime(systemTime.Year, systemTime.Month, systemTime.Day, systemTime.Hour, systemTime.Minute, s
 111        }
 112    }
 113}