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