2011-02-28 95 views
66

我想获取我系统的当前时间。对于我使用在C以下代码:获取C中的当前时间

time_t now; 
struct tm *mytime = localtime(&now); 
if (strftime(buffer, sizeof buffer, "%X", mytime)) 
{ 
    printf("time1 = \"%s\"\n", buffer); 
} 

的问题是,这个代码是给一些随机的时间。而且,随机时间每次都不相同。我想要我的系统的当前时间。

+0

的可能的复制[如何让C程序的日期和时间值(https://stackoverflow.com/questions/1442116/how-to-get-date-and-time-价值在C程序) – 2017-10-24 20:08:05

回答

92

复制粘贴从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; 
} 

(刚加入 “空白” 到main()的参数列表,以便在C工作)

+0

任何想法如何做相反的方式?字符串到tm *? – Goaler444 2013-03-23 13:46:00

+5

我知道这可能有点晚了,你现在可能已经知道了,但是你可以使用['strptime'](http://pubs.opengroup.org/onlinepubs/7908799/xsh/strptime.html)函数在[time.h](http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html)中将'char *'转换为'struct tm' – KingRadical 2013-11-05 19:59:53

+0

请注意,asctime()会留下一个\ n在字符串的末尾。 – h7r 2014-03-23 22:15:08

55

初始化您的now变量。

time_t now = time(0); // Get the system time 

localtime功能用于在经过time_t时间值转换为struct tm,实际上它并不检索系统时间。

29

简单的方法:

#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] = '\0'; 
    printf("Current Time : %s\n", time_str); 

    return 0; 
} 
+1

在最后添加一个额外的换行符。 – 2017-12-13 03:17:02

+0

@JamesKo True ... – pm89 2018-02-14 18:24:09

-3

我有一个新的方式获得系统时间的家伙。虽然它很长,并且充满了愚蠢的作品,但通过这种方式,你可以获得整数格式的系统时间。

#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; 
} 
+3

Linux中的时间库和系统调用并不令人震惊,但即使如此,您仍然希望使用它们,而不是出去找shell命令并尝试解析它。查看手册页的时间和ctime和rtc。 – Dana 2014-06-26 20:28:27

+7

这太可怕了。 – 2015-01-21 13:50:13

1
#include <stdio.h> 
#include <time.h> 

void main() 
{ 
    time_t t; 
    time(&t); 
    clrscr(); 

    printf("Today's date and time : %s",ctime(&t)); 
    getch(); 
} 
+0

您能详细解释一下使用纯文本的解决方案吗? – Magnilex 2015-01-05 07:27:59

+2

'void main()'应该是'int main(void)'。 – 2015-07-27 07:16:28

6

为了延长从@mingos答案以上,我写的以下函数我的时间来格式化为特定格式([年月日HH:MM:SS])。

// 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); 
} 

更多有关struct tm可以发现here

+0

这有帮助!谢谢。 – hiquetj 2017-02-08 16:42:03

4

你可以使用这个函数获得当前的本地时间。如果你想gmtime然后使用gmtime函数而不是本地时间。欢呼声

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); 
6
#include<stdio.h> 
#include<time.h> 

void main() 
{ 
    time_t t; 
    time(&t); 
    printf("\n current time is : %s",ctime(&t)); 
}