time函数 localtime函数获取当前时间

#include 
#include 

int main(int argc,char ** argv) {
    time_t time1;
    time(&time1);
    struct tm tm1;
    tm1 = *(localtime(&time1));

    printf("%d-",tm1.tm_year + 1900);
    printf("%d-",tm1.tm_mon + 1);
    printf("%d ",tm1.tm_mday);
    printf("%d",tm1.tm_hour);
    printf(":%d",tm1.tm_min);
    printf(":%d ",tm1.tm_sec);
    switch(tm1.tm_wday){
        case 0:printf("Sunday\n");break;
        case 1:printf("Monday\n");break;
        case 2:printf("Tuesday\n");break;
        case 3:printf("Wednesday\n");break;
        case 4:printf("Thursday\n");break;
        case 5:printf("Friday\n");break;
        case 6:printf("Saturday\n");break;
    }
    printf("%s\n",tm1.tm_zone);

    return 0;
}

输出

2019-11-5 23:32:49 Tuesday
CST

 

你可能感兴趣的:(linux,C)