我从我的C#应用程序中调用了SetSystemTime。但是,如果我将Windows时区设置为相对UTC的非零偏移,似乎有时会调整系统时钟,好像我提供的时间是UTC(即转换为本地时间),而其他时候却不是这样。将时间直接设置为date
参数。
[StructLayout(LayoutKind.Sequential)]
internal struct SystemTime
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool SetSystemTime(ref SystemTime st);
public static bool AdjustSystemClock(DateTime date)
{
SystemTime systemTime = new SystemTime();
systemTime.Year = (short)date.Year;
systemTime.Month = (short)date.Month;
systemTime.Day = (short)date.Day;
systemTime.Hour = (short)date.Hour;
systemTime.Minute = (short)date.Minute;
systemTime.Second = (short)date.Second;
return SetSystemTime(ref systemTime);
}
区别似乎是:当我使用Windows设置时区时,然后启动应用程序,当我调用SetSystemTime()
它时,它会调整提供的时间,就好像它是UTC。
但是,当我使用该SetDynamicTimeZoneInformation()
功能设置时区时,请重新启动应用程序,然后调用,SetSystemTime()
然后它将时间直接设置为我提供的时间,而不管时区如何。
这是预期的行为吗?如何在设置时区的两种方法之间保持一致性?
我相信我找到了问题。
事实证明,编写该代码的人SetDynamicTimeZoneInformation()
被忽略了以设置该Bias
属性。
因此,所设置的时区信息的UTC偏移为零,因此不会进行任何调整。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句