1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, 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 http://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#include "tool_setup.h"
23
24#include "tool_util.h"
25
26#include "memdebug.h" /* keep this as LAST include */
27
28#if defined(WIN32) && !defined(MSDOS)
29
30struct timeval tool_tvnow(void)
31{
32  /*
33  ** GetTickCount() is available on _all_ Windows versions from W95 up
34  ** to nowadays. Returns milliseconds elapsed since last system boot,
35  ** increases monotonically and wraps once 49.7 days have elapsed.
36  */
37  struct timeval now;
38  DWORD milliseconds = GetTickCount();
39  now.tv_sec = milliseconds / 1000;
40  now.tv_usec = (milliseconds % 1000) * 1000;
41  return now;
42}
43
44#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
45
46struct timeval tool_tvnow(void)
47{
48  /*
49  ** clock_gettime() is granted to be increased monotonically when the
50  ** monotonic clock is queried. Time starting point is unspecified, it
51  ** could be the system start-up time, the Epoch, or something else,
52  ** in any case the time starting point does not change once that the
53  ** system has started up.
54  */
55  struct timeval now;
56  struct timespec tsnow;
57  if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
58    now.tv_sec = tsnow.tv_sec;
59    now.tv_usec = tsnow.tv_nsec / 1000;
60  }
61  /*
62  ** Even when the configure process has truly detected monotonic clock
63  ** availability, it might happen that it is not actually available at
64  ** run-time. When this occurs simply fallback to other time source.
65  */
66#ifdef HAVE_GETTIMEOFDAY
67  else
68    (void)gettimeofday(&now, NULL);
69#else
70  else {
71    now.tv_sec = (long)time(NULL);
72    now.tv_usec = 0;
73  }
74#endif
75  return now;
76}
77
78#elif defined(HAVE_GETTIMEOFDAY)
79
80struct timeval tool_tvnow(void)
81{
82  /*
83  ** gettimeofday() is not granted to be increased monotonically, due to
84  ** clock drifting and external source time synchronization it can jump
85  ** forward or backward in time.
86  */
87  struct timeval now;
88  (void)gettimeofday(&now, NULL);
89  return now;
90}
91
92#else
93
94struct timeval tool_tvnow(void)
95{
96  /*
97  ** time() returns the value of time in seconds since the Epoch.
98  */
99  struct timeval now;
100  now.tv_sec = (long)time(NULL);
101  now.tv_usec = 0;
102  return now;
103}
104
105#endif
106
107/*
108 * Make sure that the first argument is the more recent time, as otherwise
109 * we'll get a weird negative time-diff back...
110 *
111 * Returns: the time difference in number of milliseconds.
112 */
113long tool_tvdiff(struct timeval newer, struct timeval older)
114{
115  return (newer.tv_sec-older.tv_sec)*1000+
116    (newer.tv_usec-older.tv_usec)/1000;
117}
118
119/*
120 * Same as tool_tvdiff but with full usec resolution.
121 *
122 * Returns: the time difference in seconds with subsecond resolution.
123 */
124double tool_tvdiff_secs(struct timeval newer, struct timeval older)
125{
126  if(newer.tv_sec != older.tv_sec)
127    return (double)(newer.tv_sec-older.tv_sec)+
128      (double)(newer.tv_usec-older.tv_usec)/1000000.0;
129  else
130    return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
131}
132
133/* return the number of seconds in the given input timeval struct */
134long tool_tvlong(struct timeval t1)
135{
136  return t1.tv_sec;
137}
138
139