在 C 中获取当前时间 [英] Get the current time in C
本文介绍了在 C 中获取当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取系统的当前时间.为此,我在 C 中使用以下代码:
I want to get the current time of my system. For that I'm using the following code in C:
time_t now;
struct tm *mytime = localtime(&now);
if ( strftime(buffer, sizeof buffer, "%X", mytime) )
{
printf("time1 = "%s"
", buffer);
}
问题是这段代码给出了一些随机时间.此外,随机时间每次都不同.我想要系统的当前时间.
The problem is that this code is giving some random time. Also, the random time is different everytime. I want the current time of my system.
推荐答案
从 这里:
/* 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
添加到 main()
参数列表中即可使其在 C 中工作)
(just add void
to the main()
arguments list in order for this to work in C)
这篇关于在 C 中获取当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文