C语言 在 C 中获取当前时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5141960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the current time in C
提问by Antrromet
I want to get the current time of my system. For that I'm using the following code in C:
我想获取系统的当前时间。为此,我在 C 中使用以下代码:
time_t now;
struct tm *mytime = localtime(&now);
if ( strftime(buffer, sizeof buffer, "%X", mytime) )
{
printf("time1 = \"%s\"\n", 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.
问题是这段代码给出了一些随机时间。此外,随机时间每次都不同。我想要我系统的当前时间。
回答by mingos
Copy-pasted from 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;
}
(just add "void" to the main() arguments list in order for this to work in C)
(只需将“void”添加到 main() 参数列表中,以便在 C 中工作)
回答by Erik
Initialize your nowvariable.
初始化你的now变量。
time_t now = time(0); // Get the system time
The localtimefunction is used to convert the time value in the passed time_tto a struct tm, it doesn't actually retrieve the system time.
该localtime函数用于将传递的时间值转换time_t为 a struct tm,它实际上并不检索系统时间。
回答by zishan
Easy way:
简单的方法:
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t mytime = time(NULL);
char * time_str = ctime(&mytime);
time_str[strlen(time_str)-1] = '// Store the formatted string of time in the output
void format_time(char *output){
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
sprintf(output, "[%d %d %d %d:%d:%d]",timeinfo->tm_mday, timeinfo->tm_mon + 1, timeinfo->tm_year + 1900, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
}
';
printf("Current Time : %s\n", time_str);
return 0;
}
回答by hexinpeter
To extend the answer from @mingos above, I wrote the below function to format my time to a specific format ([dd mm yyyy hh:mm:ss]).
为了扩展上面@mingos 的答案,我编写了下面的函数来将我的时间格式化为特定格式([dd mm yyyy hh:mm:ss])。
#include<stdio.h>
#include<time.h>
void main()
{
time_t t;
time(&t);
printf("\n current time is : %s",ctime(&t));
}
More information about struct tmcan be found here.
struct tm可以在此处找到有关的更多信息。
回答by guneet
time_t my_time;
struct tm * timeinfo;
time (&my_time);
timeinfo = localtime (&my_time);
CCLog("year->%d",timeinfo->tm_year+1900);
CCLog("month->%d",timeinfo->tm_mon+1);
CCLog("date->%d",timeinfo->tm_mday);
CCLog("hour->%d",timeinfo->tm_hour);
CCLog("minutes->%d",timeinfo->tm_min);
CCLog("seconds->%d",timeinfo->tm_sec);
回答by Shauket Sheikh
guys you can use this function to get current local time. if you want gmtime then use gmtime function instead of localtime. cheers
伙计们,您可以使用此功能获取当前本地时间。如果你想要 gmtime,那么使用 gmtime 函数而不是 localtime。干杯
time_t rawtime;
struct tm * timeinfo;
time( &rawtime );
timeinfo = localtime( &rawtime );
printf("%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min,
timeinfo->tm_sec);
回答by kame
If you just need the time without the date.
如果您只需要没有日期的时间。
#include <stdio.h>
#include <time.h>
void main()
{
time_t t;
time(&t);
clrscr();
printf("Today's date and time : %s",ctime(&t));
getch();
}
回答by user309026
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char hc1,hc2,mc1,mc2;
int hi1,hi2,mi1,mi2,hour,minute;
system("echo %time% >time.txt");
fp=fopen("time.txt","r");
if(fp==NULL)
exit(1) ;
hc1=fgetc(fp);
hc2=fgetc(fp);
fgetc(fp);
mc1=fgetc(fp);
mc2=fgetc(fp);
fclose(fp);
remove("time.txt");
hi1=hc1;
hi2=hc2;
mi1=mc1;
mi2=mc2;
hi1-=48;
hi2-=48;
mi1-=48;
mi2-=48;
hour=hi1*10+hi2;
minute=mi1*10+mi2;
printf("Current time is %d:%d\n",hour,minute);
return 0;
}
回答by p2013
guys i got a new way get system time. though its lengthy and is full of silly works but in this way you can get system time in integer format.
伙计们,我有了一种获取系统时间的新方法。虽然它很长并且充满了愚蠢的作品,但通过这种方式您可以获得整数格式的系统时间。
##代码##
