diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-07 04:48:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-07 04:48:35 +0000 |
commit | 207df6fc406e81bfeebdff7f404bd242ff3f099f (patch) | |
tree | a1a796b056909dd0a04ffec163db9363a8757808 /src/base/time_util.hh | |
parent | Releasing progress-linux version 0.11.2-1~progress7.99u1. (diff) | |
download | lnav-207df6fc406e81bfeebdff7f404bd242ff3f099f.tar.xz lnav-207df6fc406e81bfeebdff7f404bd242ff3f099f.zip |
Merging upstream version 0.12.2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/base/time_util.hh')
-rw-r--r-- | src/base/time_util.hh | 64 |
1 files changed, 62 insertions, 2 deletions
diff --git a/src/base/time_util.hh b/src/base/time_util.hh index ef9687f..5640463 100644 --- a/src/base/time_util.hh +++ b/src/base/time_util.hh @@ -30,6 +30,8 @@ #ifndef lnav_time_util_hh #define lnav_time_util_hh +#include <chrono> + #include <inttypes.h> #include <string.h> #include <sys/time.h> @@ -37,6 +39,7 @@ #include <time.h> #include "config.h" +#include "date/date.h" namespace lnav { @@ -48,6 +51,10 @@ ssize_t strftime_rfc3339(char* buffer, int millis, char sep = ' '); +date::sys_seconds to_sys_time(date::local_seconds secs); + +date::local_seconds to_local_time(date::sys_seconds secs); + } // namespace lnav struct tm* secs2tm(lnav::time64_t tim, struct tm* res); @@ -84,9 +91,15 @@ enum exttm_bits_t { ETB_SECOND_SET, ETB_MACHINE_ORIENTED, ETB_EPOCH_TIME, + ETB_SUB_NOT_IN_FORMAT, ETB_MILLIS_SET, ETB_MICROS_SET, ETB_NANOS_SET, + ETB_ZONE_SET, + ETB_Z_FOR_UTC, + ETB_Z_COLON, + ETB_Z_IS_UTC, + ETB_Z_IS_GMT, }; enum exttm_flags_t { @@ -98,12 +111,20 @@ enum exttm_flags_t { ETF_SECOND_SET = (1UL << ETB_SECOND_SET), ETF_MACHINE_ORIENTED = (1UL << ETB_MACHINE_ORIENTED), ETF_EPOCH_TIME = (1UL << ETB_EPOCH_TIME), + ETF_SUB_NOT_IN_FORMAT = (1UL << ETB_SUB_NOT_IN_FORMAT), ETF_MILLIS_SET = (1UL << ETB_MILLIS_SET), ETF_MICROS_SET = (1UL << ETB_MICROS_SET), ETF_NANOS_SET = (1UL << ETB_NANOS_SET), + ETF_ZONE_SET = (1UL << ETB_ZONE_SET), + ETF_Z_FOR_UTC = (1UL << ETB_Z_FOR_UTC), + ETF_Z_COLON = (1UL << ETB_Z_COLON), + ETF_Z_IS_UTC = (1UL << ETB_Z_IS_UTC), + ETF_Z_IS_GMT = (1UL << ETB_Z_IS_GMT), }; struct exttm { + static exttm from_tv(const timeval& tv); + struct tm et_tm {}; int32_t et_nsec{0}; unsigned int et_flags{0}; @@ -139,6 +160,13 @@ operator<(const struct timeval& left, const struct timeval& right) } inline bool +operator<=(const struct timeval& left, const struct timeval& right) +{ + return left.tv_sec < right.tv_sec + || ((left.tv_sec == right.tv_sec) && (left.tv_usec <= right.tv_usec)); +} + +inline bool operator!=(const struct timeval& left, const struct timeval& right) { return left.tv_sec != right.tv_sec || left.tv_usec != right.tv_usec; @@ -147,7 +175,7 @@ operator!=(const struct timeval& left, const struct timeval& right) inline bool operator==(const struct timeval& left, const struct timeval& right) { - return left.tv_sec == right.tv_sec || left.tv_usec == right.tv_usec; + return left.tv_sec == right.tv_sec && left.tv_usec == right.tv_usec; } inline struct timeval @@ -159,16 +187,31 @@ operator-(const struct timeval& lhs, const struct timeval& rhs) return diff; } +inline struct timeval +operator+(const struct timeval& lhs, const struct timeval& rhs) +{ + struct timeval retval; + + timeradd(&lhs, &rhs, &retval); + return retval; +} + typedef int64_t mstime_t; inline mstime_t +to_mstime(const timeval& tv) +{ + return tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL; +} + +inline mstime_t getmstime() { struct timeval tv; gettimeofday(&tv, nullptr); - return tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL; + return to_mstime(tv); } inline struct timeval @@ -203,4 +246,21 @@ hour_num(time_t ti) return ti / (60 * 60); } +struct time_range { + struct timeval tr_begin; + struct timeval tr_end; + + bool operator<(const time_range& rhs) const + { + return this->tr_begin < rhs.tr_begin; + } + + time_range& operator|=(const time_range& rhs); + + bool contains_inclusive(const timeval& tv) const; + + void extend_to(const timeval& tv); + std::chrono::milliseconds duration() const; +}; + #endif |