linux系统时间获取与设定

clock_gettime()

原型为int clock_gettime (clockid_t __clock_id, struct timespec *__tp);
获取当前系统时间,示例:

/**
     * 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()

原型:time_t time (time_t *__timer)
获取当前系统的绝对时间,从UTC1970-1-1 0:0:0开始计时,单位是秒;

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

localtime_r()

原型:struct tm *localtime_r (const time_t *__restrict __timer,struct tm *__restrict __tp)
将绝对时间转换成更直观表达的时间显示,该函数为线程安全函数;对应的localtime()是线程不安全函数; localtime_r()可指定时区; gmtime_r()也可以将绝对时间转换成更直观表达的时间显示,只不过是UTC时间;示例:

    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()

原型:char *asctime_r (const struct tm *__restrict __tp,
char *__restrict __buf)
将struct tm 结构体的时间转换成字符串;该函数线程安全;示例:

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

ctime_r功能类似:

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

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

gettimeofday()

原型:int gettimeofday (struct timeval *__restrict __tv,
__timezone_ptr_t __tz)
功能类似time(),示例:

    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);

settimeofday()

原型:int settimeofday (const struct timeval *__tv,
const struct timezone *__tz)
设定系统时间,用法如下:

#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;
}

猜你喜欢

转载自blog.csdn.net/zyj_zhouyongjun183/article/details/80160885