1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2025-10-30 14:53:03 +00:00

Get time zone offset in a more standard way (#17)

* Clang Tidy fixes

* Get time zone offset in a more standard way

Account for leap seconds
Use GNSS-SDR.osnma_mode=strict to check for local time

* Fix for C++20

* Initialize tm in a more portable way

* Remove unnecessary data members in osnma_msg_receiver

---------

Co-authored-by: cesaaargm <cesare.martinez@proton.me>
This commit is contained in:
Carles Fernandez
2024-08-03 19:58:34 +02:00
committed by GitHub
parent 6beb92278f
commit 3457b8ed3b
5 changed files with 110 additions and 73 deletions

View File

@@ -16,15 +16,26 @@
#include "osnma_helper.h"
#include <bitset>
#include <chrono>
#include <iomanip>
#include <ios>
#include <sstream>
#include <ctime> // timezone
uint32_t Osnma_Helper::compute_gst(uint32_t WN, uint32_t TOW) const{
Osnma_Helper::Osnma_Helper()
{
GST_START_EPOCH.tm_mday = 22;
GST_START_EPOCH.tm_mon = 7; // August (0-based)
GST_START_EPOCH.tm_year = 1999 - 1900;
}
uint32_t Osnma_Helper::compute_gst(uint32_t WN, uint32_t TOW) const
{
return (WN & 0x00000FFF) << 20 | (TOW & 0x000FFFFF);
}
uint32_t Osnma_Helper::compute_gst(tm& input)
{
auto epoch_time_point = std::chrono::system_clock::from_time_t(mktime(&GST_START_EPOCH));
@@ -34,23 +45,28 @@ uint32_t Osnma_Helper::compute_gst(tm& input)
auto duration_sec = std::chrono::duration_cast<std::chrono::seconds>(input_time_point - epoch_time_point);
// Calculate the week number (WN) and time of week (TOW)
uint32_t sec_in_week = 7 * 24 * 60 * 60;
uint32_t week_number = duration_sec.count() / sec_in_week;
uint32_t time_of_week = duration_sec.count() % sec_in_week;
const uint32_t sec_in_week = 604800;
const uint32_t week_number = duration_sec.count() / sec_in_week;
const uint32_t time_of_week = duration_sec.count() % sec_in_week;
return compute_gst(week_number, time_of_week);
}
uint32_t Osnma_Helper::compute_gst_now()
{
std::chrono::time_point epoch_time_point = std::chrono::system_clock::from_time_t(mktime(&GST_START_EPOCH) - timezone);
// auto time_utc = std::chrono::time_point_cast<std::chrono::seconds>(time).time_since_epoch();
time_t now = time(nullptr);
struct tm local_tm = *std::localtime(&now);
struct tm utc_tm = *std::gmtime(&now);
auto timezone_offset = std::mktime(&utc_tm) - std::mktime(&local_tm);
auto epoch_time_point = std::chrono::system_clock::from_time_t(std::mktime(&GST_START_EPOCH) - timezone_offset) + std::chrono::seconds(13);
auto duration_sec = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - epoch_time_point);
uint32_t sec_in_week = 7 * 24 * 60 * 60;
uint32_t week_number = duration_sec.count() / sec_in_week;
uint32_t time_of_week = duration_sec.count() % sec_in_week;
const uint32_t sec_in_week = 604800;
const uint32_t week_number = duration_sec.count() / sec_in_week;
const uint32_t time_of_week = duration_sec.count() % sec_in_week;
return compute_gst(week_number, time_of_week);
}
std::vector<uint8_t> Osnma_Helper::gst_to_uint8(uint32_t GST) const
{
std::vector<uint8_t> res;
@@ -145,12 +161,15 @@ std::vector<uint8_t> Osnma_Helper::convert_from_hex_string(const std::string& he
return result;
}
uint32_t Osnma_Helper::get_WN(uint32_t GST)
uint32_t Osnma_Helper::get_WN(uint32_t GST) const
{
return (GST & 0xFFF00000) >> 20;
}
uint32_t Osnma_Helper::get_TOW(uint32_t GST)
uint32_t Osnma_Helper::get_TOW(uint32_t GST) const
{
return GST & 0x000FFFFF;
}

View File

@@ -18,27 +18,34 @@
#define GNSS_SDR_OSNMA_HELPER_H
#include <chrono>
#include <cstdint>
#include <ctime>
#include <string>
#include <vector>
/** \addtogroup Core
* \{ */
/** \addtogroup System_Parameters
* \{ */
class Osnma_Helper
{
public:
Osnma_Helper() = default;
Osnma_Helper();
~Osnma_Helper() = default;
uint32_t compute_gst(uint32_t WN, uint32_t TOW) const;
uint32_t compute_gst(std::tm& input);
uint32_t compute_gst_now();
uint32_t get_WN(uint32_t GST);
uint32_t get_TOW(uint32_t GST);
uint32_t get_WN(uint32_t GST) const;
uint32_t get_TOW(uint32_t GST) const;
std::vector<uint8_t> gst_to_uint8(uint32_t GST) const;
std::vector<uint8_t> bytes(const std::string& binaryString) const;
std::string verification_status_str(int status) const;
std::string convert_to_hex_string(const std::vector<uint8_t>& vector) const;
std::vector<uint8_t> convert_from_hex_string(const std::string& hex_string) const; // TODO remove similar function in gnss_crypto
std::tm GST_START_EPOCH = {0, 0, 0, 22, 8 - 1, 1999 - 1900, 0, 0, 0, 0, 0};
std::tm GST_START_EPOCH{};
};
/** \} */
/** \} */
#endif // GNSS_SDR_OSNMA_HELPER_H