栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

【linux

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【linux

手动设置系统时间和硬件时间||通过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(2022,6,12,21,0,0);
int ret = stime(&tm);
//配置时间时需要把系统时钟同步到硬件时钟.
system("hwclock -w -u");
通过ntp服务器更新系统时间(后续更新)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/974257.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号