GPS系统时间与UTC时间不同,差了一个闰秒,因为UTC时间是可以调整的,而GPS时间是连续的,闰秒数在下行的导航电文中有反应。
北京时=GPS时+8小时+闰秒
GPGGA和GPRMC中本身已经将GPS时间转换为UTC时间了,所以该时间与北京时间只差8小时。
例如:GPS时间显示为2月27日0时,北京时为2月27日8时。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct UTCtime
{int year;int month;int day;int hour;int min;int sec;
}UTC_TIME;UTC_TIME utctime;//UTC时间转换为北京时间
void UTCToBeijing(unsigned int year,unsigned char month,unsigned char day,unsigned int hour,const unsigned char minute,const unsigned char second)
{hour += 8;if(1==month||3==month||5==month||7==month||8==month||10==month||12==month)//1,3,5,7,8,9,12月每月为31天{if(24 <= hour){hour -= 24;day += 1;//如果超过24小时,减去24小时,后再加上一天if(day > 31){day -= 31;month += 1;}//如果超过31一天,减去31天,后加上一个月}}else if(4==month||6==month||9==month||11==month)//4,6,9,11月每月为30天{if(24 <= hour){hour -= 24;day += 1;//如果超过24小时,减去24小时,后再加上一天if(30 < day){day -= 30;month += 1;}//如果超过30一天,减去30天,后加上一个月}}else//剩下为2月,闰年为29天,平年为28天{if(24 <= hour){hour -= 24;day += 1;if((0 == year%400)||(0 == year%4 && 0 != year%100))//判断是否为闰年,年号能被400整除或年号能被4整除,而不能被100整除为闰年{if(29 < day){day -= 29;month += 1;}}//为闰年else{if(28 < day){day -= 28;month += 1;}}//为平年}}if(12 < month){month-=12;year+=1;}ar=h=month;utctime.day=day;utctime.hour=hour;utctime.min=minute;utctime.sec=second;
}int main()
{/*测试GPS转换时间*/ar=h=2;utctime.day=28;utctime.hour=19;utctime.min=25;utctime.sec=25;printf("转换前时间:%d.%d.%d %d:%d:%dn",h,utctime.day,utctime.hour,utctime.min,utctime.sec);h,utctime.day,utctime.hour,utctime.min,utctime.sec);printf("转换后时间:%d.%d.%d %d:%d:%dn",h,utctime.day,utctime.hour,utctime.min,utctime.sec);
}
运行结果:
本文发布于:2024-01-28 23:52:48,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170645717311205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |