unix time

UTC:

  At 0:00 on January 1, 1970 since the number of seconds. (This article does not consider leap seconds)

local time:

  UTC time zone corresponds to 0, Beijing is located in the East eight districts, the time is UTC + 8h.

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

int main(int argc, char **argv)
{
    time_t utc_timestamp = time(NULL);
    printf("utc timestamp is: %ld\n", utc_timestamp);

    struct tm *utc_tm = gmtime(&utc_timestamp);
    printf("utc time is: %s\n", asctime(utc_tm));
    
    struct tm *local_tm = localtime(&utc_timestamp);
    printf("local time is: %s\n", asctime(local_tm));
    
    printf("local time is: %s\n", ctime(&utc_timestamp));
    
    return 0;
}

Related API:

// 返回UTC时间戳
time_t time (time_t* timer);
// Convert time_t to tm as UTC time
struct tm * gmtime (const time_t * timer);
// Convert time_t to tm as local time
struct tm * localtime (const time_t * timer);
// Convert tm structure to string
char* asctime (const struct tm * timeptr);
// Convert time_t value to string, in terms of local time
char* ctime (const time_t * timer);

tm structure:

struct tm {
    int    tm_sec;      /* seconds after the minute [0-60] */
    int    tm_min;      /* minutes after the hour [0-59] */
    int    tm_hour;     /* hours since midnight [0-23] */
    int    tm_mday;     /* day of the month [1-31] */
    int    tm_mon;      /* months since January [0-11] */
    int    tm_year;     /* years since 1900 */
    int    tm_wday;     /* days since Sunday [0-6] */
    int    tm_yday;     /* days since January 1 [0-365] */
    int    tm_isdst;    /* Daylight Savings Time flag */
    long   tm_gmtoff;   /* offset from CUT in seconds */
    char   *tm_zone;    /* timezone abbreviation */
};

time_t general or in a long long long.

 

Reproduced in: https: //www.cnblogs.com/gattaca/p/6927941.html

Guess you like

Origin blog.csdn.net/weixin_33948416/article/details/93401954