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