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时间转任意时区时间

void UTC_to_BJtime(nmea_time*	utc_time, int8_t	timezone)
{
	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
                lastlastday = 29;
            else
                lastlastday = 28;
        }
        if(month == 8)
            lastlastday = 31;
    }
    else if(month == 4 || month == 6 || month == 9 || month == 11){
        lastday = 30;
        lastlastday = 31;
    }
    else{
        lastlastday = 31;
        if((year%400 == 0)||(year%4 == 0 && year%100 != 0))
            lastday = 29;
        else
            lastday = 28;
    }

    if(hour >= 24){					// if >24, day+1
            hour -= 24;
            day += 1; 

            if(day > lastday){ 		// next month,  day-lastday of this month
                day -= lastday;
                month += 1;

                if(month > 12){		//	next year , month-12
                    month -= 12;
                    year += 1;
                }
            }
        }
    if(hour < 0){										// if <0, day-1
            hour += 24;
            day -= 1; 
            if(day < 1){					  // month-1, day=last day of last month
                day = lastlastday;
                month -= 1;
                if(month < 1){ 			// last year , month=12
                    month = 12;
                    year -= 1;
                }
            }
        }
   // transfer value to NMEA_result.local_time
	NMEA_result.local_time.year  = year;
	NMEA_result.local_time.month = month;
	NMEA_result.local_time.date  = day;
	NMEA_result.local_time.hour  = hour;
	NMEA_result.local_time.min	 = utc_time->min;
	NMEA_result.local_time.sec	 = utc_time->sec;
}
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页