Blame lib/gettimeofday.c

Packit Service fdd496
/* Provide gettimeofday for systems that don't have it or for which it's broken.
Packit Service fdd496
Packit Service fdd496
   Copyright (C) 2001-2003, 2005-2007, 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software; you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3, or (at your option)
Packit Service fdd496
   any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* written by Jim Meyering */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
/* Specification.  */
Packit Service fdd496
#include <sys/time.h>
Packit Service fdd496
Packit Service fdd496
#include <time.h>
Packit Service fdd496
Packit Service fdd496
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit Service fdd496
# define WINDOWS_NATIVE
Packit Service fdd496
# include <windows.h>
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#include "localtime-buffer.h"
Packit Service fdd496
Packit Service fdd496
#ifdef WINDOWS_NATIVE
Packit Service fdd496
Packit Service fdd496
/* GetSystemTimePreciseAsFileTime was introduced only in Windows 8.  */
Packit Service fdd496
typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME *lpTime);
Packit Service fdd496
static GetSystemTimePreciseAsFileTimeFuncType GetSystemTimePreciseAsFileTimeFunc = NULL;
Packit Service fdd496
static BOOL initialized = FALSE;
Packit Service fdd496
Packit Service fdd496
static void
Packit Service fdd496
initialize (void)
Packit Service fdd496
{
Packit Service fdd496
  HMODULE kernel32 = LoadLibrary ("kernel32.dll");
Packit Service fdd496
  if (kernel32 != NULL)
Packit Service fdd496
    {
Packit Service fdd496
      GetSystemTimePreciseAsFileTimeFunc =
Packit Service fdd496
	(GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime");
Packit Service fdd496
    }
Packit Service fdd496
  initialized = TRUE;
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* This is a wrapper for gettimeofday.  It is used only on systems
Packit Service fdd496
   that lack this function, or whose implementation of this function
Packit Service fdd496
   causes problems.
Packit Service fdd496
   Work around the bug in some systems whereby gettimeofday clobbers
Packit Service fdd496
   the static buffer that localtime uses for its return value.  The
Packit Service fdd496
   gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
Packit Service fdd496
   this problem.  */
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
gettimeofday (struct timeval *restrict tv, void *restrict tz)
Packit Service fdd496
{
Packit Service fdd496
#undef gettimeofday
Packit Service fdd496
#ifdef WINDOWS_NATIVE
Packit Service fdd496
Packit Service fdd496
  /* On native Windows, there are two ways to get the current time:
Packit Service fdd496
     GetSystemTimeAsFileTime
Packit Service fdd496
     <https://msdn.microsoft.com/en-us/library/ms724397.aspx>
Packit Service fdd496
     or
Packit Service fdd496
     GetSystemTimePreciseAsFileTime
Packit Service fdd496
     <https://msdn.microsoft.com/en-us/library/hh706895.aspx>.
Packit Service fdd496
     GetSystemTimeAsFileTime produces values that jump by increments of
Packit Service fdd496
     15.627 milliseconds (!) on average.
Packit Service fdd496
     Whereas GetSystemTimePreciseAsFileTime values usually jump by 1 or 2
Packit Service fdd496
     microseconds.
Packit Service fdd496
     More discussion on this topic:
Packit Service fdd496
     <http://www.windowstimestamp.com/description>.  */
Packit Service fdd496
  FILETIME current_time;
Packit Service fdd496
Packit Service fdd496
  if (!initialized)
Packit Service fdd496
    initialize ();
Packit Service fdd496
  if (GetSystemTimePreciseAsFileTimeFunc != NULL)
Packit Service fdd496
    GetSystemTimePreciseAsFileTimeFunc (&current_time);
Packit Service fdd496
  else
Packit Service fdd496
    GetSystemTimeAsFileTime (&current_time);
Packit Service fdd496
Packit Service fdd496
  /* Convert from FILETIME to 'struct timeval'.  */
Packit Service fdd496
  /* FILETIME: <https://msdn.microsoft.com/en-us/library/ms724284.aspx> */
Packit Service fdd496
  ULONGLONG since_1601 =
Packit Service fdd496
    ((ULONGLONG) current_time.dwHighDateTime << 32)
Packit Service fdd496
    | (ULONGLONG) current_time.dwLowDateTime;
Packit Service fdd496
  /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89 leap
Packit Service fdd496
     years, in total 134774 days.  */
Packit Service fdd496
  ULONGLONG since_1970 =
Packit Service fdd496
    since_1601 - (ULONGLONG) 134774 * (ULONGLONG) 86400 * (ULONGLONG) 10000000;
Packit Service fdd496
  ULONGLONG microseconds_since_1970 = since_1970 / (ULONGLONG) 10;
Packit Service fdd496
  tv->tv_sec = microseconds_since_1970 / (ULONGLONG) 1000000;
Packit Service fdd496
  tv->tv_usec = microseconds_since_1970 % (ULONGLONG) 1000000;
Packit Service fdd496
Packit Service fdd496
  return 0;
Packit Service fdd496
Packit Service fdd496
#else
Packit Service fdd496
Packit Service fdd496
# if HAVE_GETTIMEOFDAY
Packit Service fdd496
#  if GETTIMEOFDAY_CLOBBERS_LOCALTIME
Packit Service fdd496
  /* Save and restore the contents of the buffer used for localtime's
Packit Service fdd496
     result around the call to gettimeofday.  */
Packit Service fdd496
  struct tm save = *localtime_buffer_addr;
Packit Service fdd496
#  endif
Packit Service fdd496
Packit Service fdd496
#  if defined timeval /* 'struct timeval' overridden by gnulib?  */
Packit Service fdd496
#   undef timeval
Packit Service fdd496
  struct timeval otv;
Packit Service fdd496
  int result = gettimeofday (&otv, (struct timezone *) tz);
Packit Service fdd496
  if (result == 0)
Packit Service fdd496
    {
Packit Service fdd496
      tv->tv_sec = otv.tv_sec;
Packit Service fdd496
      tv->tv_usec = otv.tv_usec;
Packit Service fdd496
    }
Packit Service fdd496
#  else
Packit Service fdd496
  int result = gettimeofday (tv, (struct timezone *) tz);
Packit Service fdd496
#  endif
Packit Service fdd496
Packit Service fdd496
#  if GETTIMEOFDAY_CLOBBERS_LOCALTIME
Packit Service fdd496
  *localtime_buffer_addr = save;
Packit Service fdd496
#  endif
Packit Service fdd496
Packit Service fdd496
  return result;
Packit Service fdd496
Packit Service fdd496
# else
Packit Service fdd496
Packit Service fdd496
#  if !defined OK_TO_USE_1S_CLOCK
Packit Service fdd496
#   error "Only 1-second nominal clock resolution found.  Is that intended?" \
Packit Service fdd496
          "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
Packit Service fdd496
#  endif
Packit Service fdd496
  tv->tv_sec = time (NULL);
Packit Service fdd496
  tv->tv_usec = 0;
Packit Service fdd496
Packit Service fdd496
  return 0;
Packit Service fdd496
Packit Service fdd496
# endif
Packit Service fdd496
#endif
Packit Service fdd496
}