lemon/bits/mingw32_time.cc
changeset 2199 1229af45cc69
child 2391 14a343be7a5a
equal deleted inserted replaced
-1:000000000000 0:c6bd2a89a3fd
       
     1 #ifdef WIN32
       
     2 
       
     3 #include <lemon/bits/mingw32_time.h>
       
     4 
       
     5 #include <windows.h>
       
     6 #include <ctime>
       
     7 #include "dos.h"
       
     8 
       
     9 static const char days[] = 
       
    10 "Sun Mon Tue Wed Thu Fri Sat ";
       
    11 static const char months[] = 
       
    12 "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
       
    13 
       
    14 void num2str(char *c,int i) {
       
    15   c[0]=i/10+'0';
       
    16   c[1]=i%10+'0';
       
    17 }
       
    18 
       
    19 char *asctime_r(const struct tm *t, char *buf) {
       
    20   *(int*)buf=*(int*)(days+(t->tm_wday<<2));
       
    21   *(int*)(buf+4)=*(int*)(months+(t->tm_mon<<2));
       
    22   num2str(buf+8,t->tm_mday);
       
    23   if (buf[8]=='0') buf[8]=' ';
       
    24   buf[10]=' ';
       
    25   num2str(buf+11,t->tm_hour);
       
    26   buf[13]=':';
       
    27   num2str(buf+14,t->tm_min);
       
    28   buf[16]=':';
       
    29   num2str(buf+17,t->tm_sec);
       
    30   buf[19]=' ';
       
    31   num2str(buf+20,(t->tm_year+1900)/100);
       
    32   num2str(buf+22,(t->tm_year+1900)%100);
       
    33   buf[24]='\n'; buf[25]='\0';
       
    34   return buf;
       
    35 }
       
    36 
       
    37 struct tm * localtime_r (const time_t *t, struct tm *tm) {
       
    38   struct tm *tmp;
       
    39   
       
    40   if ((tmp = localtime(t)) && tm)
       
    41     *tm = *tmp;
       
    42   else
       
    43     return 0;
       
    44   
       
    45   return tm;
       
    46 }
       
    47 
       
    48 char *ctime_r(const time_t * tim_p , char * result) {
       
    49   struct tm tm;
       
    50   return asctime_r (localtime_r (tim_p, &tm), result);
       
    51 }
       
    52 
       
    53 
       
    54 int gettimeofday(struct timeval * tp, struct timezone *) {
       
    55   SYSTEMTIME systime;
       
    56 
       
    57   if (tp) {
       
    58     struct tm tmrec;
       
    59     time_t theTime = time(NULL);
       
    60     
       
    61     
       
    62     tmrec = *localtime(&theTime);
       
    63     tp->tv_sec = mktime(&tmrec);
       
    64     GetLocalTime(&systime); /* system time */
       
    65 
       
    66     tp->tv_usec = systime.wMilliseconds * 1000;
       
    67   }
       
    68   return 0;
       
    69 }
       
    70 
       
    71 long filetime_to_clock(FILETIME *ft)
       
    72 {
       
    73   __int64 qw = ft->dwHighDateTime;
       
    74   qw <<= 32;
       
    75   qw |= ft->dwLowDateTime;
       
    76   qw /= 10000;  
       
    77   return (long) qw;
       
    78 
       
    79 }
       
    80 
       
    81 int times(struct tms *tmbuf)
       
    82 {
       
    83   FILETIME create, exit, kernel, user;
       
    84   if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
       
    85     tmbuf->tms_utime = filetime_to_clock(&user);
       
    86     tmbuf->tms_stime = filetime_to_clock(&kernel);
       
    87     tmbuf->tms_cutime = 0;
       
    88     tmbuf->tms_cstime = 0;
       
    89   }
       
    90   else {
       
    91     tmbuf->tms_utime = clock();
       
    92     tmbuf->tms_stime = 0;
       
    93     tmbuf->tms_cutime = 0;
       
    94     tmbuf->tms_cstime = 0;
       
    95   }
       
    96   return 0;
       
    97 }
       
    98 
       
    99 int sysconf(int)
       
   100 {
       
   101   return 1;
       
   102 }
       
   103 
       
   104 #endif