GPS部分:主要进行时间格式上的转换,将utc时间转换为unix时间(以秒为单位,具有时间戳)
GPS_time.h:
#pragma once一般由编译器提供保证:同一个文件不会被包含多次。这里所说的”同一个文件”是指物理上的一个文件,而不是指内容相同的两个文件。无法对一个头文件中的一段代码作#pragma once声明,而只能针对文件。
#pragma once
//包含utils目录下的common.h文件
#include "utils/common.h"
// gps time start point
constexpr uint32_t GPS_YEAR = 1980;
constexpr uint32_t GPS_DAY_OFFSET = 5;
//头文件中对一些封装函数进行申明,具体实现在cpp文件中
bool timeString2timecount(const std::string &time_string,
double &time_count_us);
bool timecount2timeString(double time_count_us, std::string &time_string);
class GPSTime {
public:
GPSTime() = default;
GPSTime(uint32_t gps_week, double gps_second);
inline std::string GetUTCTime() { return utc_time_; }
inline std::string GetUTCTimeUs() { return utc_time_us_; }
inline double GetTotalSeconds() { return total_seconds_; }
private:
// accurate to ms for most of the data
std::string utc_time_;
// accurate to us for raw imu data
std::string utc_time_us_;
double total_seconds_;
};
GPS_time.cpp:
static_cast:主要用于数据类型之间的强制转换
UTC时间与GMT时间:
可以认为格林威治时间就是时间协调时间(GMT=UTC),格林威治时间和UTC时间均用秒数来计算的。
UTC时间格式:2016-08-9T10:01:54.123Z
字母T代表后面跟的时间,最末尾的Z表示UTC统一时间。
UTC时间与CST世界(本地世界,就是本地时间):UTC + 时区差 = 本地时间;时区差东为正,西为负。在此,把东八区时区差记为 +0800,UTC + (+0800) = 本地(北京)时间 (1);那么,UTC = 本地时间(北京时间))- 0800 (2)。
UTC时间与Unix时间戳:
在计算机中看到的UTC时间都是从(1970年01月01日 0:00:00)开始计算秒数的。所看到的UTC时间那就是从1970年这个时间点起到具体时间共有多少秒。 这个秒数就是Unix时间戳。
stoi函数_shx6666的博客-CSDN博客_stoi作用:将n进制的字符串转化为十进制的整数头文件#include <string>用法stoi(字符串, 起始位置, n进制) //将n进制字符串转化为十进制整数//将一串二进制字符串从第0位开始转化为十进制数string str = 0101010101;stoi(str, 0, 2);//十进制数可以直接转化string s = 12345;stoi(s);...https://blog.csdn.net/sunhongxuan666/article/details/120422143?ops_request_misc=&request_id=&biz_id=102&utm_term=stoi&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-120422143.142%5Ev39%5Econtrol&spm=1018.2226.3001.4187mktime()函数:用来将参数 tm 所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。返回值:返回经过的秒数
【mktime】mktime函数使用_小石头有大内涵的博客-CSDN博客_mktime函数原型time_t mktime(struct tm *)其中的 tm 结构体定义如下:struct tm {int tm_sec; /* 秒 – 取值区间为[0,59] */int tm_min; /* 分 - 取值区间为[0,59] */int tm_hour; /* 时 - 取值区间为[0,23] */int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */inthttps://blog.csdn.net/shileiwu0505/article/details/123030559?ops_request_misc=&request_id=&biz_id=102&utm_term=mktime&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-123030559.142%5Ev39%5Econtrol&spm=1018.2226.3001.4187Linux各种时间类型与时间函数提供技术文档_dianboju4932的博客-CSDN博客简介本文旨在为了解Linux各种时间类型与时间函数提供技术文档。1、Linux下常用时间类型Linux下常用时间类型有四种:time_t、struct tm、struct timeval、struct timespec1.1 time_t时间类型time_t类型在time.h中定义:[cpp] view plaincopyprint?'#ifndef __TIME_T'#...https://blog.csdn.net/dianboju4932/article/details/101572386?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165958012416781818775319%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165958012416781818775319&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-101572386-null-null.142%5Ev39%5Econtrol&utm_term=linux%E6%97%B6%E9%97%B4%E7%B1%BB%E5%9E%8B&spm=1018.2226.3001.4187
#include "time.h"
#include <string>
#include "utils/GPS_time.h"
GPSTime::GPSTime(uint32_t gps_week, double gps_seconds) {
double total_seconds = gps_week * SECOND_WEEK + gps_seconds - LEAP_SECOND +
TIME_ZONE * SECOND_HOUR + GPS_DAY_OFFSET * SECOND_DAY;
// reserve the total second
total_seconds_ = total_seconds;
uint32_t utc_year = 0;
uint32_t utc_month = 0;
uint32_t utc_day = 0;
uint32_t utc_hour = 0;
uint32_t utc_minute = 0;
uint32_t utc_second = 0;
uint32_t utc_ms = 0;
uint32_t utc_us = 0;
uint32_t year_inc = 0;
// compute year
uint32_t leap_year;
while (true) {
leap_year = static_cast<uint32_t>(is_leap_year(GPS_YEAR + year_inc));
//调用common.h中的is_leap_year()函数,判断传入的数据能否被4整除且不能被100整除||被400整除;若满足,则返回真值。
total_seconds -= MONTH_DAY[leap_year][0] * SECOND_DAY;
if (total_seconds < 0) {
utc_year = GPS_YEAR + year_inc;
total_seconds += MONTH_DAY[leap_year][0] * SECOND_DAY;
break;
}
year_inc += 1;
}
// compute month
uint32_t month_index = 1;
leap_year = static_cast<uint32_t>(is_leap_year(utc_year));
while (true) {
total_seconds -= MONTH_DAY[leap_year][month_index] * SECOND_DAY;
if (total_seconds < 0) {
utc_month = month_index;
total_seconds += MONTH_DAY[leap_year][month_index] * SECOND_DAY;
break;
}
month_index += 1;
}
// compute day
utc_day = static_cast<uint32_t>(total_seconds / SECOND_DAY) + 1;
total_seconds -= (utc_day - 1) * SECOND_DAY;
// compute hour
utc_hour = static_cast<uint32_t>(total_seconds / SECOND_HOUR);
total_seconds -= utc_hour * SECOND_HOUR;
// compute minute
utc_minute = static_cast<uint32_t>(total_seconds / 60);
total_seconds -= utc_minute * 60;
// compute second
utc_second = static_cast<uint32_t>(total_seconds);
// compute ms
utc_ms = static_cast<uint32_t>((total_seconds - utc_second) * 1000);
// compute us
utc_us = static_cast<uint32_t>((total_seconds - utc_second) * 1000000);
// format yyyy-mm-dd hh:mm:ss.ms
char buff[1024];
sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d.%03d", utc_year, utc_month,
utc_day, utc_hour, utc_minute, utc_second, utc_ms);
utc_time_ = std::string(buff);
// format yyyy-mm-dd hh:mm:ss.us
sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d.%06d", utc_year, utc_month,
utc_day, utc_hour, utc_minute, utc_second, utc_us);
utc_time_us_ = std::string(buff);
return;
}
/*
* Convert standart utc time string into time_count from unix time start
* in: time string with utc format
* out: time count from 1970-01-01 with us
* Note: different from gps time start point 1980-01-06
*/
bool timeString2timecount(const std::string &time_string,
double &time_count_us) {
if (time_string.size() < 23)
return false;
timeval tv;
struct tm stm;
if (!strptime(time_string.substr(0, 19).c_str(), "%Y-%m-%d %H:%M:%S", &stm)) {
printf("Convert %s to tm struct failed, please check your time format!\n",
time_string.substr(0, 19).c_str());
return false;
}
std::string usStr = time_string.substr(20);
int us;
if (usStr.size() == 3) {
us = stoi(usStr) * 1000; // ms to us
} else if (usStr.size() == 6) {
us = stoi(usStr);
} else {
printf("Please use millisecond or microsecond time format!\n");
return false;
}
time_t sec = mktime(&stm);
tv.tv_sec = sec;
tv.tv_usec = us;
time_count_us = static_cast<double>(tv.tv_sec * 1e6 + tv.tv_usec);
return true;
}
/*
* Convert time count(us) to standart utc format
* in: time count from 1970-01-01 with us
* out: time string with utc format
* Note: different from gps time start point 1980-01-06
*/
bool timecount2timeString(double time_count_us, std::string &time_string) {
timeval tv;
tv.tv_sec = static_cast<__time_t>(time_count_us / 1e6);
tv.tv_usec = static_cast<__suseconds_t>(time_count_us - tv.tv_sec * 1e6);
unsigned int ms = tv.tv_usec / 1000;
time_t time(tv.tv_sec);
struct tm stm;
localtime_r(&time, &stm);
char buffer[128];
strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H-%M-%S", &stm);
char res[128];
snprintf(res, sizeof(res), "%s-%03d", buffer, ms);
time_string = std::string(res);
return true;
}