我想获取系统的当前时间。为此,我在 C 中使用以下代码:
time_t now;
struct tm *mytime = localtime(&now);
if ( strftime(buffer, sizeof buffer, "%X", mytime) )
{
printf("time1 = \"%s\"\n", buffer);
}
问题是这段代码给出了一些随机时间。而且,每次的随机时间都不一样。我想要系统的当前时间。
复制粘贴自here:
/* localtime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
只需添加void
to the main()
参数列表以便在 C 中工作
PS:Linuxgettimeofday()
from <sys/time.h>
has tv_sec
and tv_microsec
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)