c语言utc时间转换北京时间_STM32开发 -- UTC、UNIX时间戳、北京时间之间的转换

我碰到的问题,GPS上UTC时间转北京时间和STM32上UNIX时间戳转北京时间。

这部分之前讲RTC和GPS的时候有涉及到一部分。

具体的RTC如何得到UNIX时间戳,和GNRMC如何解析得到UTC时间可以参看一下。

参看:STM32开发 – RTC详解

参看:STM32开发 – GPS模块开发详解

扩展:C语言再学习 – 时间函数

这里主要看三者转换方法:

一、UTC时间转换为北京时间

参看:UTC时间转换为北京时间

时间类型结构体

//UTC时间信息

__packed typedef struct

{

uint16_t year;//年份

uint8_t month;//月份

uint8_t date;//日期

uint8_t hour; //小时

uint8_t min; //分钟

uint8_t sec; //秒钟

}nmea_time;

UTC时间转任意时区时间

其中,北京时间 = UTC time + 8 hours

void UTC_to_BJtime(nmea_time*utc_time, int8_ttimezone)

{

int year,month,day,hour;

int lastday = 0;//last day of this month

int lastlastday = 0;//last day of last month

year = utc_time->year; //utc time

month  = utc_time->month;

day = utc_time->date;

hour = utc_time->hour + timezone;

if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){

lastday = 31;

if(month == 3){

if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year

你可能感兴趣的:(c语言utc时间转换北京时间)