lemon/bits/mingw32_time.cc
author deba
Tue, 22 Jul 2008 11:20:06 +0000
changeset 2616 02971275e7bf
parent 2391 14a343be7a5a
permissions -rw-r--r--
Back port bug fix from hg changeset [0915721396dc]
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifdef WIN32
    20 
    21 #include <lemon/bits/mingw32_time.h>
    22 
    23 #include <windows.h>
    24 #include <ctime>
    25 #include "dos.h"
    26 
    27 static const char days[] = 
    28 "Sun Mon Tue Wed Thu Fri Sat ";
    29 static const char months[] = 
    30 "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
    31 
    32 void num2str(char *c,int i) {
    33   c[0]=i/10+'0';
    34   c[1]=i%10+'0';
    35 }
    36 
    37 char *asctime_r(const struct tm *t, char *buf) {
    38   *(int*)buf=*(int*)(days+(t->tm_wday<<2));
    39   *(int*)(buf+4)=*(int*)(months+(t->tm_mon<<2));
    40   num2str(buf+8,t->tm_mday);
    41   if (buf[8]=='0') buf[8]=' ';
    42   buf[10]=' ';
    43   num2str(buf+11,t->tm_hour);
    44   buf[13]=':';
    45   num2str(buf+14,t->tm_min);
    46   buf[16]=':';
    47   num2str(buf+17,t->tm_sec);
    48   buf[19]=' ';
    49   num2str(buf+20,(t->tm_year+1900)/100);
    50   num2str(buf+22,(t->tm_year+1900)%100);
    51   buf[24]='\n'; buf[25]='\0';
    52   return buf;
    53 }
    54 
    55 struct tm * localtime_r (const time_t *t, struct tm *tm) {
    56   struct tm *tmp;
    57   
    58   if ((tmp = localtime(t)) && tm)
    59     *tm = *tmp;
    60   else
    61     return 0;
    62   
    63   return tm;
    64 }
    65 
    66 char *ctime_r(const time_t * tim_p , char * result) {
    67   struct tm tm;
    68   return asctime_r (localtime_r (tim_p, &tm), result);
    69 }
    70 
    71 
    72 int gettimeofday(struct timeval * tp, struct timezone *) {
    73   SYSTEMTIME systime;
    74 
    75   if (tp) {
    76     struct tm tmrec;
    77     time_t theTime = time(NULL);
    78     
    79     
    80     tmrec = *localtime(&theTime);
    81     tp->tv_sec = mktime(&tmrec);
    82     GetLocalTime(&systime); /* system time */
    83 
    84     tp->tv_usec = systime.wMilliseconds * 1000;
    85   }
    86   return 0;
    87 }
    88 
    89 long filetime_to_clock(FILETIME *ft)
    90 {
    91   __int64 qw = ft->dwHighDateTime;
    92   qw <<= 32;
    93   qw |= ft->dwLowDateTime;
    94   qw /= 10000;  
    95   return (long) qw;
    96 
    97 }
    98 
    99 int times(struct tms *tmbuf)
   100 {
   101   FILETIME create, exit, kernel, user;
   102   if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
   103     tmbuf->tms_utime = filetime_to_clock(&user);
   104     tmbuf->tms_stime = filetime_to_clock(&kernel);
   105     tmbuf->tms_cutime = 0;
   106     tmbuf->tms_cstime = 0;
   107   }
   108   else {
   109     tmbuf->tms_utime = clock();
   110     tmbuf->tms_stime = 0;
   111     tmbuf->tms_cutime = 0;
   112     tmbuf->tms_cstime = 0;
   113   }
   114   return 0;
   115 }
   116 
   117 int sysconf(int)
   118 {
   119   return 1;
   120 }
   121 
   122 #endif