lemon/bits/mingw32_time.h
changeset 2116 b6a68c15a6a3
parent 2028 d0e8a86a1ff2
child 2391 14a343be7a5a
equal deleted inserted replaced
0:881821095bc8 1:ab78108ea968
    20 #define LEMON_BITS_MINGW32_TIME_H
    20 #define LEMON_BITS_MINGW32_TIME_H
    21 
    21 
    22 #ifdef WIN32
    22 #ifdef WIN32
    23 
    23 
    24 #include <windows.h>
    24 #include <windows.h>
    25 #include <time.h>
    25 #include <ctime>
    26 #include "dos.h"
    26 #include "dos.h"
    27 
    27 
    28 static const char days[] = 
    28 char *asctime_r(const struct tm *t, char *buf);
    29 "Sun Mon Tue Wed Thu Fri Sat ";
    29 struct tm * localtime_r (const time_t *t, struct tm *tm);
    30 static const char months[] = 
    30 char *ctime_r(const time_t * tim_p , char * result);
    31 "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
    31 int gettimeofday(struct timeval * tp, struct timezone *);
    32 
       
    33 inline void num2str(char *c,int i) {
       
    34   c[0]=i/10+'0';
       
    35   c[1]=i%10+'0';
       
    36 }
       
    37 
       
    38 inline char *asctime_r(const struct tm *t, char *buf) {
       
    39   /* "Wed Jun 30 21:49:08 1993\n" */
       
    40   *(int*)buf=*(int*)(days+(t->tm_wday<<2));
       
    41   *(int*)(buf+4)=*(int*)(months+(t->tm_mon<<2));
       
    42   num2str(buf+8,t->tm_mday);
       
    43   if (buf[8]=='0') buf[8]=' ';
       
    44   buf[10]=' ';
       
    45   num2str(buf+11,t->tm_hour);
       
    46   buf[13]=':';
       
    47   num2str(buf+14,t->tm_min);
       
    48   buf[16]=':';
       
    49   num2str(buf+17,t->tm_sec);
       
    50   buf[19]=' ';
       
    51   num2str(buf+20,(t->tm_year+1900)/100);
       
    52   num2str(buf+22,(t->tm_year+1900)%100);
       
    53   buf[24]='\n'; buf[25]='\0';
       
    54   return buf;
       
    55 }
       
    56 
       
    57 inline struct tm * localtime_r (const time_t *t, struct tm *tm) {
       
    58   struct tm *tmp;
       
    59   
       
    60   if ((tmp = localtime(t)) && tm)
       
    61     *tm = *tmp;
       
    62   else
       
    63     return 0;
       
    64   
       
    65   return tm;
       
    66 }
       
    67 
       
    68 inline char *ctime_r(const time_t * tim_p , char * result) {
       
    69   struct tm tm;
       
    70   return asctime_r (localtime_r (tim_p, &tm), result);
       
    71 }
       
    72 
       
    73 
       
    74 inline int gettimeofday(struct timeval * tp, struct timezone *) {
       
    75   SYSTEMTIME systime;
       
    76 
       
    77   if (tp) {
       
    78     struct tm tmrec;
       
    79     time_t theTime = time(NULL);
       
    80     
       
    81     
       
    82     tmrec = *localtime(&theTime);
       
    83     tp->tv_sec = mktime(&tmrec);
       
    84     GetLocalTime(&systime); /* system time */
       
    85 
       
    86     tp->tv_usec = systime.wMilliseconds * 1000;
       
    87   }
       
    88   return 0;
       
    89 }
       
    90 
       
    91 
    32 
    92 struct tms {
    33 struct tms {
    93   long	tms_utime;
    34   long	tms_utime;
    94   long	tms_stime;
    35   long	tms_stime;
    95   long	tms_cutime;
    36   long	tms_cutime;
    96   long	tms_cstime;
    37   long	tms_cstime;
    97 };
    38 };
    98 
    39 
    99 inline long filetime_to_clock(FILETIME *ft)
    40 long filetime_to_clock(FILETIME *ft);
   100 {
       
   101   __int64 qw = ft->dwHighDateTime;
       
   102   qw <<= 32;
       
   103   qw |= ft->dwLowDateTime;
       
   104   qw /= 10000;  /* File time ticks at 0.1uS, clock at 1mS */
       
   105   return (long) qw;
       
   106 
    41 
   107 }
    42 int times(struct tms *tmbuf);
   108 
       
   109 inline int times(struct tms *tmbuf)
       
   110 {
       
   111   FILETIME create, exit, kernel, user;
       
   112   if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
       
   113     tmbuf->tms_utime = filetime_to_clock(&user);
       
   114     tmbuf->tms_stime = filetime_to_clock(&kernel);
       
   115     tmbuf->tms_cutime = 0;
       
   116     tmbuf->tms_cstime = 0;
       
   117   }
       
   118   else {
       
   119     tmbuf->tms_utime = clock();
       
   120     tmbuf->tms_stime = 0;
       
   121     tmbuf->tms_cutime = 0;
       
   122     tmbuf->tms_cstime = 0;
       
   123   }
       
   124   return 0;
       
   125 }
       
   126 
    43 
   127 #define _SC_CLK_TCK 1
    44 #define _SC_CLK_TCK 1
   128 
    45 
   129 inline int sysconf(int)
    46 int sysconf(int);
   130 {
       
   131   return 1;
       
   132 }
       
   133 
    47 
   134 #endif
    48 #endif
   135 
    49 
   136 
       
   137 #endif
    50 #endif