Blame lib/gettimeofday.c

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