testutil.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "curl_setup.h"
00023 #include <curl/curl.h>
00024 #include "testutil.h"
00025 #include "memdebug.h"
00026 
00027 #if defined(WIN32) && !defined(MSDOS)
00028 
00029 struct timeval tutil_tvnow(void)
00030 {
00031   /*
00032   ** GetTickCount() is available on _all_ Windows versions from W95 up
00033   ** to nowadays. Returns milliseconds elapsed since last system boot,
00034   ** increases monotonically and wraps once 49.7 days have elapsed.
00035   */
00036   struct timeval now;
00037   DWORD milliseconds = GetTickCount();
00038   now.tv_sec = milliseconds / 1000;
00039   now.tv_usec = (milliseconds % 1000) * 1000;
00040   return now;
00041 }
00042 
00043 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
00044 
00045 struct timeval tutil_tvnow(void)
00046 {
00047   /*
00048   ** clock_gettime() is granted to be increased monotonically when the
00049   ** monotonic clock is queried. Time starting point is unspecified, it
00050   ** could be the system start-up time, the Epoch, or something else,
00051   ** in any case the time starting point does not change once that the
00052   ** system has started up.
00053   */
00054   struct timeval now;
00055   struct timespec tsnow;
00056   if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
00057     now.tv_sec = tsnow.tv_sec;
00058     now.tv_usec = tsnow.tv_nsec / 1000;
00059   }
00060   /*
00061   ** Even when the configure process has truly detected monotonic clock
00062   ** availability, it might happen that it is not actually available at
00063   ** run-time. When this occurs simply fallback to other time source.
00064   */
00065 #ifdef HAVE_GETTIMEOFDAY
00066   else
00067     (void)gettimeofday(&now, NULL);
00068 #else
00069   else {
00070     now.tv_sec = (long)time(NULL);
00071     now.tv_usec = 0;
00072   }
00073 #endif
00074   return now;
00075 }
00076 
00077 #elif defined(HAVE_GETTIMEOFDAY)
00078 
00079 struct timeval tutil_tvnow(void)
00080 {
00081   /*
00082   ** gettimeofday() is not granted to be increased monotonically, due to
00083   ** clock drifting and external source time synchronization it can jump
00084   ** forward or backward in time.
00085   */
00086   struct timeval now;
00087   (void)gettimeofday(&now, NULL);
00088   return now;
00089 }
00090 
00091 #else
00092 
00093 struct timeval tutil_tvnow(void)
00094 {
00095   /*
00096   ** time() returns the value of time in seconds since the Epoch.
00097   */
00098   struct timeval now;
00099   now.tv_sec = (long)time(NULL);
00100   now.tv_usec = 0;
00101   return now;
00102 }
00103 
00104 #endif
00105 
00106 /*
00107  * Make sure that the first argument is the more recent time, as otherwise
00108  * we'll get a weird negative time-diff back...
00109  *
00110  * Returns: the time difference in number of milliseconds.
00111  */
00112 long tutil_tvdiff(struct timeval newer, struct timeval older)
00113 {
00114   return (newer.tv_sec-older.tv_sec)*1000+
00115     (newer.tv_usec-older.tv_usec)/1000;
00116 }
00117 
00118 /*
00119  * Same as tutil_tvdiff but with full usec resolution.
00120  *
00121  * Returns: the time difference in seconds with subsecond resolution.
00122  */
00123 double tutil_tvdiff_secs(struct timeval newer, struct timeval older)
00124 {
00125   if(newer.tv_sec != older.tv_sec)
00126     return (double)(newer.tv_sec-older.tv_sec)+
00127       (double)(newer.tv_usec-older.tv_usec)/1000000.0;
00128   else
00129     return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
00130 }
00131 
00132 /* return the number of seconds in the given input timeval struct */
00133 long tutil_tvlong(struct timeval t1)
00134 {
00135   return t1.tv_sec;
00136 }
00137 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06