time()函数调用时问什么与当地时间相差8个小时?

#include
#include
#include
#include
//struct tm *gmtime(const time_t *timep);
int main ()
{
struct tm *t;
// gettime(&t);
time_t timep;
int a,b;
POINT SS;
GetCursorPos(&SS);
// Sleep(5000);

// printf("%d:%d:%d\n",6+t->tm_hour,t->tm_min,t->tm_sec);
while(1)
{
Sleep(1000);
time (&timep);
t=gmtime(&timep);
if(t->tm_hour==13&&t->tm_min==15&&t->tm_sec==30)
break;
printf("%d:%d:%d\n",t->tm_hour,t->tm_min,t->tm_sec);
}
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
printf ("%d %d\n",SS.x,SS.y);
return 0;
}

本来想做一个定时点击的小程序......

因为中国在东八区!你获取的是标准时间,然后本地时间需要加上时区的时间,也就是在中国需要+8小时!

编译后结果与实际差别

gmtime是没时区信息的,换成localtime试试

也许因为计算机默认的开始时间是从1970.1.1 8:00:00

Uses the value pointed by timer to fill a tm structure with the values that represent the corresponding time, expressed as a UTC time (i.e., the time at the GMT timezone).自己谷歌翻译一下,gtime()以UTC时间为准,北京时区是东八区,领先UTC 8个小时

时差的问题,我做java也遇到这样的问题,我前台获取date类型正常传到后台就多了12个小时,最后发现是时差的问题,我在前台把data给tostring一下,传到后台就正常了

一般的话localtime别叫实用,当初做小实验的时候,感觉localtime比gmtime实用

哥老官,time()一般来获取时间差的~