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