最近在调试stm32L151单片机,因为业务需要将从RTC获取的时间转换成时间戳。转换的时候发现获取的时间戳一直不对。一直被两个问题困扰。

1.从RTC获取出来的月份为什么比实际月份小1?

2.转换得来的时间戳一直不对。

检查半天发现原来是我没有正确的理解C中的struct tm

 struct tm {
int tm_sec; /* 秒,范围从 0 到 59 */
int tm_min; /* 分,范围从 0 到 59 */
int tm_hour; /* 小时,范围从 0 到 23 */
int tm_mday; /* 一月中的第几天,范围从 1 到 31 */
int tm_mon; /* 月份,范围从 0 到 11 */
int tm_year; /* 自 1900 起的年数 */
int tm_wday; /* 一周中的第几天,范围从 0 到 6 */
int tm_yday; /* 一年中的第几天,范围从 0 到 365 */
int tm_isdst; /* 夏令时 */
};

tm.tm_year表示的是自1900起的年数。我一直以为这个是当前的年份。所以导致了时间戳一直不对。

tm.tm_mon表示月份,范围从0到11 也就是说0代表的是1月份。

另外附上RTC其他代码:

static void RTC_Config(void)
{
#if 1
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR , ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Allow access to RTC */
PWR_RTCAccessCmd(ENABLE); /* LSE used as RTC source clock */
/* The RTC Clock may varies due to LSE frequency dispersion. */
/* Enable the LSE OSC */
RCC_LSEConfig(RCC_LSE_ON);
//RCC_LSEConfig(RCC_LSE_Bypass);
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
} /* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */
if (ERROR == RTC_WaitForSynchro())
{
printf("Wait for RTC APB registers synchronisation Failed\r\n");
}
#endif
/* Calendar Configuration */
RTC_InitStructure.RTC_AsynchPrediv = ;//0x7F;
RTC_InitStructure.RTC_SynchPrediv = 0x120; /* (37KHz / 128) - 1 = 0x120*/
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
if(RTC_Init(&RTC_InitStructure) == ERROR)
{
printf("Rtc_Init failed\r\n");
}
RTC_TimeStampCmd(RTC_TimeStampEdge_Rising,ENABLE); } time_t GetTimeStamp(void) //获取时间戳
{
RTC_DateTypeDef sdatestructure;
RTC_TimeTypeDef stimestructure;
struct tm tmstr;
RTC_GetDate(RTC_Format_BIN, &sdatestructure);
RTC_GetTime(RTC_Format_BIN, &stimestructure);
tmstr.tm_year = sdatestructure.RTC_Year;
tmstr.tm_mon = sdatestructure.RTC_Month;
tmstr.tm_mday = sdatestructure.RTC_Date;
tmstr.tm_hour = stimestructure.RTC_Hours;
tmstr.tm_min = stimestructure.RTC_Minutes;
tmstr.tm_sec = stimestructure.RTC_Seconds;
printf("%u-%u-%u-%u-%u-%u.\r\n", tmstr.tm_year+,tmstr.tm_mon+, tmstr.tm_mday, tmstr.tm_hour,tmstr.tm_min, tmstr.tm_sec);
return mktime(&tmstr);
}
void SetRtcTime(time_t timestamp) //设置RTC时间
{
RTC_DateTypeDef sdatestructure;
RTC_TimeTypeDef stimestructure;
struct tm *tmstr;
tmstr = localtime(&timestamp);
printf("set>>%u-%u-%u-%u-%u-%u.\r\n", tmstr->tm_year+,tmstr->tm_mon+, tmstr->tm_mday, tmstr->tm_hour,tmstr->tm_min, tmstr->tm_sec);
sdatestructure.RTC_Year = (tmstr->tm_year);
sdatestructure.RTC_Month = tmstr->tm_mon;
sdatestructure.RTC_Date = tmstr->tm_mday;
sdatestructure.RTC_WeekDay = tmstr->tm_wday;
stimestructure.RTC_Hours = tmstr->tm_hour;
stimestructure.RTC_Minutes = tmstr->tm_min;
stimestructure.RTC_Seconds = tmstr->tm_sec;
RTC_SetTime(RTC_Format_BIN,&stimestructure);
RTC_SetDate(RTC_Format_BIN, &sdatestructure); }

C语言mktime()的更多相关文章

  1. Standard C 语言标准函数库介绍

    全面巩固所知所学,往精通方向迈进! Standard C 语言标准函数库速查 (Cheat Sheet) from:http://ganquan.info/standard-c/function/ C ...

  2. C语言的时间函数

    下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数:This page is part of release 3.35 of the Linux man-pages project. ...

  3. C语言-12-日期和时间处理标准库详细解析及示例

    概述 标准库 提供了用于日期和时间处理的结构和函数 是C++语言日期和时间处理的基础 与时间相关的类型 clock_t,本质是:unsigned long typedef unsigned long ...

  4. 学了C语言,如何写个程序计算出每个月的第一个星期一对应的日期

    在前面,我们分别利用泰勒公式和C标准库中的mktime()函数推算了某个特定日期所对应的星期几,刚做完这些,就又遇到了一个与日期相关的新任务: 老板把每个月例会的时间定在了每个月的第一个星期一,他让我 ...

  5. 用C语言写个程序推算出是星期几?(用泰勒公式实现)

    在日常生活中,我们常常遇到要知道某一天是星期几的问题.有时候,我们还想知道历史上某一天是星期几.比如: “你出生的那一天是星期几啊?” “明年五一是不是星期天?我去找你玩?” 通常,解决这个问题的最简 ...

  6. 《你必须知道的495个C语言问题》知识笔记及补充

    1. extern在函数声明中是什么意思? 它能够用作一种格式上的提示表明函数的定义可能在还有一个源文件里.但在 extern int f(); 和 int f(); 之间并没有实质的差别. 补充:e ...

  7. awk程序设计语言之-awk基础

    awk程序设计语言之-awk基础 http://man.linuxde.net/ 常用工具命令之awk命令 awk是一种编程语言,用于在Linux/Unix下对文本和数据处理.数据可以来自标准输入(s ...

  8. Python学习笔记整理总结【语言基础篇】

    一.变量赋值及命名规则① 声明一个变量及赋值 #!/usr/bin/env python # -*- coding:utf-8 -*- # _author_soloLi name1="sol ...

  9. python3+django2 开发易语言网络验证(中)

    第四步:网络验证的逻辑开发 1.将model注册到adminx.py中 1.在apps/yanzheng目录下新建admin.py 文件,添加代码: import xadmin from xadmin ...

随机推荐

  1. SpringBoot_02_servlet容器配置

    二.参考资料 1.Spring boot 自定义端口 2.Spring Boot的Web配置(九):Tomcat配置和Tomcat替换

  2. 获取两个时间节点的月份列表&&每个月份的开始时间及结束时间

    //Q:从今天起之前五个月的列表 date_default_timezone_set('PRC'); $time=strtotime('-5 month'); //包含本月 $begin = strt ...

  3. jquery 怎么判断当前按钮是否是disabled 属性

    https://zhidao.baidu.com/question/1733097469792964827.html 应该用 prop("disabled") 有则返回true吧? ...

  4. 房上的猫:JavaDoc注释

    //这是一个注释 /*   *这是一个演示程序   */ /**    *@这是JavaDoc注释.   */ JavaDoc注释 背景: javadoc是Sun公司提供的一个技术,它从程序源代码中抽 ...

  5. Java Web高级编程(一)

    Servlet 一.创建Servlet类 在Java EE中,Servlet用来接收和响应终端用户的请求.Servlet是所有Web应用程序的核心类,是唯一既可以直接处理和响应用户请求,也可以将处理工 ...

  6. SqlServer Lock_Escalation

    在今天的文章里,我想谈下SQL Server里锁升级(Lock Escalations).锁升级是SQL Server使用的优化技术,用来控制在SQL Server锁管理里把持锁的数量.我们首先用SQ ...

  7. flex弹性布局语法介绍及使用

    一.语法介绍 Flex布局(弹性布局) ,一种新的布局解决方案 可简单.快速的实现网页布局 目前市面浏览器已全部支持1.指定容器为flex布局 display: flex; Webkit内核的浏览器, ...

  8. 我的Python学习笔记(三):私有变量

    一.私有变量的定义 在Python中,有以下几种方式来定义变量: xx:公有变量 _xx:单前置下划线,私有化属性或方法,类对象和子类可以访问,from somemodule import *禁止导入 ...

  9. 简单认识python cmd模块

    0X00 前言 在早前用别人的工具时,发现有些大佬会用到交互式shell,那时候就挺好奇的,但是一直都没有看一下怎么做到的. 今天在翻p牛的博客的时候,看到他早之前写的一个工具就有用到交互式shell ...

  10. Error: Can't find Python executable, you can set the PYTHON env variable.

    该错误解决方案. NodeJS安装Npm包时出现错误: npm WARN prefer global node-gyp@3.4.0 should be installed with -g > s ...