c++周内秒转换北京时间

文章内容:

  1. 使用GPS板卡数据中的周和周内秒推算北京时间
#include 
#include 

// 将GPS周内秒转换为北京时间
void gpsSecondsToBeijingTime(int gpsWeek, int gpsSeconds)
{
    // GPS起始时间为1980年1月6日00:00:00
    const int gpsStartYear = 1980;
    const int gpsStartMonth = 1;
    const int gpsStartDay = 6;
    const int gpsStartHour = 0;
    const int gpsStartMinute = 0;
    const int gpsStartSecond = 0;

    // 计算GPS起始时间的时间戳
    struct std::tm gpsStartTime;
    gpsStartTime.tm_year = gpsStartYear - 1900;
    gpsStartTime.tm_mon = gpsStartMonth - 1;
    gpsStartTime.tm_mday = gpsStartDay;
    gpsStartTime.tm_hour = gpsStartHour;
    gpsStartTime.tm_min = gpsStartMinute;
    gpsStartTime.tm_sec = gpsStartSecond;
    std::time_t gpsStartTimestamp = std::mktime(&gpsStartTime);

    // 计算指定GPS周内秒的时间戳
    std::time_t gpsTimestamp = gpsStartTimestamp + gpsWeek * 7 * 24 * 60 * 60 + gpsSeconds;

    // 转换为北京时间
    struct std::tm *beijingTime = std::gmtime(&gpsTimestamp);

    // 输出北京时间
    std::cout << "Beijing Time: " << beijingTime->tm_year + 1900 << "-" << beijingTime->tm_mon + 1 << "-" << beijingTime->tm_mday << " "
              << beijingTime->tm_hour << ":" << beijingTime->tm_min << ":" << beijingTime->tm_sec << std::endl;
}

int main()
{
    int gpsWeek = 2099; // GPS周数
    int gpsSeconds = 345600; // GPS周内秒数

    gpsSecondsToBeijingTime(gpsWeek, gpsSeconds);

    return 0;
}

你可能感兴趣的:(c++,c++,开发语言)