瀏覽代碼

Refactor SystemTimeUtils and add exception to SetSystemTime

master
The6P4C 7 年之前
父節點
當前提交
9ce90cdf8c
共有 2 個檔案被更改,包括 42 行新增32 行删除
  1. +10
    -7
      ThunderboltTimeSync/FormMain.cs
  2. +32
    -25
      ThunderboltTimeSync/SystemTimeUtils.cs

+ 10
- 7
ThunderboltTimeSync/FormMain.cs 查看文件

@@ -1,6 +1,5 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Security.Principal;
using System.Windows.Forms; using System.Windows.Forms;


namespace ThunderboltTimeSync { namespace ThunderboltTimeSync {
@@ -19,12 +18,16 @@ namespace ThunderboltTimeSync {


InitializeComponent(); InitializeComponent();


Debug.WriteLine(SystemTimeUtils.GetTime().ToString("yyyy-MM-ddTHH:mm:ssK.ffff"));
SystemTimeUtils.SetTime(new DateTime(
2000, 1, 1,
10, 0, 0, 0
));
Debug.WriteLine(SystemTimeUtils.GetTime().ToString("yyyy-MM-ddTHH:mm:ssK.ffff"));
Debug.WriteLine(SystemTimeUtils.GetSystemTime().ToString("yyyy-MM-ddTHH:mm:ssK.ffff"));
try {
SystemTimeUtils.SetSystemTime(new DateTime(
2000, 1, 1,
10, 0, 0, 0
));
} catch (SystemTimeUtils.SystemTimeException stEx) {
Debug.WriteLine(string.Format("Setting system time failed: \"{0}\"", stEx.Message));
}
Debug.WriteLine(SystemTimeUtils.GetSystemTime().ToString("yyyy-MM-ddTHH:mm:ssK.ffff"));
} }
} }
} }

+ 32
- 25
ThunderboltTimeSync/SystemTimeUtils.cs 查看文件

@@ -1,36 +1,43 @@
using System; using System;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;


namespace ThunderboltTimeSync { namespace ThunderboltTimeSync {
class SystemTimeUtils { class SystemTimeUtils {
[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME {
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
};
public class SystemTimeException : Exception {
public SystemTimeException(int hresult) : base(string.Format("The system time could not be set (HRESULT 0x{1:X8}).", hresult)) {
HResult = hresult;
}
}


[DllImport("kernel32.dll")]
private static extern uint GetLastError();
private class WindowsAPI {
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME {
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
};


[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("kernel32.dll")]
public static extern int GetLastError();


[DllImport("kernel32.dll")]
private static extern void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetSystemTime(ref SYSTEMTIME lpSystemTime);

[DllImport("kernel32.dll")]
public static extern void GetSystemTime(ref SYSTEMTIME lpSystemTime);
}


/// <summary> /// <summary>
/// Sets the system time. /// Sets the system time.
/// </summary> /// </summary>
/// <param name="dateTime">The date and time to set the system clock to.</param> /// <param name="dateTime">The date and time to set the system clock to.</param>
public static void SetTime(DateTime dateTime) {
SYSTEMTIME systemTime = new SYSTEMTIME();
public static void SetSystemTime(DateTime dateTime) {
WindowsAPI.SYSTEMTIME systemTime = new WindowsAPI.SYSTEMTIME();


systemTime.wYear = (short) dateTime.Year; systemTime.wYear = (short) dateTime.Year;
systemTime.wMonth = (short) dateTime.Month; systemTime.wMonth = (short) dateTime.Month;
@@ -41,10 +48,10 @@ namespace ThunderboltTimeSync {
systemTime.wSecond = (short) dateTime.Second; systemTime.wSecond = (short) dateTime.Second;
systemTime.wMilliseconds = (short) dateTime.Millisecond; systemTime.wMilliseconds = (short) dateTime.Millisecond;


bool setSucceeded = SetSystemTime(ref systemTime);
bool setSucceeded = WindowsAPI.SetSystemTime(ref systemTime);


if (!setSucceeded) { if (!setSucceeded) {
Debug.WriteLine(string.Format("Call failed: error = {0}", GetLastError()));
throw new SystemTimeException(WindowsAPI.GetLastError());
} }
} }


@@ -52,9 +59,9 @@ namespace ThunderboltTimeSync {
/// Retrieves the current system time. /// Retrieves the current system time.
/// </summary> /// </summary>
/// <returns>The current system time.</returns> /// <returns>The current system time.</returns>
public static DateTime GetTime() {
SYSTEMTIME systemTime = new SYSTEMTIME();
GetSystemTime(ref systemTime);
public static DateTime GetSystemTime() {
WindowsAPI.SYSTEMTIME systemTime = new WindowsAPI.SYSTEMTIME();
WindowsAPI.GetSystemTime(ref systemTime);


DateTime systemDateTime = new DateTime( DateTime systemDateTime = new DateTime(
systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wYear, systemTime.wMonth, systemTime.wDay,


Loading…
取消
儲存