⽤C语⾔打造时间戳转换成北京时间格式本函数适⽤于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.day=0;
Second =Second/86400; //共有多少天,1天=86400秒
Second++; //每年是从第1天算起,所以加1天
// printf("\n⽇期:%d年%d天", ar,Second);
ar%4;
while(Second>Year_C[i]){
Second-=Year_C[i];
// printf("\n⽇期:%d年%d天", ar,Second);
ar%4;
}
i=0;
ar % 4==0){ //润年
while(Second>Year_b[i]){
Second -= Year_b[i];
h%12;
}
}
else{
while(Second>Year_a[i]){
Second -= Year_a[i];
h%12;
}
}
Times.day=Second;
printf("⽇期:%d-%d-%d %d:%d:%d", h,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.day=0;
Second =Second/86400; //共有多少天,1天=86400秒
Second++; //每年是从第1天算起,所以加1天
printf("\n从:%d年1⽉1⽇到今有%d天", ar,Second);
Second=Second%1461;
printf("\n还有多少:%d天",Second);
if(Second>0){
i=0;
while(Second>Year_C[i]){
Second-=Year_C[i];
printf("\n⽇期:%d年%d天", ar,Second);
i++;
}
i=0;
ar % 4==0){ //润年
while(Second>Year_b[i]){
Second -= Year_b[i];
h%12;
}
}
else{
while(Second>Year_a[i]){
Second -= Year_a[i];
h%12;
}
}
Times.day=Second;
}
else{
Times.day=1;
}
unix时间戳转换日期格式printf("⽇期:%d-%d-%d %d:%d:%d", h,Times.day,Times.hour,Times.Minute,Times.Second); }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论