FX1N/FX1S1N2N学习板底层源码/main/RTC_Time.c

178 lines
6.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*******************************************************************************
* 本文件实现基于RTC的日期功能提供年月日的读写。基于ANSI-C的time.h
*
* 作者jjldc (九九)
* QQ: 77058617
*
* RTC中保存的时间格式是UNIX时间戳格式的。即一个32bit的time_t变量实为u32
*
* ANSI-C的标准库中提供了两种表示时间的数据 型:
* time_t: UNIX时间戳从1970-1-1起到某时间经过的秒数
* typedef unsigned int time_t;
*
* struct tm: Calendar格式年月日形式
* tm结构如下
* struct tm {
* int tm_sec; // 秒 seconds after the minute, 0 to 60
* (0 - 60 allows for the occasional leap second)
* int tm_min; // 分 minutes after the hour, 0 to 59
* int tm_hour; // 时 hours since midnight, 0 to 23
* int tm_mday; // 日 day of the month, 1 to 31
* int tm_mon; // 月 months since January, 0 to 11
* int tm_year; // 年 years since 1900
* int tm_wday; // 星期 days since Sunday, 0 to 6
* int tm_yday; // 从元旦起的天数 days since January 1, 0 to 365
* int tm_isdst; // 夏令时Daylight Savings Time flag
* ...
* }
* 其中wdayyday可以自动产生软件直接读取
* mon的取值为0-11
* ***注意***
* tm_year:在time.h库中定义为1900年起的年份即2008年应表示为2008-1900=108
* 这种表示方法对用户来说不是十分友好,与现实有较大差异。
* 所以在本文件中,屏蔽了这种差异。
* 即外部调用本文件的函数时tm结构体类型的日期tm_year即为2008
* 注意若要调用系统库time.c中的函数需要自行将tm_year-=1900
*
* 成员函数说明:
* struct tm Time_ConvUnixToCalendar(time_t t);
* 输入一个Unix时间戳time_t返回Calendar格式日期
* time_t Time_ConvCalendarToUnix(struct tm t);
* 输入一个Calendar格式日期返回Unix时间戳time_t
* time_t Time_GetUnixTime(void);
* 从RTC取当前时间的Unix时间戳值
* struct tm Time_GetCalendarTime(void);
* 从RTC取当前时间的日历时间
* void Time_SetUnixTime(time_t);
* 输入UNIX时间戳格式时间设置为当前RTC时间
* void Time_SetCalendarTime(struct tm t);
* 输入Calendar格式时间设置为当前RTC时间
*
* 外部调用实例:
* 定义一个Calendar格式的日期变量
* struct tm now;
* now.tm_year = 2008;
* now.tm_mon = 11; //12月
* now.tm_mday = 20;
* now.tm_hour = 20;
* now.tm_min = 12;
* now.tm_sec = 30;
*
* 获取当前日期时间:
* tm_now = Time_GetCalendarTime();
* 然后可以直接读tm_now.tm_wday获取星期数
*
* 设置时间:
* Step1. tm_now.xxx = xxxxxxxxx;
* Step2. Time_SetCalendarTime(tm_now);
*
* 计算两个时间的差
* struct tm t1,t2;
* t1_t = Time_ConvCalendarToUnix(t1);
* t2_t = Time_ConvCalendarToUnix(t2);
* dt = t1_t - t2_t;
* dt就是两个时间差的秒数
* dt_tm = mktime(dt); //注意dt的年份匹配ansi库中函数为相对年份注意超限
* 另可以参考相关资料调用ansi-c库的格式化输出等功能ctimestrftime等
*
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "RTC_Time.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Time_Set(u32 t);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : Time_ConvUnixToCalendar(time_t t)
* Description : 转换UNIX时间戳为日历时间
* Input : u32 t 当前时间的UNIX时间戳
* Output : None
* Return : struct tm
*******************************************************************************/
struct tm Time_ConvUnixToCalendar(time_t t)
{
struct tm *t_tm;
t_tm = localtime(&t);
t_tm->tm_year += 1900; //localtime转换结果的tm_year是相对值需要转成绝对值
return *t_tm;
}
/*******************************************************************************
* Function Name : Time_ConvCalendarToUnix(struct tm t)
* Description : 写入RTC时钟当前时间
* Input : struct tm t
* Output : None
* Return : time_t
*******************************************************************************/
time_t Time_ConvCalendarToUnix(struct tm t)
{
t.tm_year -= 1900; //外部tm结构体存储的年份为2008格式
//而time.h中定义的年份格式为1900年开始的年份
//所以,在日期转换时要考虑到这个因素。
return mktime(&t);
}
/*******************************************************************************
* Function Name : Time_GetUnixTime()
* Description : 从RTC取当前时间的Unix时间戳值
* Input : None
* Output : None
* Return : time_t t
*******************************************************************************/
time_t Time_GetUnixTime(void)
{
return (time_t)RTC_GetCounter();
}
/*******************************************************************************
* Function Name : Time_GetCalendarTime()
* Description : 从RTC取当前时间的日历时间struct tm
* Input : None
* Output : None
* Return : time_t t
*******************************************************************************/
struct tm Time_GetCalendarTime(void)
{
time_t t_t;
struct tm t_tm;
t_t = (time_t)RTC_GetCounter();
t_tm = Time_ConvUnixToCalendar(t_t);
return t_tm;
}
/*******************************************************************************
* Function Name : Time_SetUnixTime()
* Description : 将给定的Unix时间戳写入RTC
* Input : time_t t
* Output : None
* Return : None
*******************************************************************************/
void Time_SetUnixTime(time_t t)
{
RTC_WaitForLastTask();
RTC_SetCounter((u32)t);
RTC_WaitForLastTask();
return;
}
/*******************************************************************************
* Function Name : Time_SetCalendarTime()
* Description : 将给定的Calendar格式时间转换成UNIX时间戳写入RTC
* Input : struct tm t
* Output : None
* Return : None
*******************************************************************************/
void Time_SetCalendarTime(struct tm t)
{
Time_SetUnixTime(Time_ConvCalendarToUnix(t));
return;
}