最近在调试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. a:hover标签已经定义了text-decoration:none,并且生效,但是还是有下划线

    a标签在F12计算出来的样式里 text-decoration:none; 确实有被应用到.但是链接的下划线并没有被去掉... 解决办法:p:commandLink <p:commandLink ...

  2. shell日志删除(超容量&自动)

    背景:避免双十一磁盘被打爆,本想通过crontab执行,但是删除需要密码,所以用作当机器磁盘高于摸个阈值,进行无关性日志强删 #!/bin/sh #use #sh clean.sh wmporder_ ...

  3. 【原创】java NIO selector 学习笔记 一

    能力有限,仅仅是自己看源码的一些笔记. 主要介绍 可选通道 和 选择器 选择键(SelectableChannel  和 Selector SelectionKey) 选择器(Selector) 选择 ...

  4. Xamarin.Android 使用Timer 并更改UI

    http://blog.csdn.net/ozhangsan12345/article/details/72653070 第一步:创建timer对象 //创建timer对象 Timer _dispat ...

  5. C#语言和SQL Server第八章笔记

    一:                                                                                                   ...

  6. JSP的三种注释方式

    HTML注释(输出注释):指在客户端查看源代码时能看见注释.例如, <!-- this is an html comment.it will show up int the response. ...

  7. 分布式文件系统FastDFS详解

    上一篇文章<一次FastDFS并发问题的排查经历>介绍了一次生产排查并发问题的经历,可能有些人对FastDFS不是特别的了解,因此计划写几篇文章完整的介绍一下这个软件. 为什么要使用分布式 ...

  8. JDBC详解系列(四)之建立Stament和执行SQL语句

    建立Stament   在获得连接之后,我们就可以跟数据库进行交互了.   在JDBC中,我们发送SQL语句到数据库这些操作时通过Stament,Preparement,CallableStateme ...

  9. Mac 下 android/iOS https抓包

    一.Charles简介 Charles,是用Java开发的,所以跨平台,不仅可以在Mac上使用,Linux以及Window下都是可以使用的,当然需要安装JDK,才能运行,但目前是收费的. 二.下载 官 ...

  10. 【高精度乘法】NOIP2003麦森数

    题目描述 形如2^{P}-12P−1的素数称为麦森数,这时PP一定也是个素数.但反过来不一定,即如果PP是个素数,2^{P}-12P−1不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的 ...