2011-02-28 91 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)); 
}