2010-08-17 23 views

回答

18

strftime

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

int main() 
{ 
    char date[9]; 
    time_t t = time(0); 
    struct tm *tm; 

    tm = gmtime(&t); 
    strftime(date, sizeof(date), "%Y%m%d", tm); 
    printf("log.%s\n", date); 
    return EXIT_SUCCESS; 
} 
+0

你實際上不需要'tm'臨時文件 - 你可以直接將'gmtime(&t)'的返回值傳遞給'strftime()'。 – caf 2010-08-18 02:18:24

+0

@caf:我認爲重點是要顯示'gmtime'返回的內容。通常你不會只打印它。 – bitmask 2013-07-30 16:20:50