RT-thread之RTC时间的获取
开启RTC后,用rt-thread官方给的例程,可以正确打印时间了,但如何使用时间的数值呢?比如要判断是白天和晚上的时间??官方没有给出说明,但前面说过,rt-thread与linux操作很相似,和时间相关time.h也是兼容ANSIC的。所以关于时间的获取这块,可以参考linux时间的用法。下面简要说明下linux时间几种使用方式。
linux下常用的几个时间函数:asctime, ctime, gmtime, localtime, gettimeofday ,mktime, asctime_r, ctime_r, gmtime_r, localtime_r,看了下time.h中相关,rt-thread基本上也都有。
我们可以找到下列四种表示“时间”的结构体:
/* Returned by `time'. */
typedef __time_t time_t; //time_t 是一个长整型,用来表示秒数。/* A time value that is accurate to the nearestmicrosecond but also has a range of years. */
struct timeval
{__time_t tv_sec; /* Seconds. */__suseconds_t tv_usec; /* Microseconds. */
};struct timespec
{__time_t tv_sec; /* Seconds. */long int tv_nsec; /* Nanoseconds. */
};struct timezone
{int tz_minuteswest; /* minutes west of Greenwich */int tz_dsttime; /* type of DST correction */
};
int tz_minuteswest; /* 格林威治时间往西方的时差 */
int tz_dsttime; /* 时间的修正方式*/ struct tm
{int tm_sec; /* Seconds. [0-60] (1 leap second) */int tm_min; /* Minutes. [0-59] */int tm_hour; /* Hours. [0-23] */int tm_mday; /* Day. [1-31] */int tm_mon; /* Month. [0-11] */int tm_year; /* Year - 1900. */int tm_wday; /* Day of week. [0-6] */int tm_yday; /* Days in year.[0-365] */int tm_isdst; /* DST. [-1/0/1]*/#ifdef __USE_BSDlong int tm_gmtoff; /* Seconds east of UTC. */__const char *tm_zone; /* Timezone abbreviation. */
#elselong int __tm_gmtoff; /* Seconds east of UTC. */__const char *__tm_zone; /* Timezone abbreviation. */
#endif
};
其中:
time_t 是一个长整型,用来表示秒数。
struct timeval 结构体是用秒和微妙来表示时间。
struct timespec 结构体是用秒和纳秒来表示时间。
struct timezone结构体用格林尼治时间来表示。
struct tm 直接用秒、分、小时、天、月、年等来表示时间。
很显然它们的精度是各不相同的。供各种不同的需要提供各种不同的选择。
获取时间的方法:
#include <time.h>
time_t time(time_t *timer);
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
参数说明:
参数说明: timer=NULL时得到当前日历时间(从1970-01-01 00:00:00到现在的秒数),timer=时间数值时,用于设置日历时间,time_t是一个unsigned long类型。如果 timer不为空,则返回值也存储在变量 timer中。
函数功能: 得到当前日历时间或者设置日历时间
#include <stdio.h>
#include <string.h>
#include <time.h>int main()
{time_t seconds;seconds = time((time_t *)NULL);printf("%d\n", seconds);return 0;
}
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
可以获取精确到微秒当前距离1970-01-01 00:00:00 +0000 (UTC)的微秒数。
//gettimeofday函数获取当前时间存于tv结构体中,相应的时区信息则存于tz结构体中
//需要注意的是tz是依赖于系统,不同的系统可能存在获取不到的可能,因此通常设置为NULL
#include <stdio.h>
#include <string.h>
#include <sys/time.h>int main()
{struct timeval tv;gettimeofday(&tv, NULL);printf("tv_sec: %d\n", tv.tv_sec);printf("tv_usec: %d\n", tv.tv_usec);return 0;
}
#include <time.h>
int clock_gettime(clockid_t clk_id, struct timespec *tp);
可以获取精确到纳秒当前距离1970-01-01 00:00:00 +0000 (UTC)的纳秒数。
如以下例子:
#include <time.h>
#include <stdio.h>int main()
{struct timespec ts;clock_gettime(CLOCK_REALTIME, &ts);printf("%.24s %ld Nanoseconds\n", ctime(&ts.tv_sec), ts.tv_nsec);return 0;
}
此三种方法比较常用。
#include <time.h>
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
取得当地目前时间和日期
/*该函数将有time函数获取的值timep转换真实世界所使用的时间日期表示方法,然后将结果由结构tm返回*/
/**需要注意的是localtime函数可以将时间转换本地时间,但是localtime函数不是线程安全的。
多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的**/
如下面例子:
#include <stdio.h>
#include <string.h>
#include <time.h>int main()
{time_t timep;struct tm *p;time(&timep);p = localtime(&timep);printf("%d-%d-%d %d:%d:%d\n", (1900 + p->tm_year), ( 1 + p->tm_mon), p->tm_mday,(p->tm_hour + 12), p->tm_min, p->tm_sec);return 0;
}
关于时间的格式化输出:
#include <time.h>
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
将时间和日期以字符串格式返回
/**gmtime是把日期和时间转换为格林威治(GMT)时间的函数。将参数time 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回**/
/**asctime 将时间以换为字符串字符串格式返回 **/
#include <stdio.h>
#include <string.h>
#include <time.h>int main()
{time_t timep;time(&timep);printf("%s\n", asctime(gmtime(&timep)));return 0;
}
#include <time.h>
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
将时间和日期以字符串格式表示
/**ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回**/
#include <stdio.h>
#include <string.h>
#include <time.h>int main(void)
{time_t timep;time(&timep);printf("%s\n", ctime(&timep));return 0;
}
#include<time.h>
sizt_t strftime
(
char
*s,
size_t
max,
const
char
*format,
const
struct
tm
*
tm
);
很显然,要严格控制时间的输出格式,只能用strftime函数,但是该函数只能对struct tm表示的时间进行输出,所以这里要涉及到struct timeval, struct timespec, time_t等表示的时间如何转换成struct tm的形式。另外因为strutc tm只能精确到秒,所i毫秒、微秒、纳秒只能另外进行输出了。
所以,我们采取的方式是:
首先将struct timeval, struct timespec等转换成time_t表示的秒数;
struct timeval tv;struct timespec ts;time_t sec_tv = tv.tv_sec;time_t sec_ts = ts.ts_sec;
然后利用下列函数将time_t转换成struct tm,
1 2 |
|
或者:
1 2 |
|
最后利用strftime函数进行格式化,得到最后的时间字符串。至于毫秒、微秒、纳秒另外用进行输出。如:
#include <time.h>
#include <sys/time.h>
#include <stdio.h>int main()
{struct timeval tv;char strTime[32];gettimeofday(&tv, NULL);struct tm *ptm = gmtime(&tv.tv_sec); //将秒转换成struct tm的形式strftime(strTime, 32, "%F %T", ptm);printf("%s ", strTime); //输出精确到秒printf("%ld Micorseconds\n", (long)tv.tv_usec); //输出微秒return 0;
}
另:
形如gmtime和形如gmtime_t函数的区别是,gmtime获得的返回的结果存在于一个static的struct tm型的变量中,可能被后面的gmtime调用覆盖掉,如果要防止覆盖,我们可以自己提供一个struct tm 型的变量,利用gmtime_r函数,将我们自己定义的变量的地址传进去,将结果保存在其中。这样就可以避免覆盖。
关于ctime和asctime等函数得到的时间字符串,它具有指定的形如("Wed Jun 30 21:49:08 1993\n")的格式,所以不利与我们不能进行格式化。注意该格式的最后具有换行符:'\n'.
#include <time.h>
time_t mktime(struct tm *tm);
/**将时间结构体struct tm的值转化为经过的秒数**/
#include <stdio.h>
#include <string.h>
#include <time.h>int main()
{time_t timep;struct tm *p;time(&timep);p = localtime(&timep);timep = mktime(p);printf("%d\n", timep);return 0;
}
RT-thread之RTC时间的获取相关推荐
- rt thread studio使用QBOOT和片外flash实现OTA升级
我们这里要使用单片机外部flash作为OTA的下载分区,外部flash硬件连接关系 PB3-->SPI3_CLK PB4-->SPI3_MISO PB5-->SPI3_MOSI PE ...
- 【linux】ARM开发板上设置RTC时间,断电重启后,设置失效的原因分析
问题描述 linux中使用date设置时间后用hwclock -w同步到RTC,断电重启后,有时会失效 原因分析 保存时间戳 1.使用命令关机(halt)会调用rc0.d中的脚本: 2.使用命令重启( ...
- 关于RT thread系统节拍时钟的配置
关于RT thread系统节拍时钟的配置 -----本文基于rt-thread-3.1.3版本编写 首先,使用RTthread OS时,要配置(或者明白)它的系统节拍 ...
- RT Thread Free Modbus移植问题整理
RT Thread Free Modbus移植问题整理 问题描述: 在读写寄存器中,写数据正常,只能读1个寄存器的值,多个值会异常. 在移植过程中发现串口(或RS485)数据接收长度异常. 一.环境描 ...
- Yeelink平台使用——远程控制 RT Thread + LwIP+ STM32
1.前言 [2014年4月重写该博文] 经过若干时间的努力终于搞定了STM32+LwIP和yeelink平台的数据互通,在学习的过程中大部分时间花在以太网协议栈学习上,但是在RT Th ...
- RT Thread之 Uart2 操作
官网连接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/uar ...
- rt thread系统下添加wiznet软件包后,不插网线CPU利用率100%问题
rt thread系统下添加wiznet软件包后如果不插网线的话其他任务运行很卡,使用ps命令发现优先级低的任务很多都超时了 rt thread线程错误码 添加了一个可以查看CPU利用率的软件包CPU ...
- stm32f407单片机rt thread 片外spi flash OTA升级配置示例
参考地址https://www.rt-thread.org/document/site/application-note/system/rtboot/an0028-rtboot/ 第一步,生成Boot ...
- xpt 2046的触摸屏 rt thread设备驱动框架
1 基于rtt 开发触摸屏驱动 准备使用rtt 框架 , 驱动xpt 2046的触摸屏, 翻阅大量资料发现, 大部分文章强调的是时序图, 而且很多代码要么直接操作寄存器, 要么是io 口模拟, 只能用 ...
最新文章
- qt 多个模型如何显示在表格中_Qt MOOC系列教程 第五章第四节:QML中的C++模型
- 自监督学习,如何从数据困境中拯救深度学习?
- spring+mybatis报错
- Android应用安全开发之浅谈加密算法的坑
- (转)GitHub 被微软收购后的 52 天,改版并放弃了 jQuery!
- Elasticsearch 2.3.0 重建索引
- JQuery的click、bind、delegate、off、unbind
- python调用本地exe_python调用exe程序 python怎么调用exe程序
- 计算机硬件4核是什么意思,8核,6核,4核和双核CPU是什么意思?
- 《Python语言程序设计》刘卫国主编字符串与正则表达式习题5详解(选择)
- 两场面试,一次心灵洗礼
- Python 飞机航班案例分析
- linux执行scp命令出错
- node.js-day04
- Android手机总是提示:存储空间不足,解决方法
- Excel如何快速插入图片?
- kaggle中的房价预测的一些数据分析方法详解
- pentaho SPARK
- verilog 实现9位有符号乘法器
- 基于ZigBee的WPAN网络配置应用