Initial commit of visual studio 9 git build superproject.
[git-build-vc9.git] / curl / lib / timeval.c
blob908296b38e6d98ddb23f86d7e843b23342b9b70c
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: timeval.c,v 1.32 2008-07-02 03:04:56 yangtse Exp $
22 ***************************************************************************/
24 #include "timeval.h"
26 #if defined(WIN32) && !defined(MSDOS)
28 struct timeval curlx_tvnow(void)
31 ** GetTickCount() is available on _all_ Windows versions from W95 up
32 ** to nowadays. Returns milliseconds elapsed since last system boot,
33 ** increases monotonically and wraps once 49.7 days have elapsed.
35 struct timeval now;
36 DWORD milliseconds = GetTickCount();
37 now.tv_sec = milliseconds / 1000;
38 now.tv_usec = (milliseconds % 1000) * 1000;
39 return now;
42 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
44 struct timeval curlx_tvnow(void)
47 ** clock_gettime() is granted to be increased monotonically when the
48 ** monotonic clock is queried. Time starting point is unspecified, it
49 ** could be the system start-up time, the Epoch, or something else,
50 ** in any case the time starting point does not change once that the
51 ** system has started up.
53 struct timeval now;
54 struct timespec tsnow;
55 if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
56 now.tv_sec = tsnow.tv_sec;
57 now.tv_usec = tsnow.tv_nsec / 1000;
60 ** Even when the configure process has truly detected monotonic clock
61 ** availability, it might happen that it is not actually available at
62 ** run-time. When this occurs simply fallback to other time source.
64 #ifdef HAVE_GETTIMEOFDAY
65 else
66 (void)gettimeofday(&now, NULL);
67 #else
68 else {
69 now.tv_sec = (long)time(NULL);
70 now.tv_usec = 0;
72 #endif
73 return now;
76 #elif defined(HAVE_GETTIMEOFDAY)
78 struct timeval curlx_tvnow(void)
81 ** gettimeofday() is not granted to be increased monotonically, due to
82 ** clock drifting and external source time synchronization it can jump
83 ** forward or backward in time.
85 struct timeval now;
86 (void)gettimeofday(&now, NULL);
87 return now;
90 #else
92 struct timeval curlx_tvnow(void)
95 ** time() returns the value of time in seconds since the Epoch.
97 struct timeval now;
98 now.tv_sec = (long)time(NULL);
99 now.tv_usec = 0;
100 return now;
103 #endif
106 * Make sure that the first argument is the more recent time, as otherwise
107 * we'll get a weird negative time-diff back...
109 * Returns: the time difference in number of milliseconds.
111 long curlx_tvdiff(struct timeval newer, struct timeval older)
113 return (newer.tv_sec-older.tv_sec)*1000+
114 (newer.tv_usec-older.tv_usec)/1000;
118 * Same as curlx_tvdiff but with full usec resolution.
120 * Returns: the time difference in seconds with subsecond resolution.
122 double curlx_tvdiff_secs(struct timeval newer, struct timeval older)
124 return (double)(newer.tv_sec-older.tv_sec)+
125 (double)(newer.tv_usec-older.tv_usec)/1000000.0;
128 /* return the number of seconds in the given input timeval struct */
129 long Curl_tvlong(struct timeval t1)
131 return t1.tv_sec;