Blame ares__timeval.c

Packit 514978
Packit 514978
/* Copyright (C) 2008 by Daniel Stenberg et al
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this software and its
Packit 514978
 * documentation for any purpose and without fee is hereby granted, provided
Packit 514978
 * that the above copyright notice appear in all copies and that both that
Packit 514978
 * copyright notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in advertising or
Packit 514978
 * publicity pertaining to distribution of the software without specific,
Packit 514978
 * written prior permission.  M.I.T. makes no representations about the
Packit 514978
 * suitability of this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
#if defined(WIN32) && !defined(MSDOS)
Packit 514978
Packit 514978
struct timeval ares__tvnow(void)
Packit 514978
{
Packit 514978
  /*
Packit 514978
  ** GetTickCount() is available on _all_ Windows versions from W95 up
Packit 514978
  ** to nowadays. Returns milliseconds elapsed since last system boot,
Packit 514978
  ** increases monotonically and wraps once 49.7 days have elapsed.
Packit 514978
  */
Packit 514978
  struct timeval now;
Packit 514978
  DWORD milliseconds = GetTickCount();
Packit 514978
  now.tv_sec = milliseconds / 1000;
Packit 514978
  now.tv_usec = (milliseconds % 1000) * 1000;
Packit 514978
  return now;
Packit 514978
}
Packit 514978
Packit 514978
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
Packit 514978
Packit 514978
struct timeval ares__tvnow(void)
Packit 514978
{
Packit 514978
  /*
Packit 514978
  ** clock_gettime() is granted to be increased monotonically when the
Packit 514978
  ** monotonic clock is queried. Time starting point is unspecified, it
Packit 514978
  ** could be the system start-up time, the Epoch, or something else,
Packit 514978
  ** in any case the time starting point does not change once that the
Packit 514978
  ** system has started up.
Packit 514978
  */
Packit 514978
  struct timeval now;
Packit 514978
  struct timespec tsnow;
Packit 514978
  if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
Packit 514978
    now.tv_sec = tsnow.tv_sec;
Packit 514978
    now.tv_usec = tsnow.tv_nsec / 1000;
Packit 514978
  }
Packit 514978
  /*
Packit 514978
  ** Even when the configure process has truly detected monotonic clock
Packit 514978
  ** availability, it might happen that it is not actually available at
Packit 514978
  ** run-time. When this occurs simply fallback to other time source.
Packit 514978
  */
Packit 514978
#ifdef HAVE_GETTIMEOFDAY
Packit 514978
  else
Packit 514978
    (void)gettimeofday(&now, NULL);  /* LCOV_EXCL_LINE */
Packit 514978
#else
Packit 514978
  else {
Packit 514978
    now.tv_sec = (long)time(NULL);
Packit 514978
    now.tv_usec = 0;
Packit 514978
  }
Packit 514978
#endif
Packit 514978
  return now;
Packit 514978
}
Packit 514978
Packit 514978
#elif defined(HAVE_GETTIMEOFDAY)
Packit 514978
Packit 514978
struct timeval ares__tvnow(void)
Packit 514978
{
Packit 514978
  /*
Packit 514978
  ** gettimeofday() is not granted to be increased monotonically, due to
Packit 514978
  ** clock drifting and external source time synchronization it can jump
Packit 514978
  ** forward or backward in time.
Packit 514978
  */
Packit 514978
  struct timeval now;
Packit 514978
  (void)gettimeofday(&now, NULL);
Packit 514978
  return now;
Packit 514978
}
Packit 514978
Packit 514978
#else
Packit 514978
Packit 514978
struct timeval ares__tvnow(void)
Packit 514978
{
Packit 514978
  /*
Packit 514978
  ** time() returns the value of time in seconds since the Epoch.
Packit 514978
  */
Packit 514978
  struct timeval now;
Packit 514978
  now.tv_sec = (long)time(NULL);
Packit 514978
  now.tv_usec = 0;
Packit 514978
  return now;
Packit 514978
}
Packit 514978
Packit 514978
#endif
Packit 514978
Packit 514978
#if 0 /* Not used */
Packit 514978
/*
Packit 514978
 * Make sure that the first argument is the more recent time, as otherwise
Packit 514978
 * we'll get a weird negative time-diff back...
Packit 514978
 *
Packit 514978
 * Returns: the time difference in number of milliseconds.
Packit 514978
 */
Packit 514978
long ares__tvdiff(struct timeval newer, struct timeval older)
Packit 514978
{
Packit 514978
  return (newer.tv_sec-older.tv_sec)*1000+
Packit 514978
    (newer.tv_usec-older.tv_usec)/1000;
Packit 514978
}
Packit 514978
#endif
Packit 514978