Time stamp to Beijing time, implemented in C language

Time stamp to Beijing time, C language implementation
#include “stdio.h”
#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int
//Determine whether it is a leap year function
//Month 1 2 3 4 5 6 7 8 9 10 11 12
//Leap year 31 29 31 30 31 30 31 31 30 31 30 31
//Non-leap year 31 28 31 30 31 30 31 31 30 31 30 31
//Input: Year
//Output: Is the year a leap year. 1 , Is .0, not
u8 Is_Leap_Year(u16 year)
{
if(year%40) //Must be divisible by 4
{ if(year%100
0)
{
if(year%4000)return 1;//If it ends in 00, it must be divisible by 400
else return 0;
}else return 1;
}else return 0;
}
//Set the clock
//Convert the input clock to seconds
// January 1,
1970 is the benchmark ///1970~2099 is a legal year
// Return value: 0, success; others: error code.
//Month data table
u8 const table_week[12]={0,3,3, 6,1,4,6,2,5,0,3,5}; //Monthly revised data table
//Month and date table of the average year
const u8 mon_table[12]={31,28,31,30,31, 30,31,31,30,31,30,31};
u32 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
{ u16 t; u32 seccount=0; if(syear<1970 ||syear>2099)return 1; for(t=1970;t<syear;t++) //Add the seconds of all years { if(Is_Leap_Year(t))seccount+=31622400;//Leap year seconds else seccount+=31536000; //The number of seconds in the average year







}
smon-=1;
for(t=0;t<smon;t++) //Add the seconds of the previous month
{ seccount+=(u32)mon_table[t]*86400;//Add the seconds of the month if(Is_Leap_Year(syear)&&t

1)seccount+=86400;//Increase the number of seconds of a day in February of leap year
}
seccount+=(u32)(sday-1) 86400;//Add the seconds of the previous date
seccount+=(u32)hour
3600;/ /Hours and seconds
seccount+=(u32)min*60; //minutes and seconds
seccount+=sec;//add the last second

//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);	//使能PWR和BKP外设时钟  
//PWR_BackupAccessCmd(ENABLE);	//使能RTC和后备寄存器访问 
//RTC_SetCounter(seccount);	//设置RTC计数器的值

//RTC_WaitForLastTask();	//等待最近一次对RTC寄存器的写操作完成  
seccount-=28800;
return seccount;	    

}
void main()
{
u32 Time_count;
u8 timeg_H,timeg_L,timed_h,timed_l;
Time_count=RTC_Set(2019,7,22,9,32,55);
printf(“时间戳为=%ld\n”,Time_count);
timeg_H=(u8)(Time_count>>24);
timeg_L=(u8)( (Time_count>>16)&0x00ff );
timed_h=(u8) ( (Time_count>>8)&0x0000ff);
timed_l=(u8) (Time_count&0x000000ff);
printf(“timeg_H=%x ,timeg_L=%x,timed_h=%x,timed_l=%x,\n”,timeg_H,timeg_L,timed_h,timed_l);
printf(“timeg_H=%d ,timeg_L=%d,timed_h=%d,timed_l=%d,\n”,timeg_H,timeg_L,timed_h,timed_l);

}

Guess you like

Origin blog.csdn.net/qq_40831436/article/details/96837079
Recommended