Time.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #ifndef COIL_TIME_H
00020 #define COIL_TIME_H
00021 
00022 
00023 #include <windows.h>
00024 #include <winsock.h>
00025 //#include <winsock2.h>
00026 //#pragma comment(lib, "WS2_32.LIB")
00027 #include <time.h>
00028 #include <coil/config_coil.h>
00029 #include <coil/TimeValue.h>
00030 
00031 namespace coil
00032 {
00033         #define EPOCHFILETIME (116444736000000000i64)
00034 struct timezone {
00035           int tz_minuteswest;
00036           int tz_dsttime;
00037   };
00038 
00060   inline unsigned int sleep(unsigned int seconds)
00061   {
00062 
00063     ::Sleep( seconds *1000 );
00064     return 0;
00065   }
00066 
00067 //static short m_time_DLLinit_count = 0;
00068 
00090   inline int sleep(TimeValue interval)
00091   {
00092     struct timeval tv;
00093     WSADATA wsa;
00094     SOCKET ssoc;
00095     fd_set mask;
00096     WORD ver;
00097     int iret;
00098 
00099     //The WSAStartup function initiates use of the Winsock DLL by a process.
00100     ver = MAKEWORD(2,2);
00101     iret = ::WSAStartup(ver,&wsa);
00102     if( iret != 0 ) 
00103     {
00104         return iret;
00105     }
00106 
00107     //The socket function creates a socket that is bound to a specific transport service provider.
00108     ssoc = ::socket(AF_INET,      //It is assumed AF_INET because there is no AF_UNIX for Windows.
00109                     SOCK_STREAM,
00110                     0);
00111     if(ssoc==INVALID_SOCKET){
00112       iret = ::WSAGetLastError();
00113       ::WSACleanup();
00114       return iret;  
00115     }
00116 
00117 
00118     //Initialize fd_set. 
00119     FD_ZERO(&mask);
00120     //Register the reading socket.
00121     FD_SET(ssoc,&mask);
00122 
00123     tv.tv_sec = interval.sec();
00124     tv.tv_usec = interval.usec();
00125     iret = ::select((int)ssoc+1, &mask, NULL, NULL, &tv);
00126     if( iret == SOCKET_ERROR ) 
00127     {
00128       iret = ::WSAGetLastError();
00129       //The closesocket function closes an existing socket.
00130       ::closesocket(ssoc);
00131       //The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
00132       ::WSACleanup();
00133       return iret;  
00134     }
00135 
00136     //The closesocket function closes an existing socket.
00137     ::closesocket(ssoc);
00138 
00139     //The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
00140     ::WSACleanup();
00141     return iret;
00142   }
00143 
00165   inline int usleep(unsigned int usec)
00166   {
00167     struct timeval tv;
00168     int iret;
00169     WORD ver;
00170     WSADATA wsa;
00171     fd_set mask;
00172     SOCKET ssoc;
00173 
00174     //The WSAStartup function initiates use of the Winsock DLL by a process.
00175     ver = MAKEWORD(2,2);
00176     iret = ::WSAStartup(ver,&wsa);
00177     if( iret != 0 ) 
00178     {
00179         return iret;
00180     }
00181 
00182     //The socket function creates a socket that is bound to a specific transport service provider.
00183     ssoc = ::socket(AF_INET,SOCK_STREAM,0);
00184     if(ssoc==INVALID_SOCKET){
00185       iret = ::WSAGetLastError();
00186       ::WSACleanup();
00187       return iret;  
00188     }
00189     FD_ZERO(&mask);
00190     FD_SET(ssoc,&mask);
00191     
00192     tv.tv_sec = usec / 1000000;
00193     tv.tv_usec = usec % 1000000;
00194     iret = ::select((int)ssoc+1, &mask, NULL, NULL, &tv);
00195     if( iret == SOCKET_ERROR ) 
00196     {
00197       iret = ::WSAGetLastError();
00198       //The closesocket function closes an existing socket.
00199       ::closesocket(ssoc);
00200       //The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
00201       ::WSACleanup();
00202       return iret;  
00203     }
00204     //The closesocket function closes an existing socket.
00205     ::closesocket(ssoc);
00206     //The WSACleanup function terminates use of the Winsock 2 DLL (Ws2_32.dll).
00207     ::WSACleanup();
00208     return iret;
00209 
00210   }
00211 
00235   inline int gettimeofday(struct timeval *tv, struct timezone *tz)
00236   {
00237           FILETIME        ftime;
00238           LARGE_INTEGER   lint;
00239           __int64         val64;
00240           static int      tzflag;
00241           if (tv != NULL)
00242           {
00243                   ::GetSystemTimeAsFileTime(&ftime);
00244                   lint.LowPart  = ftime.dwLowDateTime;
00245                   lint.HighPart = ftime.dwHighDateTime;
00246                   val64 = lint.QuadPart;
00247                   val64 = val64 - EPOCHFILETIME;
00248                   val64 = val64 / 10;
00249                   tv->tv_sec  = (long)(val64 / 1000000);
00250                   tv->tv_usec = (long)(val64 % 1000000);
00251           }
00252           if (tz)
00253           {
00254                   if (!tzflag)
00255                   {
00256                           ::_tzset();
00257                           ++tzflag;
00258                   }
00259                   long tzone = 0;
00260                   ::_get_timezone(&tzone);
00261                   tz->tz_minuteswest = tzone / 60;
00262                   int dlight = 0;
00263                   ::_get_daylight(&dlight);
00264                   tz->tz_dsttime = dlight;
00265           }
00266           return 0;
00267   }
00268 
00286   inline TimeValue gettimeofday()
00287   {
00288     timeval tv;
00289     coil::gettimeofday(&tv, 0);
00290     return TimeValue(tv.tv_sec, tv.tv_usec);
00291   }
00292 
00316   inline int settimeofday(const struct timeval *tv , const struct timezone *tz)
00317   {
00318 
00319           SYSTEMTIME systime;
00320           FILETIME ftime;
00321           LARGE_INTEGER lint;
00322           __int64 val64;
00323           int bias(0);
00324 
00325         // tv,tz -> ftime
00326         if (tv != NULL)
00327         {
00328                 if (tz != NULL)
00329                 {
00330                         bias = tz->tz_minuteswest;
00331                 }
00332 
00333                 val64 = (tv->tv_sec + bias * 60) * 1000000;
00334                 val64 = val64 + tv->tv_usec;
00335                 lint.QuadPart = val64;
00336                 ftime.dwHighDateTime = lint.LowPart;
00337                 ftime.dwHighDateTime = lint.HighPart;
00338                 ::FileTimeToSystemTime(&ftime, &systime);
00339                 ::SetSystemTime(&systime);
00340         }
00341 
00342     return 0;
00343   }
00344 
00345 
00346 };
00347 
00348 #endif // COIL_TIME_H


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:39