generated from ut-issl/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #548 from ut-issl/feature/add-time-system-library
Update time system library
- Loading branch information
Showing
10 changed files
with
425 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @file epoch_time.cpp | ||
* @brief A class to handle time like UNIX time with fractions | ||
*/ | ||
|
||
#include "epoch_time.hpp" | ||
|
||
#include <cmath> | ||
|
||
EpochTime::EpochTime(const DateTime date_time) { | ||
// No leap second calculation | ||
const int doy[] = {1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; //!< Day of Year for the 1st day of each month | ||
// Parse Calender | ||
const size_t year = date_time.GetYear(); | ||
const size_t month = date_time.GetMonth(); | ||
const size_t day = date_time.GetDay(); | ||
const size_t hour = date_time.GetHour(); | ||
const size_t minute = date_time.GetMinute(); | ||
const size_t second = (size_t)std::floor(date_time.GetSecond()); | ||
|
||
// TODO: assertion | ||
if (year < 1970 || month < 1 || 12 < month) return; | ||
if (day < 1 || 32 <= day) return; | ||
if (60 <= minute) return; | ||
if (60 <= second) return; | ||
|
||
// leap year if year%4==0 in 1901-2099 | ||
uint64_t days = (year - 1970) * 365 + (year - 1969) / 4 + doy[month - 1] + day - 2 + (year % 4 == 0 && month >= 3 ? 1 : 0); | ||
time_s_ = (uint64_t)days * 86400 + hour * 3600 + minute * 60 + second; | ||
fraction_s_ = date_time.GetSecond() - (double)second; | ||
} | ||
|
||
bool EpochTime::operator==(const EpochTime& target) const { | ||
if (this->time_s_ != target.time_s_) return false; | ||
if (this->fraction_s_ != target.fraction_s_) return false; // TODO: comparison of double | ||
return true; | ||
} | ||
|
||
bool EpochTime::operator>(const EpochTime& right_side) const { | ||
if (*this == right_side) return false; | ||
if (this->time_s_ < right_side.time_s_) return false; | ||
if (this->time_s_ > right_side.time_s_) return true; | ||
if (this->fraction_s_ < right_side.fraction_s_) return false; | ||
return true; | ||
} | ||
|
||
bool EpochTime::operator<(const EpochTime& right_side) const { | ||
if (*this == right_side) return false; | ||
if (this->time_s_ > right_side.time_s_) return false; | ||
if (this->time_s_ < right_side.time_s_) return true; | ||
if (this->fraction_s_ > right_side.fraction_s_) return false; | ||
return true; | ||
} | ||
|
||
EpochTime EpochTime::operator+(const EpochTime& right_side) const { | ||
time_t time_s = this->time_s_ + right_side.GetTime_s(); | ||
double fraction_s = this->fraction_s_ + right_side.GetFraction_s(); | ||
if (fraction_s > 1.0) { | ||
fraction_s -= 1.0; | ||
time_s += 1; | ||
} | ||
EpochTime result(time_s, fraction_s); | ||
return result; | ||
} | ||
|
||
EpochTime EpochTime::operator-(const EpochTime& right_side) const { | ||
time_t time_s = this->time_s_ - right_side.GetTime_s(); // TODO: assertion for negative value | ||
double fraction_s = this->fraction_s_ - right_side.GetFraction_s(); | ||
if (fraction_s < 0.0) { | ||
fraction_s += 1.0; | ||
time_s -= 1; | ||
} | ||
EpochTime result(time_s, fraction_s); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @file epoch_time.hpp | ||
* @brief A class to handle time like UNIX time with fractions | ||
*/ | ||
|
||
#ifndef S2E_LIBRARY_TIME_SYSTEM_EPOCH_TIME_HPP_ | ||
#define S2E_LIBRARY_TIME_SYSTEM_EPOCH_TIME_HPP_ | ||
|
||
#include <cstdint> | ||
|
||
#include "date_time_format.hpp" | ||
|
||
class DateTime; | ||
|
||
/** | ||
* @class EpochTime | ||
* @brief A class to handle time like UNIX time with fractions. | ||
* @note This class doesn't care leap seconds. | ||
*/ | ||
class EpochTime { | ||
public: | ||
/** | ||
* @fn EpochTime | ||
* @brief Constructor initialized with week and second | ||
*/ | ||
EpochTime(const uint64_t time_s = 0, const double fraction_s = 0.0) : time_s_(time_s), fraction_s_(fraction_s) {} | ||
/** | ||
* @fn EpochTime | ||
* @brief Constructor initialized with date time expression | ||
*/ | ||
EpochTime(const DateTime date_time); | ||
/** | ||
* @fn ~EpochTime | ||
* @brief Destructor | ||
*/ | ||
~EpochTime() {} | ||
|
||
// Getter | ||
/** | ||
* @fn GetTime_s | ||
* @return time [s] | ||
*/ | ||
inline uint64_t GetTime_s() const { return time_s_; } | ||
/** | ||
* @fn GetFraction_s | ||
* @return fraction time [s] | ||
*/ | ||
inline double GetFraction_s() const { return fraction_s_; } | ||
/** | ||
* @fn GetTimeWithFraction_s | ||
* @return time + fraction [s] | ||
*/ | ||
inline double GetTimeWithFraction_s() const { return (double)(time_s_) + fraction_s_; } | ||
|
||
// Operator | ||
bool operator==(const EpochTime& target) const; | ||
bool operator!=(const EpochTime& target) const { return !(*this == target); } | ||
bool operator>(const EpochTime& right_side) const; | ||
bool operator<=(const EpochTime& right_side) const { return !(*this > right_side); } | ||
bool operator<(const EpochTime& right_side) const; | ||
bool operator>=(const EpochTime& right_side) const { return !(*this < right_side); } | ||
EpochTime operator+(const EpochTime& right_side) const; | ||
EpochTime operator-(const EpochTime& right_side) const; | ||
|
||
private: | ||
uint64_t time_s_; //!< Number of seconds without leap seconds since 00:00:00 Jan 1 1970 UTC. | ||
double fraction_s_; //!< Fraction of second under 1 sec [0, 1) | ||
}; | ||
|
||
#endif // S2E_LIBRARY_TIME_SYSTEM_EPOCH_TIME_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @file gps_time.cpp | ||
* @brief A class to define GPS time expression | ||
*/ | ||
|
||
#include "gps_time.hpp" | ||
|
||
const DateTime GpsTime::kEpochOfGpsTimeInDateTime_ = DateTime("1980/1/6 00:00:00.0"); | ||
const EpochTime GpsTime::kEpochOfGpsTimeInEpochTime_ = EpochTime(kEpochOfGpsTimeInDateTime_); | ||
const EpochTime GpsTime::kLeapSecondAheadFromUtc_ = EpochTime(18, 0); //!< Leap second ahead from UTC @ May 2023 | ||
|
||
void GpsTime::CalcGpsWeekTime() { | ||
EpochTime time_diff = epoch_time_ - kEpochOfGpsTimeInEpochTime_; | ||
week_ = (size_t)(time_diff.GetTime_s() / kSecondsInWeek_); | ||
elapsed_time_from_week_s_ = (double)(time_diff.GetTime_s() - week_ * kSecondsInWeek_) + time_diff.GetFraction_s(); | ||
} | ||
|
||
void GpsTime::CalcEpochTime() { | ||
size_t integer_time_s = (size_t)elapsed_time_from_week_s_; | ||
double fraction_s = elapsed_time_from_week_s_ - (double)(integer_time_s); | ||
time_t time_s = week_ * kSecondsInWeek_ + integer_time_s; | ||
EpochTime time_diff(time_s, fraction_s); | ||
|
||
epoch_time_ = kEpochOfGpsTimeInEpochTime_ + time_diff; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.