我在网络论坛上找到了一段示例代码。当我开始学习它。我发现输出很奇怪。
为什么代码无法正常运行?
当我运行下面的代码时,它总是向前走8小时。
使用VS2005和WinXP。谢谢。
class Tester
{
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError =
true)]
private static extern bool GetSystemTime(out SYSTEMTIME systemTime);
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError =
true)]
private static extern bool SetSystemTime(ref SYSTEMTIME systemTime);
struct SYSTEMTIME
{
internal short wYear;
internal short wMonth;
internal short wDayOfWeek;
internal short wDay;
internal short wHour;
internal short wMinute;
internal short wSecond;
internal short wMilliseconds;
}
static void Main()
{
SYSTEMTIME st;
if (GetSystemTime(out st))
{
st.wHour = 2; // new system time was set to nearby 10:00 AM, 2 + 8
// If i replace the line with below one.
// st.wHour = 18; // new system time was set to nearby 2:00 AM next day, 18 + 8 = 26, 26 - 24. go to next day!
if (SetSystemTime(ref st))
Console.WriteLine("success");
else
Console.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
}
else
Console.WriteLine("GetSystemTime failed: {0}",
System.Runtime.InteropServices.Marshal.GetLastWin32Error());
}
}
答案 0 :(得分:1)
以UTC格式返回系统时间 - 改为使用GetLocalTime:
http://msdn.microsoft.com/en-us/library/ms724390(VS.85).aspx
答案 1 :(得分:1)