SetSystemTime 与服务器时间相差八个小时怎么解决?

boff131 2011-05-08 12:01:30
char sYear[5],sMonth[3],sDay[3],sHour[3],sMinute[3],sSecond[3];
memset(sYear,0,5);
memset(sMonth,0,3);
memset(sDay,0,3);
memset(sHour,0,3);
memset(sMinute,0,3);
memset(sSecond,0,3);

//分析读到的数据格式,从中解析出时间数据
strncpy(sYear,m_pReadBuf+8,4);
strncpy(sMonth,m_pReadBuf+12,2);
strncpy(sDay,m_pReadBuf+14,2);
strncpy(sHour,m_pReadBuf+16,2);
strncpy(sMinute,m_pReadBuf+18,2);
strncpy(sSecond,m_pReadBuf+20,2);

//根据服务器发过来的时间数据设置本机时间
SYSTEMTIME time;
time.wYear = atoi(sYear);
time.wMonth = atoi(sMonth);
time.wDay = atoi(sDay);
time.wHour = atoi(sHour);
time.wMinute = atoi(sMinute);
time.wSecond = atoi(sSecond);
time.wMilliseconds = 0;
time.wDayOfWeek = 0;

SetSystemTime(&time);

客户方从服务器得到时间后 更改自己的时间 结果多了八个小时 怎么解决这八个小时呢?使用SetLocalTime(&time)结果客户方该不了自己的时间 怎么办呢
...全文
975 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
WinEggDrop 2011-05-11
  • 打赏
  • 举报
回复
根本就不需要调用SetLocalTime两次。调用SetLocalTime时它就会在更新时间前就激活SE_SYSTEMTIME_NAME权限。以下是msdn原话

The SetLocalTime function enables the SE_SYSTEMTIME_NAME privilege before changing the local time and disables the privilege before returning
boff131 2011-05-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bokutake 的回复:]

你让服务器把时区设置也发过来吧,然后自己计算一下应该就可以了。
如果用SetLocalTime最好连续调用两次,第一次系统会修正time的。
[/Quote]


SYSTEMTIME time;
time.wYear = atoi(sYear);
time.wMonth = atoi(sMonth);
time.wDay = atoi(sDay);
time.wHour = atoi(sHour);
time.wMinute = atoi(sMinute);
time.wSecond = atoi(sSecond);
time.wMilliseconds = 0;
time.wDayOfWeek = 0;

SetSystemTime(&time);
SetSystemTime(&time);

这样还是不行啊 服务器发送时区 应该怪麻烦的吧?
boff131 2011-05-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lostying 的回复:]

我一般用GetLocalTime,GetSystemTime获取的应该是格林威治时间?刚好8小时
[/Quote]

用GetLocalTime 客户端的系统时间不改变啊 怎么回事呢
辰岡墨竹 2011-05-08
  • 打赏
  • 举报
回复
你让服务器把时区设置也发过来吧,然后自己计算一下应该就可以了。
如果用SetLocalTime最好连续调用两次,第一次系统会修正time的。
hztj2005 2011-05-08
  • 打赏
  • 举报
回复
The SetLocalTime function enables the SE_SYSTEMTIME_NAME privilege

这句英语看起来怪怪的。
无言猪 2011-05-08
  • 打赏
  • 举报
回复
我一般用GetLocalTime,GetSystemTime获取的应该是格林威治时间?刚好8小时
辰岡墨竹 2011-05-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zwfgdlc 的回复:]

用SetLocalTime()要在SE_SYSTEMTIME_NAME 的权限,先提升进程的权限.

Remarks
The SetLocalTime function enables the SE_SYSTEMTIME_NAME privilege before changing the local time. This privilege is disabled by default.……
[/Quote]

SetSystemTime也会需要这个权限,一般来说XP下只要是管理员用户运行的话,SetSystemTime和SetLocalTime都会自动提权成功的。
LZ注意我说的是SetLocalTime调用两遍……
boff131 2011-05-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xumaojun 的回复:]

由于时区问题
本机是北京时间:(UTC+08:00)北京,重庆,香港特别行政区,乌鲁木齐
与标准的UTC时间相差8小时
[/Quote]
我也知道是时差问题 现在的问题是怎么样解决这多出来的八个小时的问题
xumaojun 2011-05-08
  • 打赏
  • 举报
回复
由于时区问题
本机是北京时间:(UTC+08:00)北京,重庆,香港特别行政区,乌鲁木齐
与标准的UTC时间相差8小时
zwfgdlc 2011-05-08
  • 打赏
  • 举报
回复
用SetLocalTime()要在SE_SYSTEMTIME_NAME 的权限,先提升进程的权限.

Remarks
The SetLocalTime function enables the SE_SYSTEMTIME_NAME privilege before changing the local time. This privilege is disabled by default. For more information, see Running with Special Privileges.

「已注销」 2011-05-08
  • 打赏
  • 举报
回复
mktime()转一下,减去8小时的秒数。再用gettime转回来。设到系统里就可以了。(不过这样会把毫秒数给丢了。)

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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