made Curl_tvdiff round the diff better and make the subtraction before
[platform/upstream/curl.git] / lib / timeval.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * In order to be useful for every potential user, curl and libcurl are
11  * dual-licensed under the MPL and the MIT/X-derivate licenses.
12  *
13  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
14  * copies of the Software, and permit persons to whom the Software is
15  * furnished to do so, under the terms of the MPL or the MIT/X-derivate
16  * licenses. You may pick one of these licenses.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  *****************************************************************************/
23
24 #ifdef WIN32
25 #include <windows.h>
26 #endif
27 #include "timeval.h"
28
29 #ifndef HAVE_GETTIMEOFDAY
30
31 #ifdef WIN32
32 int
33 gettimeofday (struct timeval *tp, void *nothing)
34 {
35  SYSTEMTIME st;
36  time_t tt;
37  struct tm tmtm;
38  /* mktime converts local to UTC */
39  GetLocalTime (&st);
40  tmtm.tm_sec = st.wSecond;
41  tmtm.tm_min = st.wMinute;
42  tmtm.tm_hour = st.wHour;
43  tmtm.tm_mday = st.wDay;
44  tmtm.tm_mon = st.wMonth - 1;
45  tmtm.tm_year = st.wYear - 1900;
46  tmtm.tm_isdst = -1;
47  tt = mktime (&tmtm);
48  tp->tv_sec = tt;
49  tp->tv_usec = st.wMilliseconds * 1000;
50  return 1;
51 }
52 #define HAVE_GETTIMEOFDAY
53 #endif
54 #endif
55
56 struct timeval Curl_tvnow (void)
57 {
58  struct timeval now;
59 #ifdef HAVE_GETTIMEOFDAY
60  gettimeofday (&now, NULL);
61 #else
62  now.tv_sec = (long) time(NULL);
63  now.tv_usec = 0;
64 #endif
65  return now;
66 }
67
68 /*
69  * Make sure that the first argument is the more recent time, as otherwise
70  * we'll get a weird negative time-diff back...
71  */
72 long Curl_tvdiff (struct timeval newer, struct timeval older)
73 {
74   return (newer.tv_sec-older.tv_sec)*1000+
75     (499+newer.tv_usec-older.tv_usec)/1000;
76 }
77
78 long Curl_tvlong (struct timeval t1)
79 {
80  return t1.tv_sec;
81 }
82
83 /*
84  * local variables:
85  * eval: (load-file "../curl-mode.el")
86  * end:
87  * vim600: fdm=marker
88  * vim: et sw=2 ts=2 sts=2 tw=78
89  */