本函数适用于SMT32F10x,GD32F10x的RTC时间戳转换成有效年月日和时间;
uint8_t const Year_a[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
uint8_t const Year_b[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
uint16_t const Year_C[4]={366,365,365,365};
typedef struct //时间包
{
uint16_t year; //0--年
uint8_t month; //1--月
uint8_t day; //2--日
uint8_t hour; //3--时
uint8_t Minute; //4--分
uint8_t Second; //5--分
}TimePackge;
/********************************************
函数功能:时间戳转换成时间
*********************************************/
void LocalTime(uint32_t Second)
{
TimePackge Times;
uint8_t i;
Second += 28800; //北京时间是8:00:00
Times.Second=Second%60;
Times.Minute=Second%3600/60;
Times.hour=Second/3600%24;
Times.year=1970;
Times.month=0;
Times.day=0;
Second =Second/86400; //共有多少天,1天=86400秒
Second++; //每年是从第1天算起,所以加1天
// printf("\n日期:%d年%d天", Times.year,Second);
i=Times.year%4;
while(Second>Year_C[i]){
Second-=Year_C[i];
Times.year++;
// printf("\n日期:%d年%d天", Times.year,Second);
i=Times.year%4;
}
i=0;
if(Times.year % 4==0){ //润年
while(Second>Year_b[i]){
Second -= Year_b[i];
Times.month++;
i=Times.month%12;
}
}
else{
while(Second>Year_a[i]){
Second -= Year_a[i];
Times.month++;
i=Times.month%12;
}
}
Times.month++;
Times.day=Second;
printf("日期:%d-%d-%d %d:%d:%d", Times.year,Times.month,Times.day,Times.hour,Times.Minute,Times.Second);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
另一种算法,速度相对会快点
uint16_t Get_Time_Year(void)
{
uint16_t NewUtc = 0;
uint32_t GetTime;
RTC_WaitForSynchro();
GetTime = RTC_GetCounter();
NewUtc = GetTime/Day_S;
return NewUtc;
}
uint8_t const Year_a[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
uint8_t const Year_b[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
uint16_t const Year_C[4]={365,365,366,365};
typedef struct //时间包
{
uint16_t year; //0--年
uint8_t month; //1--月
uint8_t day; //2--日
uint8_t hour; //3--时
uint8_t Minute; //4--分
uint8_t Second; //5--分
}TimePackge;
/********************************************
函数功能:时间戳转换成时间
*********************************************/
void LocalTime(uint32_t Second)
{
TimePackge Times;
uint8_t i;
Second += 28800; //北京时间是8:00:00 60*60*8=28800秒
Times.Second=Second%60;
Times.Minute=Second%3600/60;
Times.hour=Second/3600%24;
Times.month=0;
Times.day=0;
Second =Second/86400; //共有多少天,1天=86400秒
Second++; //每年是从第1天算起,所以加1天
printf("\n从:%d年1月1日到今有%d天", Times.year,Second);
Times.year=Second/1461;//4年
Times.year =Times.year*4+1970;//
Second=Second%1461;
printf("\n还有多少:%d天",Second);
if(Second>0){
i=0;
while(Second>Year_C[i]){
Second-=Year_C[i];
Times.year++;
printf("\n日期:%d年%d天", Times.year,Second);
i++;
}
i=0;
if(Times.year % 4==0){ //润年
while(Second>Year_b[i]){
Second -= Year_b[i];
Times.month++;
i=Times.month%12;
}
}
else{
while(Second>Year_a[i]){
Second -= Year_a[i];
Times.month++;
i=Times.month%12;
}
}
Times.day=Second;
Times.month++;
}
else{
Times.year--;
Times.day=1;
Times.month=1;
}
printf("日期:%d-%d-%d %d:%d:%d", Times.year,Times.month,Times.day,Times.hour,Times.Minute,Times.Second);
}