CPU:RK3288

系统:Linux

IC:hym8563

在 Linux 系统中,指令 date 和 hwclock 都可以读写时间

date:读写系统时间,写时间需要管理员权限

hwclock:读写硬件时间,也就是 rtc 模块的时间,读写都必须有管理员权限

// 读取当前系统时间
$ date
Thu Oct :: UTC
// 普通用户设置时间失败
$ date 102411032019.00
date: cannot set date: Operation not permitted
Thu Oct :: UTC
// 超级用户设置时间成功,格式:月日时分年.秒
$ sudo date 102411032019.00
Thu Oct :: UTC
// 读取当前系统时间,确认时间设置成功
// 小时与设置的11点不同,这是由于时区造成的
$ date
Thu Oct :: UTC

设置时区的问题,请参考:[Linux] 通过指令修改时区 tzselect

$ date
Thu Oct :: CST
// 查看硬件时间
$ sudo hwclock
Thu Oct :: PM CST . seconds
// 查看硬件时间
$ sudo hwclock -r
Thu Oct :: PM CST . seconds
// 查看硬件时间
$ sudo hwclock -show
Thu Oct :: PM CST . seconds
// 修改硬件时间,与系统时间相同
$ sudo hwclock -w
// 修改系统时间,与硬件时间相同
$ sudo hwclock -w

rtc 测试 demo:

#include <stdio.h>
#include <stdlib.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h> int main(int argc, char *argv[])
{
int fd, retval;
struct rtc_time rtc_tm;
time_t timep;
struct tm *p; fd = open("/dev/rtc", O_RDONLY);
if (fd == -) {
fprintf(stderr, "open /dev/rtc error\n");
exit(errno);
} /* Read the RTC time/date */
retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
if (retval == -) {
perror("ioctl");
exit(errno);
} fprintf(stderr, "RTC date/time: %d/%d/%d %02d:%02d:%02d\n",
rtc_tm.tm_mday, rtc_tm.tm_mon + , rtc_tm.tm_year + ,
rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
time(&timep);
p = gmtime(&timep);
fprintf(stderr, "OS date/time(UTC): %d/%d/%d %02d:%02d:%02d\n",
p->tm_mday, p->tm_mon + , p->tm_year + ,
p->tm_hour, p->tm_min, p->tm_sec);
p = localtime(&timep);
fprintf(stderr, "OS date/time(Local): %d/%d/%d %02d:%02d:%02d\n",
p->tm_mday, p->tm_mon + , p->tm_year + ,
p->tm_hour, p->tm_min, p->tm_sec); rtc_tm.tm_mday = ;
rtc_tm.tm_mon = ;
rtc_tm.tm_hour = ;
rtc_tm.tm_min = ;
retval = ioctl(fd, RTC_SET_TIME, &rtc_tm);
if (retval == -) {
perror("ioctl");
exit(errno);
} /* Write the RTC time/date */
retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
if (retval == -) {
perror("ioctl");
exit(errno);
} fprintf(stderr, "RTC date/time: %d/%d/%d %02d:%02d:%02d\n",
rtc_tm.tm_mday, rtc_tm.tm_mon + , rtc_tm.tm_year + ,
rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
time(&timep);
p = gmtime(&timep);
fprintf(stderr, "OS date/time(UTC): %d/%d/%d %02d:%02d:%02d\n",
p->tm_mday, p->tm_mon + , p->tm_year + ,
p->tm_hour, p->tm_min, p->tm_sec);
p = localtime(&timep);
fprintf(stderr, "OS date/time(Local): %d/%d/%d %02d:%02d:%02d\n",
p->tm_mday, p->tm_mon + , p->tm_year + ,
p->tm_hour, p->tm_min, p->tm_sec); close(fd); return ;
}

参考:https://blog.csdn.net/u010703935/article/details/11728091

[Linux] RTC 读写指令及测试程序的更多相关文章

  1. linux rtc 接口【转】

    转自:http://blog.csdn.net/goldfighter/article/details/6126178 Linux操作系统内核对RTC的编程详解 转自: http://xenyinze ...

  2. linux驱动基础系列--linux rtc子系统

    前言 linux驱动子系统太多了,连时钟也搞了个子系统,这导致一般的时钟芯片的驱动也会涉及到至少2个子系统,一个是时钟芯片接口子系统(比如I2c接口的时钟芯片),一个是内核给所有时钟芯片提供的rtc子 ...

  3. (转)Linux最常用指令及快捷键 Linux学习笔记

    Linux最常用指令及快捷键 Linux学习笔记 原文:http://blog.csdn.net/yanghongche/article/details/50827478 [摘自 鸟叔的私房菜]--转 ...

  4. linux的一些指令

    linux的一些指令 █查看指令參數man 指令,如:man ls ,按q鍵退出 █查看文件列表ls -lht 按時間排序ll 列表ls 列表 目錄cd 目錄名稱 進入路徑cd .. 返回上層路徑 █ ...

  5. Linux文件常用指令

    目录 Linux文件常用指令 1.pwd 显示当前目录 2.cd 切换目录 3.mkdir 创建目录 4.touch 修改或创建文件 5.ls 显示目录下的内容 6.cat 查看文件信息 7.echo ...

  6. Linux入门50指令

    1, mkdir创建目录 make directory 可以带着路径,在什么位置创建什么目录 如:mkdir /etc/date  在目录etc下创建date目录 重要参数 –p 递归创建,mkdir ...

  7. linux常用操作指令

    Linux常用操作指令: 常用指令 ls        显示文件或目录 -l           列出文件详细信息l(list) -a          列出当前目录下所有文件及目录,包括隐藏的a(a ...

  8. linux RTC 驱动模型分析【转】

    转自:http://blog.csdn.net/yaozhenguo2006/article/details/6824970 RTC(real time clock)实时时钟,主要作用是给Linux系 ...

  9. Linux日常使用指令大全

    Linux日常使用指令大全 Java代码 www.ahlinux.com 001.日常维护常用查询命令 #top    显示系统进程 #clear  清理屏幕信息 #cat /etc/redhat-r ...

随机推荐

  1. springmvc集成cxf的方法

    最近需要在项目中增加webservice接口,供三方调用,下面就把集成的方法展示如下,供大家参考: 第一步:服务端的发布; 1:配置web.xml文件,添加cxf的servlet <servle ...

  2. [MySQL]重装数据库后无法启动服务

    具体情况请查看下面网址 https://www.jianshu.com/p/8dda50ee812f 错误信息可以在CMD里输入mysqld --console查看

  3. Springboot默认定时任务——Scheduled注解

    1.pom配置 <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  4. zabbix 自动发现(LLD)

    概述 自动发现(LLD)提供了一种在计算机上为不同实体自动创建监控项,触发器和图形的方法.例如,Zabbix可以在你的机器上自动开始监控文件系统或网络接口,而无需为每个文件系统或网络接口手动创建监控项 ...

  5. JSON空值处理与 StringUtils工具类使用

    JSON 动态查询时,需要的条件本应是null,前端传入的是" " //null转换为"" private static ValueFilter filter ...

  6. AQS的子类在各个同步工具类中的使用情况

    AQS AQS(AbstractQueuedSynchronizer)是 java.util.concurrent的基础.J.U.C中宣传的封装良好的同步工具类Semaphore.CountDownL ...

  7. 关于Spring的常用注解和接口

    接口 1. BeanPostProcessor 2. FactoryBean 3. Condition 4. ImportSelector 5. ImportBeanDefinitionRegitra ...

  8. spring源码分析系列 (15) 设计模式解析

    spring是目前使用最为广泛的Java框架之一.虽然spring最为核心是IOC和AOP,其中代码实现中很多设计模式得以应用,代码看起来简洁流畅,在日常的软件设计中很值得借鉴.以下是对一些设计模式的 ...

  9. python中的一切皆对象

    python中一切皆对象是这个语言灵活的根本.函数和类也是对象,属于python的一等公民.包括代码包和模块也都是对象.python的面向对象更加彻底. 可以赋值给一个变量可以添加到集合对象中可以作为 ...

  10. MySQL 效率提高N倍的19条MySQL优化秘籍

    一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单的示例,标注(1,2,3,4,5)我们要重点关注的数据 type列,连接类型.一个好的sql语句至少 ...