Blame lib/gettimeofday.c

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