GPS时间(周、秒) 转 UTC 时间 C++版本
#include <ctime>
#include <iostream>
#include <chrono>
#include <time.h>
int main(int argc, const char *argv[])
{
unsigned short gps_week = 2322;
double gps_seconds = 112576.3; //数值来自GPS设备
double gps_total_seconds = gps_week*7*24*60*60 + gps_seconds;
// GPS 参考时间起点: 1980-01-06 00:00:00
int gps_year = 1980;
int gps_month = 1;
int gps_day = 6;
std::tm tm{};
tm.tm_year = gps_year - 1900;//1980; //年从 1900年开始
tm.tm_mon = gps_month - 1;//1; // 月从0开始: 0~11
tm.tm_mday = gps_day;//6;
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = 0;
std::time_t t = std::mktime(&tm) + 8*60*60;
double total_seconds = static_cast<double>(t) + gps_total_seconds;
total_seconds -= 18; //计算 GPS秒对应的时间差,GPS时间与UTC时间在秒上相差18秒
std::cout<<"t:"<<t<<std::endl;
printf("T:%f\n\n\n", total_seconds);
time_t tnow = static_cast<time_t>(total_seconds);
std::tm * dt = gmtime(&tnow);
//printf ( "The current date/time is: %s", asctime(dt) );
printf("%d-%02d-%02d %02d:%02d:%02d\n", dt->tm_year+1900, \
dt->tm_mon+1, dt->tm_mday, dt->tm_hour, dt->tm_min, dt->tm_sec);
}
#include <iostream>
#include <chrono>
#include <time.h>
int main(int argc, const char *argv[])
{
unsigned short gps_week = 2322;
double gps_seconds = 112576.3; //数值来自GPS设备
double gps_total_seconds = gps_week*7*24*60*60 + gps_seconds;
// GPS 参考时间起点: 1980-01-06 00:00:00
int gps_year = 1980;
int gps_month = 1;
int gps_day = 6;
std::tm tm{};
tm.tm_year = gps_year - 1900;//1980; //年从 1900年开始
tm.tm_mon = gps_month - 1;//1; // 月从0开始: 0~11
tm.tm_mday = gps_day;//6;
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = 0;
std::time_t t = std::mktime(&tm) + 8*60*60;
double total_seconds = static_cast<double>(t) + gps_total_seconds;
total_seconds -= 18; //计算 GPS秒对应的时间差,GPS时间与UTC时间在秒上相差18秒
std::cout<<"t:"<<t<<std::endl;
printf("T:%f\n\n\n", total_seconds);
time_t tnow = static_cast<time_t>(total_seconds);
std::tm * dt = gmtime(&tnow);
//printf ( "The current date/time is: %s", asctime(dt) );
printf("%d-%02d-%02d %02d:%02d:%02d\n", dt->tm_year+1900, \
dt->tm_mon+1, dt->tm_mday, dt->tm_hour, dt->tm_min, dt->tm_sec);
double utc_time = 0.0; // GPS UTC time: hhmmss.ss
double ms = gps_seconds - static_cast<int64_t>(gps_seconds)*1.0;
utc_time = dt->tm_hour*10000.0 + dt->tm_min*100.0 + dt->tm_sec + ms;
}
评论已关闭