| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 16 | namespace NAV::util::time | ||
| 17 | { | ||
| 18 | namespace | ||
| 19 | { | ||
| 20 | NAV::util::time::Mode timeMode = NAV::util::time::Mode::REAL_TIME; | ||
| 21 | NAV::InsTime currentTime; | ||
| 22 | |||
| 23 | std::chrono::steady_clock::time_point currentTimeComputer; | ||
| 24 | |||
| 25 | } // namespace | ||
| 26 | } // namespace NAV::util::time | ||
| 27 | |||
| 28 | ✗ | NAV::util::time::Mode NAV::util::time::GetMode() | |
| 29 | { | ||
| 30 | ✗ | return timeMode; | |
| 31 | } | ||
| 32 | |||
| 33 | 113 | void NAV::util::time::SetMode(NAV::util::time::Mode mode) | |
| 34 | { | ||
| 35 | 113 | timeMode = mode; | |
| 36 | 113 | } | |
| 37 | |||
| 38 | ✗ | NAV::InsTime NAV::util::time::GetCurrentInsTime() | |
| 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 | |||
| 49 | ✗ | void NAV::util::time::SetCurrentTime(const NAV::InsTime& insTime) | |
| 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 | |||
| 70 | 114 | void NAV::util::time::SetCurrentTimeToComputerTime() | |
| 71 | { | ||
| 72 | 114 | std::time_t t = std::time(nullptr); | |
| 73 | 114 | std::tm* now = std::gmtime(&t); // NOLINT(concurrency-mt-unsafe) | |
| 74 | |||
| 75 | 114 | currentTimeComputer = std::chrono::steady_clock::now(); | |
| 76 |
1/2✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
|
114 | 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 | 114 | static_cast<uint16_t>(now->tm_hour), static_cast<uint16_t>(now->tm_min), static_cast<long double>(now->tm_sec), UTC }; | |
| 78 | 114 | } | |
| 79 | |||
| 80 | ✗ | void NAV::util::time::ClearCurrentTime() | |
| 81 | { | ||
| 82 | ✗ | currentTime.reset(); | |
| 83 | ✗ | } | |
| 84 |