tobicore  7.0.0
 All Classes Functions Variables Typedefs Enumerator Friends Groups Pages
TCTime.cpp
1 /*
2  Copyright (C) 2011 Francesco Leotta <francescoleotta@hotmail.com>
3  Copyright (C) 2009-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
4  Michele Tavella <michele.tavella@epfl.ch>
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "TCTime.hpp"
21 #undef _WIN32
22 
23 #ifdef __MINGW32__
24 #undef _WIN32
25 #elif WIN32
26 #define _WIN32
27 #endif //__MINGW32__
28 
29 #ifdef __MINGW32__
30 #include <time.h>
31 #include <windows.h>
32 #endif
33 
34 #ifndef __MINGW32__
35 #ifdef _WIN32
36 #include <time.h>
37 #include <WinSock2.h>
38 
39 int gettimeofday (struct timeval *tv, struct timezone *tz) {
40  FILETIME ft;
41  unsigned __int64 tmpres = 0;
42  static int tzflag;
43 
44  if (NULL != tv) {
45  GetSystemTimeAsFileTime(&ft);
46 
47  tmpres |= ft.dwHighDateTime;
48  tmpres <<= 32;
49  tmpres |= ft.dwLowDateTime;
50 
51  // converting file time to unix epoch
52  tmpres -= 11644473600000000ULL;
53  tmpres /= 10; // convert into microseconds
54  tv->tv_sec = (long)(tmpres / 1000000UL);
55  tv->tv_usec = (long)(tmpres % 1000000UL);
56  }
57 
58  if (NULL != tz) {
59  if (!tzflag) {
60  _tzset();
61  tzflag++;
62  }
63  tz->tz_minuteswest = _timezone / 60;
64  tz->tz_dsttime = _daylight;
65  }
66 
67  return 0;
68 }
69 
70 //void timerclear(struct timeval *tvp) {
71 // ZeroMemory(tvp, sizeof(timeval));
72 //}
73 
74 //int timerisset(struct timeval *tvp) {
75 // if (tvp->tv_sec || tvp->tv_usec)
76 // return 1;
77 // else
78 // return 0;
79 //}
80 #endif
81 #endif
82 
83 void TCSleep(double ms) {
84  timeval tm;
85  tm.tv_sec = 0;
86  tm.tv_usec = (long)1000*ms;
87 #ifdef __MINGW32__
88  Sleep(ms);
89 #else
90  select(0, 0, 0, 0, &tm);
91 #endif
92 }