北京時間へのタイムスタンプ、C言語で実装

北京時間のタイムスタンプ、C言語の実装
#include“ stdio.h”
#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int
//うるう年関数かどうかを判断する
//月12 3 4 5 6 7 8 9 10 11 12
//うるう年3129 31 30 31 30 31 31 30 31 30 31
//うるう年ではない3128 31 30 31 30 31 31 30 31 30 31
//入力:年
//出力:Is年うるう年。1、Is .0、
u8ではないIs_Leap_Year(u16 year)
{
if(year%40)// 4で割り切れる必要があります
{ if(year%100
0)
{ if(year%400
0)return 1; // 00で終わる場合は、400で割り切れる必要があります

それ以外の場合は0を返します。} else return 1;
} else return 0;
}
//クロックを設定します//
入力クロックを秒に変換します
// 1月
1、1970はベンチマークです/// 1970〜2099は法定年です
//戻り値:0、成功;その他:エラーコード
//月データテーブル
u8 const table_week [12] = {0,3,3、6 、1,4,6,2,5,0,3,5}; //月ごとに改訂されたデータテーブル
//平均年の月と日付のテーブル
constu8 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 ++)//すべての年の秒を追加{ if(Is_Leap_Year(t))seccount + = 31622400; // Leap year seconds else seccount + = 31536000; //平均年の秒数







}
smon- = 1;
for(t = 0; t <smon; t ++)//前月の秒を追加
{ seccount + =(u32)mon_table [t] * 86400; //月の秒を追加if( Is_Leap_Year(syear)&& t

1)seccount + = 86400; //うるう年の2月の1日の秒数を増やす
}
seccount + =(u32)(sday-1)86400; //前の日付の秒を追加
seccount + =(u32)hour
3600 ;
//時間と秒の秒数+ =(u32)min * 60; //分と秒の秒
数+ =秒; //最後の秒を追加

//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);









}

おすすめ

転載: blog.csdn.net/qq_40831436/article/details/96837079