手动设置系统时间和硬件时间||通过ntp同步更新系统时间
前言手动设置系统时钟并同步硬件时钟先把年月日通过函数返回time_t 时间戳通过stime函数设置系统时间并同步硬件时钟 通过ntp服务器更新系统时间(后续更新)前言
在Linux中有硬件时钟与系统时钟等两种时钟。
当Linux系统启动时,系统时钟会去读硬件时钟,它们两是独立的,读完后,后面各自时间有可能不一样,可以进行同步,linux时间函数读的都是系统时钟
手动设置系统时钟并同步硬件时钟
先把年月日通过函数返回time_t 时间戳
//当前时区的墙钟时间转time_t time_t JTime_mktime_ex(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second){struct tm stTm={0};stTm.tm_year = year - 1900;stTm.tm_mon = month - 1;stTm.tm_mday = day;stTm.tm_hour = hour;stTm.tm_min = minute;stTm.tm_sec = second;stTm.tm_isdst = -1;return mktime(&stTm);}
通过stime函数设置系统时间并同步硬件时钟
time_t tm = JTime_mktime_ex(,6,12,21,0,0);int ret = stime(&tm);//配置时间时需要把系统时钟同步到硬件时钟.system("hwclock -w -u");