timeval.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 
00023 #include "timeval.h"
00024 
00025 #if defined(WIN32) && !defined(MSDOS)
00026 
00027 struct timeval curlx_tvnow(void)
00028 {
00029   /*
00030   ** GetTickCount() is available on _all_ Windows versions from W95 up
00031   ** to nowadays. Returns milliseconds elapsed since last system boot,
00032   ** increases monotonically and wraps once 49.7 days have elapsed.
00033   */
00034   struct timeval now;
00035 #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
00036     (_WIN32_WINNT < _WIN32_WINNT_VISTA)
00037   DWORD milliseconds = GetTickCount();
00038   now.tv_sec = milliseconds / 1000;
00039   now.tv_usec = (milliseconds % 1000) * 1000;
00040 #else
00041   ULONGLONG milliseconds = GetTickCount64();
00042   now.tv_sec = (long) (milliseconds / 1000);
00043   now.tv_usec = (long) (milliseconds % 1000) * 1000;
00044 #endif
00045 
00046   return now;
00047 }
00048 
00049 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
00050 
00051 struct timeval curlx_tvnow(void)
00052 {
00053   /*
00054   ** clock_gettime() is granted to be increased monotonically when the
00055   ** monotonic clock is queried. Time starting point is unspecified, it
00056   ** could be the system start-up time, the Epoch, or something else,
00057   ** in any case the time starting point does not change once that the
00058   ** system has started up.
00059   */
00060   struct timeval now;
00061   struct timespec tsnow;
00062   if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
00063     now.tv_sec = tsnow.tv_sec;
00064     now.tv_usec = tsnow.tv_nsec / 1000;
00065   }
00066   /*
00067   ** Even when the configure process has truly detected monotonic clock
00068   ** availability, it might happen that it is not actually available at
00069   ** run-time. When this occurs simply fallback to other time source.
00070   */
00071 #ifdef HAVE_GETTIMEOFDAY
00072   else
00073     (void)gettimeofday(&now, NULL);
00074 #else
00075   else {
00076     now.tv_sec = (long)time(NULL);
00077     now.tv_usec = 0;
00078   }
00079 #endif
00080   return now;
00081 }
00082 
00083 #elif defined(HAVE_GETTIMEOFDAY)
00084 
00085 struct timeval curlx_tvnow(void)
00086 {
00087   /*
00088   ** gettimeofday() is not granted to be increased monotonically, due to
00089   ** clock drifting and external source time synchronization it can jump
00090   ** forward or backward in time.
00091   */
00092   struct timeval now;
00093   (void)gettimeofday(&now, NULL);
00094   return now;
00095 }
00096 
00097 #else
00098 
00099 struct timeval curlx_tvnow(void)
00100 {
00101   /*
00102   ** time() returns the value of time in seconds since the Epoch.
00103   */
00104   struct timeval now;
00105   now.tv_sec = (long)time(NULL);
00106   now.tv_usec = 0;
00107   return now;
00108 }
00109 
00110 #endif
00111 
00112 /*
00113  * Make sure that the first argument is the more recent time, as otherwise
00114  * we'll get a weird negative time-diff back...
00115  *
00116  * Returns: the time difference in number of milliseconds. For large diffs it
00117  * returns 0x7fffffff on 32bit time_t systems.
00118  */
00119 time_t curlx_tvdiff(struct timeval newer, struct timeval older)
00120 {
00121 #if SIZEOF_TIME_T < 8
00122   /* for 32bit time_t systems, add a precaution to avoid overflow for really
00123      big time differences */
00124   time_t diff = newer.tv_sec-older.tv_sec;
00125   if(diff >= (0x7fffffff/1000))
00126     return 0x7fffffff;
00127 #endif
00128   return (newer.tv_sec-older.tv_sec)*1000+
00129     (time_t)(newer.tv_usec-older.tv_usec)/1000;
00130 }
00131 
00132 /*
00133  * Same as curlx_tvdiff but with full usec resolution.
00134  *
00135  * Returns: the time difference in seconds with subsecond resolution.
00136  */
00137 double curlx_tvdiff_secs(struct timeval newer, struct timeval older)
00138 {
00139   if(newer.tv_sec != older.tv_sec)
00140     return (double)(newer.tv_sec-older.tv_sec)+
00141       (double)(newer.tv_usec-older.tv_usec)/1000000.0;
00142   else
00143     return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
00144 }
00145 
00146 /* return the number of seconds in the given input timeval struct */
00147 time_t Curl_tvlong(struct timeval t1)
00148 {
00149   return t1.tv_sec;
00150 }


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