zoukankan      html  css  js  c++  java
  • 系统时钟和硬件时钟同步

    硬件时钟调整与系统时钟一致:

    //system("hwclock -w")

    int SyncSystemClockToHw(void)
    {
    	struct timeval tv;
    	struct tm tm_time;
    	int fd;
    	int ret = 0;
    
    	ret = access("/dev/rtc1",F_OK);
    	if(0 == ret)
    	{
    		fd = open("/dev/rtc1", O_WRONLY);//只写
    		if (fd < 0)
    		{
    			TIMER_INFO("open /dev/rtc0  failed!");
    			return -1;
    		}
    	}
    	else
    	{
    		fd = open("/dev/rtc0", O_WRONLY);
    		if (fd < 0)
    		{
    			TIMER_INFO("open /dev/rtc0  failed!");
    			return -1;
    		}
    	}
    
    	gettimeofday(&tv, NULL);
    	/* Prepare tm_time */
    		
    	localtime_r((time_t*)&tv.tv_sec, &tm_time);
    	tm_time.tm_isdst = 0;
    	
    	ioctl(fd, RTC_SET_TIME, &tm_time);
    	
    	close(fd);
    	return 0;
    	//system("hwclock -w");
    }


    系统时钟调整与硬件时钟一致:

    //system("hwclock -s")

    int SyncHwClockToSystem(void)
    {
    
    	struct timeval tv;
    	struct timezone tz;
    	struct tm tm_time;
    	int fd;
    	int ret = 0;
    
    	gettimeofday(&tv, &tz);
    
    	ret = access("/dev/rtc1",F_OK);
    	if(0 == ret)
    	{
    		fd = open("/dev/rtc1", O_WRONLY);//只写
    		if (fd < 0)
    		{
    			TIMER_INFO("open /dev/rtc0  failed!");
    			return -1;
    		}
    	}
    	else
    	{
    		fd = open("/dev/rtc0", O_WRONLY);
    		if (fd < 0)
    		{
    			TIMER_INFO("open /dev/rtc0  failed!");
    			return -1;
    		}
    	}
    
    	memset(&tm_time, 0, sizeof(tm_time));
    	ioctl(fd, RTC_RD_TIME, &tm_time);
    	tm_time.tm_isdst = -1; /* "not known" */
    
    	close(fd);
    
    	tv.tv_sec = mktime(&tm_time);
    	tv.tv_usec = 0;
    	if (settimeofday(&tv, &tz))
    	{
    		TIMER_INFO("settimeofday failed!");
    		return -1;
    	}
    
    	return 0;
    	//system("hwclock -s");
    }

  • 相关阅读:
    c++ 利用new动态的定义二维数组
    golang在linux后台执行的方法
    Linux安装配置go运行环境
    SpringCloud 笔记
    你真的了解 Unicode 和 UTF-8 吗?
    Elasticsearch 系列文章汇总(持续更新...)
    Maven 的依赖范围
    在 centos 上安装 virutalbox
    Java 异常总结
    使用 RabbitMQ 实现异步调用
  • 原文地址:https://www.cnblogs.com/wangfengju/p/6172362.html
Copyright © 2011-2022 走看看