2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-23 18:07:00 +00:00
|
|
|
//
|
2023-12-16 18:48:25 +00:00
|
|
|
// ES-DE
|
2020-06-23 18:07:00 +00:00
|
|
|
// TimeUtil.h
|
|
|
|
//
|
|
|
|
// Low-level date and time functions.
|
|
|
|
// Set and get time, format to string formats, count days and months etc.
|
|
|
|
//
|
|
|
|
|
2017-11-22 21:01:12 +00:00
|
|
|
#ifndef ES_CORE_UTILS_TIME_UTIL_H
|
|
|
|
#define ES_CORE_UTILS_TIME_UTIL_H
|
|
|
|
|
2020-11-26 17:53:00 +00:00
|
|
|
#include <ctime>
|
2017-11-22 21:01:12 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace Utils
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
namespace Time
|
|
|
|
{
|
|
|
|
class DateTime
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DateTime();
|
2021-05-30 18:46:17 +00:00
|
|
|
DateTime(const time_t& time);
|
|
|
|
DateTime(const tm& timeStruct);
|
|
|
|
DateTime(const std::string& isoString);
|
2021-07-07 18:31:46 +00:00
|
|
|
~DateTime() {}
|
2020-06-23 18:07:00 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
const bool operator<(const DateTime& other) const { return (mTime < other.mTime); }
|
|
|
|
const bool operator<=(const DateTime& other) const { return (mTime <= other.mTime); }
|
|
|
|
const bool operator>(const DateTime& other) const { return (mTime > other.mTime); }
|
|
|
|
const bool operator>=(const DateTime& other) const { return (mTime >= other.mTime); }
|
2020-06-23 18:07:00 +00:00
|
|
|
operator time_t() const { return mTime; }
|
|
|
|
operator tm() const { return mTimeStruct; }
|
|
|
|
operator std::string() const { return mIsoString; }
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
void setTime(const time_t& time);
|
2020-06-23 18:07:00 +00:00
|
|
|
const time_t& getTime() const { return mTime; }
|
2021-05-30 18:46:17 +00:00
|
|
|
void setTimeStruct(const tm& timeStruct);
|
2020-06-23 18:07:00 +00:00
|
|
|
const tm& getTimeStruct() const { return mTimeStruct; }
|
2021-07-07 18:31:46 +00:00
|
|
|
void setIsoString(const std::string& isoString);
|
|
|
|
const std::string& getIsoString() const { return mIsoString; }
|
2020-06-23 18:07:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
time_t mTime;
|
|
|
|
tm mTimeStruct;
|
|
|
|
std::string mIsoString;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Duration
|
|
|
|
{
|
|
|
|
public:
|
2021-05-30 18:46:17 +00:00
|
|
|
Duration(const time_t& time);
|
2021-07-07 18:31:46 +00:00
|
|
|
~Duration() {}
|
2020-06-23 18:07:00 +00:00
|
|
|
|
|
|
|
unsigned int getDays() const { return mDays; }
|
|
|
|
unsigned int getHours() const { return mHours; }
|
|
|
|
unsigned int getMinutes() const { return mMinutes; }
|
|
|
|
unsigned int getSeconds() const { return mSeconds; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int mTotalSeconds;
|
|
|
|
unsigned int mDays;
|
|
|
|
unsigned int mHours;
|
|
|
|
unsigned int mMinutes;
|
|
|
|
unsigned int mSeconds;
|
|
|
|
};
|
|
|
|
|
|
|
|
time_t now();
|
2021-05-30 18:46:17 +00:00
|
|
|
time_t stringToTime(const std::string& string, const std::string& format = "%Y%m%dT%H%M%S");
|
|
|
|
std::string timeToString(const time_t& time, const std::string& format = "%Y%m%dT%H%M%S");
|
|
|
|
int daysInMonth(const int year, const int month);
|
|
|
|
int daysInYear(const int year);
|
2022-01-11 20:57:00 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
} // namespace Time
|
|
|
|
|
|
|
|
} // namespace Utils
|
2017-11-22 21:01:12 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_UTILS_TIME_UTIL_H
|