copyright year update in the source header
[platform/upstream/curl.git] / lib / timeval.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2003, 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  * $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 #ifdef WITHOUT_MM_LIB
36   SYSTEMTIME st;
37   time_t tt;
38   struct tm tmtm;
39   /* mktime converts local to UTC */
40   GetLocalTime (&st);
41   tmtm.tm_sec = st.wSecond;
42   tmtm.tm_min = st.wMinute;
43   tmtm.tm_hour = st.wHour;
44   tmtm.tm_mday = st.wDay;
45   tmtm.tm_mon = st.wMonth - 1;
46   tmtm.tm_year = st.wYear - 1900;
47   tmtm.tm_isdst = -1;
48   tt = mktime (&tmtm);
49   tp->tv_sec = tt;
50   tp->tv_usec = st.wMilliseconds * 1000;
51 #else
52   /**
53    ** The earlier time calculations using GetLocalTime
54    ** had a time resolution of 10ms.The timeGetTime, part
55    ** of multimedia apis offer a better time resolution
56    ** of 1ms.Need to link against winmm.lib for this
57    **/
58   unsigned long Ticks = 0;
59   unsigned long Sec =0;
60   unsigned long Usec = 0;
61   Ticks = timeGetTime();
62
63   Sec = Ticks/1000;
64   Usec = (Ticks - (Sec*1000))*1000;
65   tp->tv_sec = Sec;
66   tp->tv_usec = Usec;
67 #endif
68   return 1;
69 }
70 #define HAVE_GETTIMEOFDAY
71 #endif
72 #endif
73
74 struct timeval Curl_tvnow (void)
75 {
76  struct timeval now;
77 #ifdef HAVE_GETTIMEOFDAY
78  gettimeofday (&now, NULL);
79 #else
80  now.tv_sec = (long) time(NULL);
81  now.tv_usec = 0;
82 #endif
83  return now;
84 }
85
86 /*
87  * Make sure that the first argument is the more recent time, as otherwise
88  * we'll get a weird negative time-diff back...
89  */
90 long Curl_tvdiff (struct timeval newer, struct timeval older)
91 {
92   return (newer.tv_sec-older.tv_sec)*1000+
93     (499+newer.tv_usec-older.tv_usec)/1000;
94 }
95
96 long Curl_tvlong (struct timeval t1)
97 {
98  return t1.tv_sec;
99 }
100
101 /*
102  * local variables:
103  * eval: (load-file "../curl-mode.el")
104  * end:
105  * vim600: fdm=marker
106  * vim: et sw=2 ts=2 sts=2 tw=78
107  */