9/23/2022

c++ 'time_t' to 'yyyy mm dd', 'tm' to 'time_t' , example source code

 


refer to bloew example code:


..

//current day of second from 1900.01.01
    time_t current_time;
    current_time = time(NULL);
    printf("%ld seconds since January 1, 1900\n", current_time);

    //time_t to yyyy mm dd
    char buffer[80];
    strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", localtime(&current_time));
    printf("%s", buffer);

    //make timt_t from custome yyyy mm dd
    struct tm s_tm = { 0 };
    s_tm.tm_year = 2022 - 1900;
    s_tm.tm_mon = 9 - 1;
    s_tm.tm_mday = 23;
    time_t s_tmt = mktime(&s_tm);
    printf("s_tmt %ld \n", s_tmt);

    /*
    struct tm {
        int tm_sec;         // seconds
        int tm_min;         // minutes
        int tm_hour;        // hours
        int tm_mday;        // day of the month
        int tm_mon;         // month
        int tm_year;        // year
        int tm_wday;        // day of the week
        int tm_yday;        // day in the year
        int tm_isdst;       // daylight saving time
    };
    */

..


Thank you.

www.marearts.com