Linux system time acquisition and setting

clock_gettime()

The prototype is int clock_gettime (clockid_t __clock_id, struct timespec *__tp);
Get the current system time, example:

/**
     * clock_gettime()的第一个参数可以如下设定:
     * CLOCK_REALTIME:获取当前系统的绝对时间,从UTC1970-1-1 0:0:0开始计时;
     * CLOCK_MONOTONIC:linux系统开机到现在的时间;
     * CLOCK_PROCESS_CPUTIME_ID:当前进程运行的时间;
     * CLOCK_THREAD_CPUTIME_ID:当前线程运行的时间;
     * struct timespec结构体中的tv_sec单位是秒,tv_nsec单位是纳秒;
     * 换算关系:1秒=1000毫秒,1毫秒=1000微秒, 1微秒=1000纳秒, 1纳秒= 1000飞秒
     */
    struct timespec sysTime;
    clock_gettime(CLOCK_REALTIME, &sysTime);
    printf("sysTime.tv_sec:%u\n", sysTime.tv_sec);
    printf("sysTime.tv_nsec:%u\n", sysTime.tv_nsec);

time()

Prototype: time_t time (time_t *__timer)
Get the absolute time of the current system, starting from UTC1970-1-1 0:0:0, in seconds;

time_t stTime;
time(&stTime);
printf("stTime:%u\n", stTime);

localtime_r()

Prototype: struct tm *localtime_r (const time_t *__restrict __timer, struct tm *__restrict __tp)
converts absolute time into a more intuitive time display, this function is a thread-safe function; the corresponding localtime() is a thread-unsafe function; localtime_r () can specify a time zone; gmtime_r() can also convert absolute time into a more intuitive time display, just UTC time; example:

    struct tm localTime;
    localtime_r(&stTime, &localTime);
    printf("the year:%u\n", localTime.tm_year); //从1900年开始到现在的年数
    printf("the month:%u\n", localTime.tm_mon);// 从一月算起到现在的月数;
    printf("the day:%u\n", localTime.tm_mday);//目前是几号;
    printf("the hour:%u\n", localTime.tm_hour);//从午夜算起的时数;
    printf("the minute:%u\n", localTime.tm_min);//目前的分数;
    printf("the second:%u\n", localTime.tm_sec);//目前的秒数;
    printf("the day of year:%u\n", localTime.tm_yday);//一年的第几天,从0开始;
    printf("the day of week:%u\n", localTime.tm_wday);//星期几,0是代表星期天;
    printf("the daylight saving time:%d\n", localTime.tm_isdst);//是否是夏令时;

asctime_r()

Prototype: char *asctime_r (const struct tm *__restrict __tp,
char *__restrict __buf)
converts the time of the struct tm structure to a string; the function is thread-safe; example:

char strBuf[1024];
asctime_r(&localTime, strBuf);
printf("The time is:%s\n", strBuf);

ctime_r functions similarly:

/**
* 将time_t 结构体的时间转换成字符串;
*/
ctime_r(&stTime, strBuf);
printf("The time is:%s\n", strBuf);

/**
* 另外还有strftime()函数,可用显式的将指定时间转换成需要的字符串格式;
* 而对应的strptime()则将字符串格式的时间转换到struct tm结构体中;
*/

gettimeofday()

Prototype: int gettimeofday (struct timeval *__restrict __tv,
__timezone_ptr_t __tz)
function similar to time(), example:

    struct timeval tv;
    struct timezone tz;
    gettimeofday(&tv, NULL);
    printf("The tv_sec is:%d\n", tv.tv_sec);
    printf("The tv_usec is:%d\n", tv.tv_usec);
//  printf("The tz_minuteswest is:%d\n", tz.tz_minuteswest);
//  printf("The tz_dsttime is:%d\n", tz.tz_dsttime);

week of day ()

Prototype: int settimeofday (const struct timeval *__tv,
const struct timezone *__tz)
to set the system time, the usage is as follows:

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

#include <time.h>
#include <sys/time.h>
void setSysTime(char *timeStr)
{
    struct timeval tv;
    struct tm localTime;  
    int year, month, day, hour, minute, second;

    sscanf(timeStr, "%d.%d.%d %d:%d:%d", &year, &month,&day,&hour,&minute,&second);
    localTime.tm_sec = second;
    localTime.tm_min = minute;
    localTime.tm_hour = hour;
    localTime.tm_mday = day;
    localTime.tm_mon = month-1;
    localTime.tm_year = year-1900;

    /**
     * 将struct tm结构体的时间转换成1970年1月1日以来逝去时间的秒数;
     */
    tv.tv_sec = mktime(&localTime);
    tv.tv_usec = 0;
    /**
     * 必须在root权限下运行该程序才能调用settimeofday()正常设定时间下去;
     */
    settimeofday(&tv, NULL);
}

int main(int argc, char *argv[])
{
    setSysTime("2018.5.1 22:43:50");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325628577&siteId=291194637