0.4.1
Loading...
Searching...
No Matches
TimeBase.cpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#include "TimeBase.hpp"
10
11#include <chrono>
12#include <ctime>
13
14#include "util/Logger.hpp"
15
17{
18namespace
19{
21NAV::InsTime currentTime;
22
23std::chrono::steady_clock::time_point currentTimeComputer;
24
25} // namespace
26} // namespace NAV::util::time
27
29{
30 return timeMode;
31}
32
34{
35 timeMode = mode;
36}
37
39{
40 if (timeMode == Mode::POST_PROCESSING || currentTime.empty())
41 {
42 return currentTime;
43 }
44 // (timeMode == Mode::REAL_TIME)
45 auto elapsed = std::chrono::steady_clock::now() - currentTimeComputer;
46 return currentTime + elapsed;
47}
48
50{
51 if (auto currentExactTime = GetCurrentInsTime();
52 insTime < currentExactTime)
53 {
54 LOG_DATA("Not updating current Time [{} {:.6f}] to [{} {:.6f}], because the new time is earlier.",
55 currentExactTime.toGPSweekTow().gpsWeek, currentExactTime.toGPSweekTow().tow,
56 insTime.toGPSweekTow().gpsWeek, insTime.toGPSweekTow().tow);
57 }
58 else if (insTime >= currentExactTime)
59 {
60 if (timeMode == Mode::REAL_TIME)
61 {
62 LOG_INFO("Updating current Time [{}] to [{} ]", currentExactTime, insTime);
63 }
64 currentTimeComputer = std::chrono::steady_clock::now();
65 currentTime = insTime;
66 LOG_DATA("Updating current Time [{}] to [{} ]", currentExactTime, insTime);
67 }
68}
69
71{
72 std::time_t t = std::time(nullptr);
73 std::tm* now = std::gmtime(&t); // NOLINT(concurrency-mt-unsafe)
74
75 currentTimeComputer = std::chrono::steady_clock::now();
76 currentTime = InsTime{ static_cast<uint16_t>(now->tm_year + 1900), static_cast<uint16_t>(now->tm_mon) + 1, static_cast<uint16_t>(now->tm_mday),
77 static_cast<uint16_t>(now->tm_hour), static_cast<uint16_t>(now->tm_min), static_cast<long double>(now->tm_sec), UTC };
78}
79
81{
82 currentTime.reset();
83}
Utility class for logging to console and file.
#define LOG_DATA
All output which occurs repeatedly every time observations are received.
Definition Logger.hpp:29
#define LOG_INFO
Info to the user on the state of the program.
Definition Logger.hpp:69
Keeps track of the current real/simulation time.
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
constexpr InsTime_GPSweekTow toGPSweekTow(TimeSystem timesys=GPST) const
Converts this time object into a different format.
Definition InsTime.hpp:854
void reset()
Resets the InsTime object.
Definition InsTime.hpp:1095
Mode
Different Modes the Time Base class can work in.
Definition TimeBase.hpp:22
@ POST_PROCESSING
Time will be set by FlowExecutor only.
Definition TimeBase.hpp:24
@ REAL_TIME
Computer clock will be added to last time update.
Definition TimeBase.hpp:23
void ClearCurrentTime()
Clears the current time object.
Definition TimeBase.cpp:80
void SetCurrentTime(const InsTime &insTime)
Set the current time object.
Definition TimeBase.cpp:49
void SetMode(Mode mode)
Set the time mode.
Definition TimeBase.cpp:33
InsTime GetCurrentInsTime()
Get the current time.
Definition TimeBase.cpp:38
void SetCurrentTimeToComputerTime()
Set the current time object to the computer time.
Definition TimeBase.cpp:70
Mode GetMode()
Get the time mode.
Definition TimeBase.cpp:28
@ UTC
Coordinated Universal Time.
int32_t gpsWeek
Contains GPS week in GPS standard time [GPST].
Definition InsTime.hpp:371
long double tow
Contains GPS time of week in seconds in GPS standard time [GPST].
Definition InsTime.hpp:372