win32/coil/Time.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef COIL_TIME_H
20 #define COIL_TIME_H
21 
22 
23 #include <windows.h>
24 #include <winsock.h>
25 //#include <winsock2.h>
26 //#pragma comment(lib, "WS2_32.LIB")
27 #include <time.h>
28 #include <coil/config_coil.h>
29 #include <coil/TimeValue.h>
30 
31 namespace coil
32 {
33  #define EPOCHFILETIME (116444736000000000i64)
34 struct timezone {
37  };
38 
60  inline unsigned int sleep(unsigned int seconds)
61  {
62 
63  ::Sleep( seconds *1000 );
64  return 0;
65  }
66 
67 //static short m_time_DLLinit_count = 0;
68 
90  inline int sleep(TimeValue interval)
91  {
92  struct timeval tv;
93  WSADATA wsa;
94  SOCKET ssoc;
95  fd_set mask;
96  WORD ver;
97  int iret;
98 
99  //The WSAStartup function initiates use of the Winsock DLL by a process.
100  ver = MAKEWORD(2,2);
101  iret = ::WSAStartup(ver,&wsa);
102  if( iret != 0 )
103  {
104  return iret;
105  }
106 
107  //The socket function creates a socket that is bound to a specific transport service provider.
108  ssoc = ::socket(AF_INET, //It is assumed AF_INET because there is no AF_UNIX for Windows.
109  SOCK_STREAM,
110  0);
111  if(ssoc==INVALID_SOCKET){
112  iret = ::WSAGetLastError();
113  ::WSACleanup();
114  return iret;
115  }
116 
117 
118  //Initialize fd_set.
119  FD_ZERO(&mask);
120  //Register the reading socket.
121  FD_SET(ssoc,&mask);
122 
123  tv.tv_sec = interval.sec();
124  tv.tv_usec = interval.usec();
125  iret = ::select((int)ssoc+1, &mask, NULL, NULL, &tv);
126  if( iret == SOCKET_ERROR )
127  {
128  iret = ::WSAGetLastError();
129  //The closesocket function closes an existing socket.
130  ::closesocket(ssoc);
131  //The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
132  ::WSACleanup();
133  return iret;
134  }
135 
136  //The closesocket function closes an existing socket.
137  ::closesocket(ssoc);
138 
139  //The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
140  ::WSACleanup();
141  return iret;
142  }
143 
165  inline int usleep(unsigned int usec)
166  {
167  struct timeval tv;
168  int iret;
169  WORD ver;
170  WSADATA wsa;
171  fd_set mask;
172  SOCKET ssoc;
173 
174  //The WSAStartup function initiates use of the Winsock DLL by a process.
175  ver = MAKEWORD(2,2);
176  iret = ::WSAStartup(ver,&wsa);
177  if( iret != 0 )
178  {
179  return iret;
180  }
181 
182  //The socket function creates a socket that is bound to a specific transport service provider.
183  ssoc = ::socket(AF_INET,SOCK_STREAM,0);
184  if(ssoc==INVALID_SOCKET){
185  iret = ::WSAGetLastError();
186  ::WSACleanup();
187  return iret;
188  }
189  FD_ZERO(&mask);
190  FD_SET(ssoc,&mask);
191 
192  tv.tv_sec = usec / 1000000;
193  tv.tv_usec = usec % 1000000;
194  iret = ::select((int)ssoc+1, &mask, NULL, NULL, &tv);
195  if( iret == SOCKET_ERROR )
196  {
197  iret = ::WSAGetLastError();
198  //The closesocket function closes an existing socket.
199  ::closesocket(ssoc);
200  //The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
201  ::WSACleanup();
202  return iret;
203  }
204  //The closesocket function closes an existing socket.
205  ::closesocket(ssoc);
206  //The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
207  ::WSACleanup();
208  return iret;
209 
210  }
211 
235  inline int gettimeofday(struct timeval *tv, struct timezone *tz)
236  {
237  FILETIME ftime;
238  LARGE_INTEGER lint;
239  __int64 val64;
240  static int tzflag;
241  if (tv != NULL)
242  {
243  ::GetSystemTimeAsFileTime(&ftime);
244  lint.LowPart = ftime.dwLowDateTime;
245  lint.HighPart = ftime.dwHighDateTime;
246  val64 = lint.QuadPart;
247  val64 = val64 - EPOCHFILETIME;
248  val64 = val64 / 10;
249  tv->tv_sec = (long)(val64 / 1000000);
250  tv->tv_usec = (long)(val64 % 1000000);
251  }
252  if (tz)
253  {
254  if (!tzflag)
255  {
256  ::_tzset();
257  ++tzflag;
258  }
259  long tzone = 0;
260  ::_get_timezone(&tzone);
261  tz->tz_minuteswest = tzone / 60;
262  int dlight = 0;
263  ::_get_daylight(&dlight);
264  tz->tz_dsttime = dlight;
265  }
266  return 0;
267  }
268 
286  inline TimeValue gettimeofday()
287  {
288  timeval tv;
289  coil::gettimeofday(&tv, 0);
290  return TimeValue(tv.tv_sec, tv.tv_usec);
291  }
292 
316  inline int settimeofday(const struct timeval *tv , const struct timezone *tz)
317  {
318 
319  SYSTEMTIME systime;
320  FILETIME ftime;
321  LARGE_INTEGER lint;
322  __int64 val64;
323  int bias(0);
324 
325  // tv,tz -> ftime
326  if (tv != NULL)
327  {
328  if (tz != NULL)
329  {
330  bias = tz->tz_minuteswest;
331  }
332 
333  val64 = (tv->tv_sec + bias * 60) * 1000000;
334  val64 = val64 + tv->tv_usec;
335  lint.QuadPart = val64;
336  ftime.dwHighDateTime = lint.LowPart;
337  ftime.dwHighDateTime = lint.HighPart;
338  ::FileTimeToSystemTime(&ftime, &systime);
339  ::SetSystemTime(&systime);
340  }
341 
342  return 0;
343  }
344 
345 
346 };
347 
348 #endif // COIL_TIME_H
long int sec() const
Get value of second time scale.
Definition: TimeValue.h:110
unsigned int sleep(unsigned int seconds)
Stop a processing at specified second time.
Definition: ace/coil/Time.h:40
int gettimeofday(struct timeval *tv, struct timezone *tz)
Get the time and timezone.
Definition: ace/coil/Time.h:57
#define EPOCHFILETIME
int settimeofday(const struct timeval *tv, const struct timezone *tz)
Set the time and timezone.
Definition: ace/coil/Time.h:75
long int usec() const
Get value of micro second time scale.
Definition: TimeValue.h:131
int usleep(useconds_t usec)
Stop a processing at specified micro second time.
Definition: ace/coil/Time.h:51
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:26:00