11cb0ef41Sopenharmony_ci/* MIT License
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Copyright (c) 2008 Daniel Stenberg
41cb0ef41Sopenharmony_ci *
51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights
81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
111cb0ef41Sopenharmony_ci *
121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next
131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
141cb0ef41Sopenharmony_ci * Software.
151cb0ef41Sopenharmony_ci *
161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
221cb0ef41Sopenharmony_ci * SOFTWARE.
231cb0ef41Sopenharmony_ci *
241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT
251cb0ef41Sopenharmony_ci */
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#include "ares_setup.h"
281cb0ef41Sopenharmony_ci#include "ares.h"
291cb0ef41Sopenharmony_ci#include "ares_private.h"
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci#if defined(WIN32) && !defined(MSDOS)
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cistruct timeval ares__tvnow(void)
341cb0ef41Sopenharmony_ci{
351cb0ef41Sopenharmony_ci  /*
361cb0ef41Sopenharmony_ci  ** GetTickCount() is available on _all_ Windows versions from W95 up
371cb0ef41Sopenharmony_ci  ** to nowadays. Returns milliseconds elapsed since last system boot,
381cb0ef41Sopenharmony_ci  ** increases monotonically and wraps once 49.7 days have elapsed.
391cb0ef41Sopenharmony_ci  */
401cb0ef41Sopenharmony_ci  struct timeval now;
411cb0ef41Sopenharmony_ci  DWORD          milliseconds = GetTickCount();
421cb0ef41Sopenharmony_ci  now.tv_sec                  = (long)milliseconds / 1000;
431cb0ef41Sopenharmony_ci  now.tv_usec                 = (long)(milliseconds % 1000) * 1000;
441cb0ef41Sopenharmony_ci  return now;
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cistruct timeval ares__tvnow(void)
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  /*
521cb0ef41Sopenharmony_ci  ** clock_gettime() is granted to be increased monotonically when the
531cb0ef41Sopenharmony_ci  ** monotonic clock is queried. Time starting point is unspecified, it
541cb0ef41Sopenharmony_ci  ** could be the system start-up time, the Epoch, or something else,
551cb0ef41Sopenharmony_ci  ** in any case the time starting point does not change once that the
561cb0ef41Sopenharmony_ci  ** system has started up.
571cb0ef41Sopenharmony_ci  */
581cb0ef41Sopenharmony_ci  struct timeval  now;
591cb0ef41Sopenharmony_ci  struct timespec tsnow;
601cb0ef41Sopenharmony_ci  if (0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
611cb0ef41Sopenharmony_ci    now.tv_sec  = tsnow.tv_sec;
621cb0ef41Sopenharmony_ci    now.tv_usec = (int)(tsnow.tv_nsec / 1000);
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci  /*
651cb0ef41Sopenharmony_ci  ** Even when the configure process has truly detected monotonic clock
661cb0ef41Sopenharmony_ci  ** availability, it might happen that it is not actually available at
671cb0ef41Sopenharmony_ci  ** run-time. When this occurs simply fallback to other time source.
681cb0ef41Sopenharmony_ci  */
691cb0ef41Sopenharmony_ci#  ifdef HAVE_GETTIMEOFDAY
701cb0ef41Sopenharmony_ci  else
711cb0ef41Sopenharmony_ci    (void)gettimeofday(&now, NULL); /* LCOV_EXCL_LINE */
721cb0ef41Sopenharmony_ci#  else
731cb0ef41Sopenharmony_ci  else {
741cb0ef41Sopenharmony_ci    now.tv_sec  = (long)time(NULL);
751cb0ef41Sopenharmony_ci    now.tv_usec = 0;
761cb0ef41Sopenharmony_ci  }
771cb0ef41Sopenharmony_ci#  endif
781cb0ef41Sopenharmony_ci  return now;
791cb0ef41Sopenharmony_ci}
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci#elif defined(HAVE_GETTIMEOFDAY)
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_cistruct timeval ares__tvnow(void)
841cb0ef41Sopenharmony_ci{
851cb0ef41Sopenharmony_ci  /*
861cb0ef41Sopenharmony_ci  ** gettimeofday() is not granted to be increased monotonically, due to
871cb0ef41Sopenharmony_ci  ** clock drifting and external source time synchronization it can jump
881cb0ef41Sopenharmony_ci  ** forward or backward in time.
891cb0ef41Sopenharmony_ci  */
901cb0ef41Sopenharmony_ci  struct timeval now;
911cb0ef41Sopenharmony_ci  (void)gettimeofday(&now, NULL);
921cb0ef41Sopenharmony_ci  return now;
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci#else
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_cistruct timeval ares__tvnow(void)
981cb0ef41Sopenharmony_ci{
991cb0ef41Sopenharmony_ci  /*
1001cb0ef41Sopenharmony_ci  ** time() returns the value of time in seconds since the Epoch.
1011cb0ef41Sopenharmony_ci  */
1021cb0ef41Sopenharmony_ci  struct timeval now;
1031cb0ef41Sopenharmony_ci  now.tv_sec  = (long)time(NULL);
1041cb0ef41Sopenharmony_ci  now.tv_usec = 0;
1051cb0ef41Sopenharmony_ci  return now;
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci#endif
109