add the helper for the UTC time and mach checks
This commit is contained in:
53
include/aman/helper/Math.h
Normal file
53
include/aman/helper/Math.h
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* @brief Defines and implements functions to get mathematical helper functions
|
||||||
|
* @file helper/Math.h
|
||||||
|
* @author Sven Czarnian <devel@svcz.de>
|
||||||
|
* @copyright Copyright 2020-2021 Sven Czarnian
|
||||||
|
* @license This project is published under the GNU General Public License v3 (GPLv3)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define _USE_MATH_DEFINES
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
namespace aman {
|
||||||
|
/**
|
||||||
|
* @brief Contains helper functions to get numerical stability in the functions
|
||||||
|
* @ingroup helper
|
||||||
|
*/
|
||||||
|
class Math {
|
||||||
|
public:
|
||||||
|
Math(const Math& other) = delete;
|
||||||
|
Math(Math&& other) = delete;
|
||||||
|
Math& operator=(const Math& other) = delete;
|
||||||
|
Math& operator=(Math&& other) = delete;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Compares two floating point numbers if they are almost equal
|
||||||
|
* @param[in] value0 The first floating point number
|
||||||
|
* @param[in] value1 The second floating point number
|
||||||
|
* @param[in] threshold The threshold which defines both numbers as almost equal
|
||||||
|
* @return True if the difference between value0 and value1 is smaller than the threshold, else false
|
||||||
|
*/
|
||||||
|
static __inline bool almostEqual(float value0, float value1, float threshold = 1e-4) noexcept {
|
||||||
|
return std::abs(value0 - value1) <= threshold;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @brief Converts a degree value into a radian value
|
||||||
|
* @param[in] degree The degree value
|
||||||
|
* @return The radian value
|
||||||
|
*/
|
||||||
|
static constexpr __inline float deg2rad(float degree) noexcept {
|
||||||
|
return degree * static_cast<float>(M_PI) / 180.0f;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @brief Converts a radian value into a degree value
|
||||||
|
* @param[in] radian The degree value
|
||||||
|
* @return The degree value
|
||||||
|
*/
|
||||||
|
static constexpr __inline float rad2deg(float radian) noexcept {
|
||||||
|
return radian * 180.0f / static_cast<float>(M_PI);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
70
include/aman/helper/UtcTime.h
Normal file
70
include/aman/helper/UtcTime.h
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* @brief Defines and implements functions to handle timestamps
|
||||||
|
* @file helper/UtcTime.h
|
||||||
|
* @author Sven Czarnian <devel@svcz.de>
|
||||||
|
* @copyright Copyright 2020-2021 Sven Czarnian
|
||||||
|
* @license This project is published under the GNU General Public License v3 (GPLv3)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
|
#include <chrono>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
namespace aman {
|
||||||
|
/**
|
||||||
|
* @brief Defines helper functions to handle system timestamps and UTC timestamps
|
||||||
|
* @ingroup helper
|
||||||
|
*/
|
||||||
|
class UtcTime {
|
||||||
|
public:
|
||||||
|
using Point = std::chrono::utc_clock::time_point;
|
||||||
|
|
||||||
|
UtcTime() = delete;
|
||||||
|
UtcTime(const UtcTime&) = delete;
|
||||||
|
UtcTime(UtcTime&&) = delete;
|
||||||
|
UtcTime& operator=(const UtcTime&) = delete;
|
||||||
|
UtcTime& operator=(UtcTime&&) = delete;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns the current time in UTC
|
||||||
|
* @return The current UTC time
|
||||||
|
*/
|
||||||
|
static __inline Point currentUtc() {
|
||||||
|
return std::chrono::utc_clock::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Converts a string into a std::chrono::system_clock::time_point in UTC
|
||||||
|
* @param[in] date The string containing the timepoint
|
||||||
|
* @return The converted time
|
||||||
|
*/
|
||||||
|
static __inline Point stringToTime(const std::string& date) {
|
||||||
|
Point retval;
|
||||||
|
std::stringstream stream(date);
|
||||||
|
std::chrono::from_stream(stream, "%Y-%m-%d %X", retval);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Converts a time point into a string
|
||||||
|
* The function converts into the following format:
|
||||||
|
* - %y%m%d%H%M
|
||||||
|
* @param[in] time The timepoint which needs to be converted in UTC
|
||||||
|
* @return The converted time
|
||||||
|
*/
|
||||||
|
static __inline std::string timeToString(const Point& time, const std::string& format = "%Y-%m-%d %X") {
|
||||||
|
if ((std::chrono::time_point<std::chrono::utc_clock>::max)() != time) {
|
||||||
|
std::stringstream stream;
|
||||||
|
stream << std::format("{0:" + format + "}", time);
|
||||||
|
return stream.str();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user