[time] use a 64-bit time_t in some places

This commit is contained in:
Timothy Stack 2021-03-31 10:03:00 -07:00
parent 30b4067e43
commit 32ddc76624
7 changed files with 55 additions and 19 deletions

View File

@ -1,6 +1,6 @@
# aminclude_static.am generated automatically by Autoconf
# from AX_AM_MACROS_STATIC on Sun Mar 28 13:05:16 PDT 2021
# from AX_AM_MACROS_STATIC on Mon Mar 29 09:04:25 PDT 2021
# Code coverage

View File

@ -1,3 +1,31 @@
/**
* Copyright (c) 2021, 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;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (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_humanize_time_hh
#define lnav_humanize_time_hh

View File

@ -118,16 +118,16 @@ void secs2wday(const struct timeval &tv, struct tm *res)
res->tm_wday += DAYSPERWEEK;
}
struct tm *secs2tm(time_t *tim_p, struct tm *res)
struct tm *secs2tm(lnav::time64_t tim, struct tm *res)
{
long days, rem;
time_t lcltime;
lnav::time64_t lcltime;
int y;
int yleap;
const unsigned short int *ip;
/* base decision about std/dst time on current time */
lcltime = *tim_p;
lcltime = tim;
days = ((long)lcltime) / SECSPERDAY;
rem = ((long)lcltime) % SECSPERDAY;

View File

@ -33,9 +33,16 @@
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <inttypes.h>
#include <string.h>
struct tm *secs2tm(time_t *tim_p, struct tm *res);
namespace lnav {
using time64_t = uint64_t;
}
struct tm *secs2tm(lnav::time64_t tim, struct tm *res);
/**
* Convert the time stored in a 'tm' struct into epoch time.
*
@ -57,7 +64,7 @@ time_t convert_log_time_to_local(time_t value) {
return tm2sec(&tm);
}
constexpr time_t MAX_TIME_T = 4000000000LL;
constexpr lnav::time64_t MAX_TIME_T = 4000000000LL;
enum exttm_bits_t {
ETB_YEAR_SET,

View File

@ -292,7 +292,7 @@ inline void ftime_S(char *dst, off_t &off_inout, ssize_t len, const struct exttm
inline bool ptime_s(struct exttm *dst, const char *str, off_t &off_inout, ssize_t len)
{
off_t off_start = off_inout;
time_t epoch = 0;
lnav::time64_t epoch = 0;
while (off_inout < len && isdigit(str[off_inout])) {
if ((off_inout - off_start) > 11) {
@ -308,7 +308,7 @@ inline bool ptime_s(struct exttm *dst, const char *str, off_t &off_inout, ssize_
return false;
}
secs2tm(&epoch, &dst->et_tm);
secs2tm(epoch, &dst->et_tm);
dst->et_flags = ETF_DAY_SET|ETF_MONTH_SET|ETF_YEAR_SET|ETF_MACHINE_ORIENTED|ETF_EPOCH_TIME;
return (epoch > 0);
@ -325,7 +325,7 @@ inline void ftime_s(char *dst, off_t &off_inout, ssize_t len, const struct exttm
inline bool ptime_q(struct exttm *dst, const char *str, off_t &off_inout, ssize_t len)
{
off_t off_start = off_inout;
time_t epoch = 0;
lnav::time64_t epoch = 0;
while (off_inout < len && isxdigit(str[off_inout])) {
if ((off_inout - off_start) > 11) {
@ -362,7 +362,7 @@ inline bool ptime_q(struct exttm *dst, const char *str, off_t &off_inout, ssize_
return false;
}
secs2tm(&epoch, &dst->et_tm);
secs2tm(epoch, &dst->et_tm);
dst->et_flags = ETF_DAY_SET|ETF_MONTH_SET|ETF_YEAR_SET|ETF_MACHINE_ORIENTED|ETF_EPOCH_TIME;
return (epoch > 0);
@ -448,7 +448,7 @@ inline void ftime_H(char *dst, off_t &off_inout, ssize_t len, const struct exttm
inline bool ptime_i(struct exttm *dst, const char *str, off_t &off_inout, ssize_t len)
{
uint64_t epoch_ms = 0;
time_t epoch;
lnav::time64_t epoch;
while (off_inout < len && isdigit(str[off_inout])) {
epoch_ms *= 10;
@ -463,7 +463,7 @@ inline bool ptime_i(struct exttm *dst, const char *str, off_t &off_inout, ssize_
return false;
}
secs2tm(&epoch, &dst->et_tm);
secs2tm(epoch, &dst->et_tm);
dst->et_flags = ETF_DAY_SET|ETF_MONTH_SET|ETF_YEAR_SET|ETF_MACHINE_ORIENTED|ETF_EPOCH_TIME;
return (epoch_ms > 0);
@ -481,7 +481,7 @@ inline void ftime_i(char *dst, off_t &off_inout, ssize_t len, const struct exttm
inline bool ptime_6(struct exttm *dst, const char *str, off_t &off_inout, ssize_t len)
{
uint64_t epoch_us = 0;
time_t epoch;
lnav::time64_t epoch;
while (off_inout < len && isdigit(str[off_inout])) {
epoch_us *= 10;
@ -496,7 +496,7 @@ inline bool ptime_6(struct exttm *dst, const char *str, off_t &off_inout, ssize_
return false;
}
secs2tm(&epoch, &dst->et_tm);
secs2tm(epoch, &dst->et_tm);
dst->et_flags = ETF_DAY_SET|ETF_MONTH_SET|ETF_YEAR_SET|ETF_MACHINE_ORIENTED|ETF_EPOCH_TIME;
return (epoch_us > 0);
@ -1008,13 +1008,13 @@ inline bool ptime_at(struct exttm *dst, const char *str, off_t &off_inout, ssize
}
dst->et_nsec = 0;
time_t small_secs = secs - 4611686018427387914ULL;
lnav::time64_t small_secs = secs - 4611686018427387914ULL;
if (small_secs >= MAX_TIME_T) {
return false;
}
secs2tm(&small_secs, &dst->et_tm);
secs2tm(small_secs, &dst->et_tm);
});
if ((len - off_inout) == 8) {

View File

@ -523,13 +523,13 @@ void attach_sqlite_db(sqlite3 *db, const std::string &filename)
}
}
ssize_t sql_strftime(char *buffer, size_t buffer_size, time_t time, int millis,
ssize_t sql_strftime(char *buffer, size_t buffer_size, lnav::time64_t tim, int millis,
char sep)
{
struct tm gmtm;
int year, month, index = 0;
secs2tm(&time, &gmtm);
secs2tm(tim, &gmtm);
year = gmtm.tm_year + 1900;
month = gmtm.tm_mon + 1;
buffer[index++] = '0' + ((year / 1000) % 10);

View File

@ -41,6 +41,7 @@
#include <string>
#include <vector>
#include "base/time_util.hh"
#include "attr_line.hh"
#include "help_text.hh"
@ -67,7 +68,7 @@ void dump_sqlite_schema(sqlite3 *db, std::string &schema_out);
void attach_sqlite_db(sqlite3 *db, const std::string &filename);
ssize_t sql_strftime(char *buffer, size_t buffer_size, time_t time, int millis,
ssize_t sql_strftime(char *buffer, size_t buffer_size, lnav::time64_t tim, int millis,
char sep = ' ');
inline ssize_t sql_strftime(char *buffer, size_t buffer_size,