Source
xxxxxxxxxx
static int time_unit_seconds[ZBX_TIME_UNIT_COUNT] = {0, 1, SEC_PER_MIN, SEC_PER_HOUR, SEC_PER_DAY, SEC_PER_WEEK, 0,
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/******************************************************************************
* *
* Purpose: Gets the current time. *
* *
* Return value: Time in seconds *
* *
* Comments: Time in seconds since midnight (00:00:00), *
* January 1, 1970, coordinated universal time (UTC). *
* *
******************************************************************************/
double zbx_time(void)
{
zbx_timespec_t ts;
zbx_timespec(&ts);
return (double)ts.sec + 1.0e-9 * (double)ts.ns;
}
/******************************************************************************
* *
* Purpose: Gets the current time. *
* *
* Comments: Time in seconds since midnight (00:00:00), *
* January 1, 1970, coordinated universal time (UTC). *
* *
******************************************************************************/
void zbx_timespec(zbx_timespec_t *ts)
{
static ZBX_THREAD_LOCAL zbx_timespec_t last_ts = {0, 0};
static ZBX_THREAD_LOCAL int corr = 0;
static ZBX_THREAD_LOCAL LARGE_INTEGER tickPerSecond = {0};
struct _timeb tb;
int sec_diff;
struct timeval tv;
int rc = -1;
struct timespec tp;
if (0 == tickPerSecond.QuadPart)
QueryPerformanceFrequency(&tickPerSecond);
_ftime(&tb);
ts->sec = (int)tb.time;
ts->ns = tb.millitm * 1000000;
if (0 != tickPerSecond.QuadPart)
{
LARGE_INTEGER tick;
if (TRUE == QueryPerformanceCounter(&tick))
{
static ZBX_THREAD_LOCAL LARGE_INTEGER last_tick = {0};
if (0 < last_tick.QuadPart)
{
LARGE_INTEGER qpc_tick = {0}, ntp_tick = {0};
/* _ftime () returns precision in milliseconds, but 'ns' could be increased up to 1ms */
if (last_ts.sec == ts->sec && last_ts.ns > ts->ns && 1000000 > (last_ts.ns - ts->ns))
{
ts->ns = last_ts.ns;
}
else
{
ntp_tick.QuadPart = tickPerSecond.QuadPart * (ts->sec - last_ts.sec) +
tickPerSecond.QuadPart * (ts->ns - last_ts.ns) / 1000000000;