timeval.h

00001 /**********************************************************************
00002  *
00003  * GEOS - Geometry Engine Open Source
00004  * http://geos.osgeo.org
00005  *
00006  * Copyright (C) 2006 Wu Yongwei
00007  *
00008  * This is free software; you can redistribute and/or modify it under
00009  * the terms of the GNU Lesser General Public Licence as published
00010  * by the Free Software Foundation. 
00011  * See the COPYING file for more information.
00012  *
00013  * Note: This code is in the public domain, see 
00014  *       http://wyw.dcweb.cn/time.htm
00015  *
00016  **********************************************************************/
00017 
00018 #ifndef GEOS_TIMEVAL_H
00019 #define GEOS_TIMEVAL_H
00020 
00021 #ifndef WIN32_LEAN_AND_MEAN
00022 #define WIN32_LEAN_AND_MEAN
00023 #endif
00024 
00025 #include <winsock2.h>
00026 #include <time.h>
00027 
00028 #if defined(_MSC_VER) || defined(__BORLANDC__)
00029 #define EPOCHFILETIME (116444736000000000i64)
00030 #else
00031 #define EPOCHFILETIME (116444736000000000LL)
00032 #endif
00033 
00034 struct timezone {
00035     int tz_minuteswest; /* minutes W of Greenwich */
00036     int tz_dsttime;     /* type of dst correction */
00037 };
00038 
00039 
00040 #if !defined(_WIN32_WCE)
00041 
00042 __inline int gettimeofday(struct timeval *tv, struct timezone *tz)
00043 {
00044     FILETIME        ft;
00045     LARGE_INTEGER   li;
00046     __int64         t;
00047     static int      tzflag;
00048 
00049     if (tv)
00050     {
00051         GetSystemTimeAsFileTime(&ft);
00052         li.LowPart  = ft.dwLowDateTime;
00053         li.HighPart = ft.dwHighDateTime;
00054         t  = li.QuadPart;       /* In 100-nanosecond intervals */
00055         t -= EPOCHFILETIME;     /* Offset to the Epoch time */
00056         t /= 10;                /* In microseconds */
00057         tv->tv_sec  = (long)(t / 1000000);
00058         tv->tv_usec = (long)(t % 1000000);
00059     }
00060 
00061     if (tz)
00062     {
00063         if (!tzflag)
00064         {
00065             _tzset();
00066             tzflag++;
00067         }
00068         tz->tz_minuteswest = _timezone / 60;
00069         tz->tz_dsttime = _daylight;
00070     }
00071 
00072     return 0;
00073 }
00074 
00075 #else
00076 
00077 __inline int gettimeofday(struct timeval *tv, struct timezone *tz)
00078 {
00079         SYSTEMTIME      st;
00080     FILETIME        ft;
00081     LARGE_INTEGER   li;
00082     TIME_ZONE_INFORMATION tzi;
00083     __int64         t;
00084     static int      tzflag;
00085 
00086     if (tv)
00087     {
00088                 GetSystemTime(&st);
00089                 SystemTimeToFileTime(&st, &ft);
00090         li.LowPart  = ft.dwLowDateTime;
00091         li.HighPart = ft.dwHighDateTime;
00092         t  = li.QuadPart;       /* In 100-nanosecond intervals */
00093         t -= EPOCHFILETIME;     /* Offset to the Epoch time */
00094         t /= 10;                /* In microseconds */
00095         tv->tv_sec  = (long)(t / 1000000);
00096         tv->tv_usec = (long)(t % 1000000);
00097     }
00098 
00099     if (tz)
00100     {   
00101         GetTimeZoneInformation(&tzi);
00102                 
00103         tz->tz_minuteswest = tzi.Bias;
00104                 if (tzi.StandardDate.wMonth != 0)
00105         {
00106                         tz->tz_minuteswest += tzi.StandardBias * 60;
00107         }
00108 
00109         if (tzi.DaylightDate.wMonth != 0)
00110         {
00111             tz->tz_dsttime = 1;
00112         }
00113         else
00114         {
00115             tz->tz_dsttime = 0;
00116         }
00117     }
00118 
00119     return 0;
00120 }
00121 
00122 #endif /* _WIN32_WCE */
00123 
00124 #endif /* GEOS_TIMEVAL_H */

Generated on 15 May 2014 for GEOS by  doxygen 1.4.7