晕了!!UniversalTime 和 LocalTime 的问题!高手一定帮忙啊!

renchao 2003-05-17 02:39:56
我想用API实现“设置系统时间”:

[DllImport("Kernel32.dll")]
public static extern bool SetSystemTime(ref SYSTEMTIME st);

public static void SetTime(string sTime)
{
string[] sTimeParts = sTime.Split(":");

SYSTEMTIME st;
GetSystemTime(out st);
st.wHour = System.UInt16.Parse(sTimtParts[0]);
st.wMinute = System.UInt16.Parse(sTimtParts[1]);
st.wSecond = System.UInt16.Parse(sTimtParts[2]);

SetSystemTime(ref st);
}

程序可以正常运行运行,但时间总是设置错误。
比如调用:SetTime("11:32:25"),系统时间会变成"19:32:25"。

调试发现是UniversalTime和LocalTime的问题。
我查看dt.ToUniversalTime().ToString(),为正确的"11:32:25";
再查看dt.ToLocalTime().ToString(),为错误的"19:32:25";

可Windows任务栏中显示的是LocalTime呀,怎么办呢?
我看了半天都晕了,高手帮忙给改改吧?谢谢了。
...全文
1452 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
renchao 2003-05-17
  • 打赏
  • 举报
回复
哦,试试
谢了
coollzh 2003-05-17
  • 打赏
  • 举报
回复
如:
System.DateTime datetime = DateTime.Now;
datetime = datetime.AddHours(-8);
自动变成前一天的时间,呵呵。
renchao 2003-05-17
  • 打赏
  • 举报
回复
我试过-8:
st.wHour = nHour - 8;

但这样的话,当nHour > 8时,程序正确
小于8时,为负值了,也不行呀?
coollzh 2003-05-17
  • 打赏
  • 举报
回复
SDK这么说:
SetSystemTime
The SetSystemTime function sets the current system time and date. The system time is expressed in Coordinated Universal Time (UTC).
也就是说你设置的时间会被根据时区(系统区域)自动转换成本地时间,我们是东8区,所以得你的时间要减去8才能设置正确,这也是为什么你系统的时间总是比你设定的时间早8小时

好运 2003-05-17
  • 打赏
  • 举报
回复
GetSystemTime(),怎么用?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧