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