2020-11-17 18:04:23 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2020, Timothy Stack
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Timothy Stack nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
2022-03-16 22:38:08 +00:00
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
2020-11-17 18:04:23 +00:00
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lnav_time_util_hh
|
|
|
|
#define lnav_time_util_hh
|
|
|
|
|
2023-08-15 02:12:17 +00:00
|
|
|
#include <chrono>
|
|
|
|
|
2021-03-31 17:03:00 +00:00
|
|
|
#include <inttypes.h>
|
2020-11-20 05:36:51 +00:00
|
|
|
#include <string.h>
|
2022-03-16 22:38:08 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2024-04-04 17:13:59 +00:00
|
|
|
#include "date/date.h"
|
2024-04-28 17:01:43 +00:00
|
|
|
#include "date/tz.h"
|
2020-11-20 05:36:51 +00:00
|
|
|
|
2021-03-31 17:03:00 +00:00
|
|
|
namespace lnav {
|
|
|
|
|
|
|
|
using time64_t = uint64_t;
|
|
|
|
|
2024-06-01 16:10:04 +00:00
|
|
|
ssize_t strftime_rfc3339(
|
|
|
|
char* buffer, size_t buffer_size, time64_t tim, int millis, char sep = ' ');
|
|
|
|
|
|
|
|
std::string to_rfc3339_string(time64_t tim, int millis, char sep = ' ');
|
|
|
|
|
|
|
|
inline std::string
|
|
|
|
to_rfc3339_string(struct timeval tv, char sep = ' ')
|
|
|
|
{
|
|
|
|
return to_rfc3339_string(tv.tv_sec, tv.tv_usec / 1000, sep);
|
|
|
|
}
|
2022-09-21 03:07:44 +00:00
|
|
|
|
2024-04-28 17:01:43 +00:00
|
|
|
date::sys_info sys_time_to_info(date::sys_seconds secs);
|
|
|
|
|
|
|
|
date::local_info local_time_to_info(date::local_seconds secs);
|
|
|
|
|
2024-02-22 02:23:34 +00:00
|
|
|
date::sys_seconds to_sys_time(date::local_seconds secs);
|
|
|
|
|
|
|
|
date::local_seconds to_local_time(date::sys_seconds secs);
|
|
|
|
|
2022-09-21 03:07:44 +00:00
|
|
|
} // namespace lnav
|
2021-03-31 17:03:00 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
struct tm* secs2tm(lnav::time64_t tim, struct tm* res);
|
2020-11-20 05:36:51 +00:00
|
|
|
/**
|
|
|
|
* Convert the time stored in a 'tm' struct into epoch time.
|
|
|
|
*
|
|
|
|
* @param t The 'tm' structure to convert to epoch time.
|
|
|
|
* @return The given time in seconds since the epoch.
|
|
|
|
*/
|
2022-03-16 22:38:08 +00:00
|
|
|
time_t tm2sec(const struct tm* t);
|
|
|
|
void secs2wday(const struct timeval& tv, struct tm* res);
|
2020-11-20 05:36:51 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline time_t
|
|
|
|
convert_log_time_to_local(time_t value)
|
|
|
|
{
|
2021-03-28 20:50:26 +00:00
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
localtime_r(&value, &tm);
|
|
|
|
#ifdef HAVE_STRUCT_TM_TM_ZONE
|
|
|
|
tm.tm_zone = NULL;
|
|
|
|
#endif
|
|
|
|
tm.tm_isdst = 0;
|
|
|
|
return tm2sec(&tm);
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:03:00 +00:00
|
|
|
constexpr lnav::time64_t MAX_TIME_T = 4000000000LL;
|
2020-11-20 05:36:51 +00:00
|
|
|
|
|
|
|
enum exttm_bits_t {
|
|
|
|
ETB_YEAR_SET,
|
|
|
|
ETB_MONTH_SET,
|
|
|
|
ETB_DAY_SET,
|
2022-06-04 02:17:00 +00:00
|
|
|
ETB_HOUR_SET,
|
|
|
|
ETB_MINUTE_SET,
|
|
|
|
ETB_SECOND_SET,
|
2020-11-20 05:36:51 +00:00
|
|
|
ETB_MACHINE_ORIENTED,
|
|
|
|
ETB_EPOCH_TIME,
|
2023-08-10 23:08:56 +00:00
|
|
|
ETB_SUB_NOT_IN_FORMAT,
|
2022-04-04 16:14:57 +00:00
|
|
|
ETB_MILLIS_SET,
|
|
|
|
ETB_MICROS_SET,
|
|
|
|
ETB_NANOS_SET,
|
2023-08-10 23:08:56 +00:00
|
|
|
ETB_ZONE_SET,
|
|
|
|
ETB_Z_FOR_UTC,
|
|
|
|
ETB_Z_COLON,
|
2023-08-12 06:19:19 +00:00
|
|
|
ETB_Z_IS_UTC,
|
2023-08-18 04:31:28 +00:00
|
|
|
ETB_Z_IS_GMT,
|
2020-11-20 05:36:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum exttm_flags_t {
|
|
|
|
ETF_YEAR_SET = (1UL << ETB_YEAR_SET),
|
|
|
|
ETF_MONTH_SET = (1UL << ETB_MONTH_SET),
|
|
|
|
ETF_DAY_SET = (1UL << ETB_DAY_SET),
|
2022-06-04 02:17:00 +00:00
|
|
|
ETF_HOUR_SET = (1UL << ETB_HOUR_SET),
|
|
|
|
ETF_MINUTE_SET = (1UL << ETB_MINUTE_SET),
|
|
|
|
ETF_SECOND_SET = (1UL << ETB_SECOND_SET),
|
2020-11-20 05:36:51 +00:00
|
|
|
ETF_MACHINE_ORIENTED = (1UL << ETB_MACHINE_ORIENTED),
|
|
|
|
ETF_EPOCH_TIME = (1UL << ETB_EPOCH_TIME),
|
2023-08-10 23:08:56 +00:00
|
|
|
ETF_SUB_NOT_IN_FORMAT = (1UL << ETB_SUB_NOT_IN_FORMAT),
|
2022-04-04 16:14:57 +00:00
|
|
|
ETF_MILLIS_SET = (1UL << ETB_MILLIS_SET),
|
|
|
|
ETF_MICROS_SET = (1UL << ETB_MICROS_SET),
|
|
|
|
ETF_NANOS_SET = (1UL << ETB_NANOS_SET),
|
2023-08-10 23:08:56 +00:00
|
|
|
ETF_ZONE_SET = (1UL << ETB_ZONE_SET),
|
|
|
|
ETF_Z_FOR_UTC = (1UL << ETB_Z_FOR_UTC),
|
|
|
|
ETF_Z_COLON = (1UL << ETB_Z_COLON),
|
2023-08-12 06:19:19 +00:00
|
|
|
ETF_Z_IS_UTC = (1UL << ETB_Z_IS_UTC),
|
2023-08-18 04:31:28 +00:00
|
|
|
ETF_Z_IS_GMT = (1UL << ETB_Z_IS_GMT),
|
2020-11-20 05:36:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct exttm {
|
2023-08-07 06:26:27 +00:00
|
|
|
static exttm from_tv(const timeval& tv);
|
|
|
|
|
2022-05-24 04:41:50 +00:00
|
|
|
struct tm et_tm {};
|
2022-04-04 16:58:16 +00:00
|
|
|
int32_t et_nsec{0};
|
|
|
|
unsigned int et_flags{0};
|
|
|
|
long et_gmtoff{0};
|
2020-11-20 05:36:51 +00:00
|
|
|
|
2022-09-21 03:07:44 +00:00
|
|
|
exttm() { memset(&this->et_tm, 0, sizeof(this->et_tm)); }
|
2022-04-04 18:53:47 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
bool operator==(const exttm& other) const
|
|
|
|
{
|
2020-11-20 05:36:51 +00:00
|
|
|
return memcmp(this, &other, sizeof(exttm)) == 0;
|
2022-05-24 04:41:50 +00:00
|
|
|
}
|
2020-11-20 05:36:51 +00:00
|
|
|
|
2021-03-21 15:53:21 +00:00
|
|
|
struct timeval to_timeval() const;
|
2020-11-20 05:36:51 +00:00
|
|
|
};
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline bool
|
|
|
|
operator<(const struct timeval& left, time_t right)
|
|
|
|
{
|
2020-11-17 18:04:23 +00:00
|
|
|
return left.tv_sec < right;
|
2020-11-20 05:36:51 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline bool
|
|
|
|
operator<(time_t left, const struct timeval& right)
|
|
|
|
{
|
2020-11-17 18:04:23 +00:00
|
|
|
return left < right.tv_sec;
|
2020-11-20 05:36:51 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
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));
|
2020-11-20 05:36:51 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2023-08-02 17:44:13 +00:00
|
|
|
inline bool
|
|
|
|
operator<=(const struct timeval& left, const struct timeval& right)
|
|
|
|
{
|
2023-08-15 13:21:06 +00:00
|
|
|
return left.tv_sec < right.tv_sec
|
2023-08-02 17:44:13 +00:00
|
|
|
|| ((left.tv_sec == right.tv_sec) && (left.tv_usec <= right.tv_usec));
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline bool
|
|
|
|
operator!=(const struct timeval& left, const struct timeval& right)
|
|
|
|
{
|
|
|
|
return left.tv_sec != right.tv_sec || left.tv_usec != right.tv_usec;
|
2020-11-20 05:36:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 03:07:44 +00:00
|
|
|
inline bool
|
|
|
|
operator==(const struct timeval& left, const struct timeval& right)
|
|
|
|
{
|
2023-08-20 14:43:38 +00:00
|
|
|
return left.tv_sec == right.tv_sec && left.tv_usec == right.tv_usec;
|
2022-09-21 03:07:44 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline struct timeval
|
|
|
|
operator-(const struct timeval& lhs, const struct timeval& rhs)
|
2021-04-03 21:03:18 +00:00
|
|
|
{
|
|
|
|
struct timeval diff;
|
|
|
|
|
|
|
|
timersub(&lhs, &rhs, &diff);
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2023-08-28 00:12:29 +00:00
|
|
|
inline struct timeval
|
|
|
|
operator+(const struct timeval& lhs, const struct timeval& rhs)
|
|
|
|
{
|
|
|
|
struct timeval retval;
|
|
|
|
|
|
|
|
timeradd(&lhs, &rhs, &retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2020-11-20 05:36:51 +00:00
|
|
|
typedef int64_t mstime_t;
|
|
|
|
|
2023-08-15 02:04:31 +00:00
|
|
|
inline mstime_t
|
|
|
|
to_mstime(const timeval& tv)
|
|
|
|
{
|
|
|
|
return tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline mstime_t
|
|
|
|
getmstime()
|
|
|
|
{
|
2020-11-20 05:36:51 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
|
2023-08-15 02:04:31 +00:00
|
|
|
return to_mstime(tv);
|
2020-11-20 05:36:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline struct timeval
|
|
|
|
current_timeval()
|
|
|
|
{
|
2021-04-01 23:22:04 +00:00
|
|
|
struct timeval retval;
|
|
|
|
|
|
|
|
gettimeofday(&retval, nullptr);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline struct timespec
|
|
|
|
current_timespec()
|
|
|
|
{
|
2021-09-03 19:33:11 +00:00
|
|
|
struct timespec retval;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &retval);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline time_t
|
|
|
|
day_num(time_t ti)
|
2020-11-20 05:36:51 +00:00
|
|
|
{
|
|
|
|
return ti / (24 * 60 * 60);
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
inline time_t
|
|
|
|
hour_num(time_t ti)
|
2020-11-20 05:36:51 +00:00
|
|
|
{
|
|
|
|
return ti / (60 * 60);
|
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2023-08-15 02:04:31 +00:00
|
|
|
struct time_range {
|
|
|
|
struct timeval tr_begin;
|
|
|
|
struct timeval tr_end;
|
|
|
|
|
2024-05-07 05:36:03 +00:00
|
|
|
bool valid() const { return this->tr_end.tv_sec == 0; }
|
|
|
|
|
|
|
|
void invalidate()
|
|
|
|
{
|
|
|
|
this->tr_begin.tv_sec = INT_MAX;
|
|
|
|
this->tr_begin.tv_usec = 0;
|
|
|
|
this->tr_end.tv_sec = 0;
|
|
|
|
this->tr_end.tv_usec = 0;
|
|
|
|
}
|
|
|
|
|
2023-08-15 02:04:31 +00:00
|
|
|
bool operator<(const time_range& rhs) const
|
|
|
|
{
|
|
|
|
return this->tr_begin < rhs.tr_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_range& operator|=(const time_range& rhs);
|
2023-08-15 13:21:06 +00:00
|
|
|
|
|
|
|
bool contains_inclusive(const timeval& tv) const;
|
|
|
|
|
2023-08-15 02:04:31 +00:00
|
|
|
void extend_to(const timeval& tv);
|
|
|
|
std::chrono::milliseconds duration() const;
|
|
|
|
};
|
|
|
|
|
2020-11-17 18:04:23 +00:00
|
|
|
#endif
|