# timeval.h

    00001 /*
    00002  * timeval.h    1.3 2003/01/14
    00003  *
    00004  * Defines gettimeofday, timeval, etc. for Win32
    00005  *
    00006  * By Wu Yongwei
    00007  *
    00008  */
    00009
    00010 #ifndef _TIMEVAL_H
    00011 #define _TIMEVAL_H
    00012
    00013 #ifndef WIN32_LEAN_AND_MEAN
    00014 #define WIN32_LEAN_AND_MEAN
    00015 #endif
    00016
    00017 #include <winsock2.h>
    00018 #include <time.h>
    00019
    00020 #if defined(_MSC_VER) || defined(__BORLANDC__)
    00021 #define EPOCHFILETIME (116444736000000000i64)
    00022 #else
    00023 #define EPOCHFILETIME (116444736000000000LL)
    00024 #endif
    00025
    00026 struct timezone {
    00027     int tz_minuteswest; /* minutes W of Greenwich */
    00028     int tz_dsttime;     /* type of dst correction */
    00029 };
    00030
    00031
    00032 #if !defined(_WIN32_WCE)
    00033
    00034 __inline int gettimeofday(struct timeval *tv, struct timezone *tz)
    00035 {
    00036     FILETIME        ft;
    00037     LARGE_INTEGER   li;
    00038     __int64         t;
    00039     static int      tzflag;
    00040
    00041     if (tv)
    00042     {
    00043         GetSystemTimeAsFileTime(&ft);
    00044         li.LowPart  = ft.dwLowDateTime;
    00045         li.HighPart = ft.dwHighDateTime;
    00046         t  = li.QuadPart;       /* In 100-nanosecond intervals */
    00047         t -= EPOCHFILETIME;     /* Offset to the Epoch time */
    00048         t /= 10;                /* In microseconds */
    00049         tv->tv_sec  = (long)(t / 1000000);
    00050         tv->tv_usec = (long)(t % 1000000);
    00051     }
    00052
    00053     if (tz)
    00054     {
    00055         if (!tzflag)
    00056         {
    00057             _tzset();
    00058             tzflag++;
    00059         }
    00060         tz->tz_minuteswest = _timezone / 60;
    00061         tz->tz_dsttime = _daylight;
    00062     }
    00063
    00064     return 0;
    00065 }
    00066
    00067 #else
    00068
    00069 __inline int gettimeofday(struct timeval *tv, struct timezone *tz)
    00070 {
    00071         SYSTEMTIME      st;
    00072     FILETIME        ft;
    00073     LARGE_INTEGER   li;
    00074     TIME_ZONE_INFORMATION tzi;
    00075     __int64         t;
    00076     static int      tzflag;
    00077
    00078     if (tv)
    00079     {
    00080                 GetSystemTime(&st);
    00081                 SystemTimeToFileTime(&st, &ft);
    00082         li.LowPart  = ft.dwLowDateTime;
    00083         li.HighPart = ft.dwHighDateTime;
    00084         t  = li.QuadPart;       /* In 100-nanosecond intervals */
    00085         t -= EPOCHFILETIME;     /* Offset to the Epoch time */
    00086         t /= 10;                /* In microseconds */
    00087         tv->tv_sec  = (long)(t / 1000000);
    00088         tv->tv_usec = (long)(t % 1000000);
    00089     }
    00090
    00091     if (tz)
    00092     {
    00093         GetTimeZoneInformation(&tzi);
    00094
    00095         tz->tz_minuteswest = tzi.Bias;
    00096                 if (tzi.StandardDate.wMonth != 0)
    00097         {
    00098                         tz->tz_minuteswest += tzi.StandardBias * 60;
    00099         }
    00100
    00101         if (tzi.DaylightDate.wMonth != 0)
    00102         {
    00103             tz->tz_dsttime = 1;
    00104         }
    00105         else
    00106         {
    00107             tz->tz_dsttime = 0;
    00108         }
    00109     }
    00110
    00111     return 0;
    00112 }
    00113
    00114 #endif /* _WIN32_WCE */
    00115
    00116 #endif /* _TIMEVAL_H */