timeval.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 
23 #include "timeval.h"
24 
25 #if defined(WIN32) && !defined(MSDOS)
26 
27 struct curltime curlx_tvnow(void)
28 {
29  /*
30  ** GetTickCount() is available on _all_ Windows versions from W95 up
31  ** to nowadays. Returns milliseconds elapsed since last system boot,
32  ** increases monotonically and wraps once 49.7 days have elapsed.
33  */
34  struct curltime now;
35 #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
36  (_WIN32_WINNT < _WIN32_WINNT_VISTA)
37  DWORD milliseconds = GetTickCount();
38  now.tv_sec = milliseconds / 1000;
39  now.tv_usec = (milliseconds % 1000) * 1000;
40 #else
41  ULONGLONG milliseconds = GetTickCount64();
42  now.tv_sec = (time_t) (milliseconds / 1000);
43  now.tv_usec = (unsigned int) (milliseconds % 1000) * 1000;
44 #endif
45 
46  return now;
47 }
48 
49 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
50 
51 struct curltime curlx_tvnow(void)
52 {
53  /*
54  ** clock_gettime() is granted to be increased monotonically when the
55  ** monotonic clock is queried. Time starting point is unspecified, it
56  ** could be the system start-up time, the Epoch, or something else,
57  ** in any case the time starting point does not change once that the
58  ** system has started up.
59  */
60  struct timeval now;
61  struct curltime cnow;
62  struct timespec tsnow;
63  if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
64  cnow.tv_sec = tsnow.tv_sec;
65  cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
66  }
67  /*
68  ** Even when the configure process has truly detected monotonic clock
69  ** availability, it might happen that it is not actually available at
70  ** run-time. When this occurs simply fallback to other time source.
71  */
72 #ifdef HAVE_GETTIMEOFDAY
73  else {
74  (void)gettimeofday(&now, NULL);
75  cnow.tv_sec = now.tv_sec;
76  cnow.tv_usec = (unsigned int)now.tv_usec;
77  }
78 #else
79  else {
80  cnow.tv_sec = time(NULL);
81  cnow.tv_usec = 0;
82  }
83 #endif
84  return cnow;
85 }
86 
87 #elif defined(HAVE_GETTIMEOFDAY)
88 
89 struct curltime curlx_tvnow(void)
90 {
91  /*
92  ** gettimeofday() is not granted to be increased monotonically, due to
93  ** clock drifting and external source time synchronization it can jump
94  ** forward or backward in time.
95  */
96  struct timeval now;
97  struct curltime ret;
98  (void)gettimeofday(&now, NULL);
99  ret.tv_sec = now.tv_sec;
100  ret.tv_usec = now.tv_usec;
101  return ret;
102 }
103 
104 #else
105 
106 struct curltime curlx_tvnow(void)
107 {
108  /*
109  ** time() returns the value of time in seconds since the Epoch.
110  */
111  struct curltime now;
112  now.tv_sec = time(NULL);
113  now.tv_usec = 0;
114  return now;
115 }
116 
117 #endif
118 
119 /*
120  * Make sure that the first argument is the more recent time, as otherwise
121  * we'll get a weird negative time-diff back...
122  *
123  * Returns: the time difference in number of milliseconds. For large diffs it
124  * returns 0x7fffffff on 32bit time_t systems.
125  *
126  * @unittest: 1323
127  */
128 time_t curlx_tvdiff(struct curltime newer, struct curltime older)
129 {
130 #if SIZEOF_TIME_T < 8
131  /* for 32bit time_t systems, add a precaution to avoid overflow for really
132  big time differences */
133  time_t diff = newer.tv_sec-older.tv_sec;
134  if(diff >= (0x7fffffff/1000))
135  return 0x7fffffff;
136 #endif
137  return (newer.tv_sec-older.tv_sec)*1000+
138  (int)(newer.tv_usec-older.tv_usec)/1000;
139 }
140 
141 /*
142  * Make sure that the first argument is the more recent time, as otherwise
143  * we'll get a weird negative time-diff back...
144  *
145  * Returns: the time difference in number of microseconds. For too large diffs
146  * it returns max value.
147  */
148 time_t Curl_tvdiff_us(struct curltime newer, struct curltime older)
149 {
150  time_t diff = newer.tv_sec-older.tv_sec;
151 #if SIZEOF_TIME_T < 8
152  /* for 32bit time_t systems */
153  if(diff >= (0x7fffffff/1000000))
154  return 0x7fffffff;
155 #else
156  /* for 64bit time_t systems */
157  if(diff >= (0x7fffffffffffffffLL/1000000))
158  return 0x7fffffffffffffffLL;
159 #endif
160  return (newer.tv_sec-older.tv_sec)*1000000+
161  (int)(newer.tv_usec-older.tv_usec);
162 }
time_t tv_sec
Definition: timeval.h:33
struct curltime curlx_tvnow(void)
Definition: timeval.c:106
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
struct curltime now
Definition: unit1399.c:83
time_t curlx_tvdiff(struct curltime newer, struct curltime older)
Definition: timeval.c:128
time_t Curl_tvdiff_us(struct curltime newer, struct curltime older)
Definition: timeval.c:148
unsigned int tv_usec
Definition: timeval.h:34


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16