lemon/bits/windows.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 20 Feb 2009 20:36:26 +0000
changeset 498 afd134142111
child 493 3f0ddf255524
permissions -rw-r--r--
Various fixes for compiling on AIX (#211, #212)
alpar@491
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@491
     2
 *
alpar@491
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@491
     4
 *
alpar@491
     5
 * Copyright (C) 2003-2009
alpar@491
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@491
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@491
     8
 *
alpar@491
     9
 * Permission to use, modify and distribute this software is granted
alpar@491
    10
 * provided that this copyright notice appears in all copies. For
alpar@491
    11
 * precise terms see the accompanying LICENSE file.
alpar@491
    12
 *
alpar@491
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@491
    14
 * express or implied, and with no claim as to its suitability for any
alpar@491
    15
 * purpose.
alpar@491
    16
 *
alpar@491
    17
 */
alpar@491
    18
alpar@491
    19
///\file
alpar@491
    20
///\brief Some basic non-inline functions and static global data.
alpar@491
    21
alpar@491
    22
#include<lemon/bits/windows.h>
alpar@491
    23
alpar@491
    24
#ifdef WIN32
alpar@491
    25
#ifndef WIN32_LEAN_AND_MEAN
alpar@491
    26
#define WIN32_LEAN_AND_MEAN
alpar@491
    27
#endif
alpar@491
    28
#ifndef NOMINMAX
alpar@491
    29
#define NOMINMAX
alpar@491
    30
#endif
alpar@491
    31
#include <windows.h>
alpar@491
    32
#else
alpar@491
    33
#include <unistd.h>
alpar@491
    34
#include <ctime>
alpar@491
    35
#include <sys/times.h>
alpar@491
    36
#include <sys/time.h>
alpar@491
    37
#endif
alpar@491
    38
alpar@491
    39
#include <cmath>
alpar@491
    40
#include <sstream>
alpar@491
    41
alpar@491
    42
namespace lemon {
alpar@491
    43
  namespace bits {
alpar@491
    44
    void getWinProcTimes(double &rtime,
alpar@491
    45
                         double &utime, double &stime,
alpar@491
    46
                         double &cutime, double &cstime)
alpar@491
    47
    {
alpar@491
    48
#ifdef WIN32
alpar@491
    49
      static const double ch = 4294967296.0e-7;
alpar@491
    50
      static const double cl = 1.0e-7;
alpar@491
    51
alpar@491
    52
      FILETIME system;
alpar@491
    53
      GetSystemTimeAsFileTime(&system);
alpar@491
    54
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
alpar@491
    55
alpar@491
    56
      FILETIME create, exit, kernel, user;
alpar@491
    57
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
alpar@491
    58
        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
alpar@491
    59
        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
alpar@491
    60
        cutime = 0;
alpar@491
    61
        cstime = 0;
alpar@491
    62
      } else {
alpar@491
    63
        rtime = 0;
alpar@491
    64
        utime = 0;
alpar@491
    65
        stime = 0;
alpar@491
    66
        cutime = 0;
alpar@491
    67
        cstime = 0;
alpar@491
    68
      }
alpar@491
    69
#else
alpar@491
    70
      timeval tv;
alpar@491
    71
      gettimeofday(&tv, 0);
alpar@491
    72
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
alpar@491
    73
alpar@491
    74
      tms ts;
alpar@491
    75
      double tck=sysconf(_SC_CLK_TCK);
alpar@491
    76
      times(&ts);
alpar@491
    77
      utime=ts.tms_utime/tck;
alpar@491
    78
      stime=ts.tms_stime/tck;
alpar@491
    79
      cutime=ts.tms_cutime/tck;
alpar@491
    80
      cstime=ts.tms_cstime/tck;
alpar@491
    81
#endif
alpar@491
    82
    }
alpar@491
    83
alpar@491
    84
    std::string getWinFormattedDate()
alpar@491
    85
    {
alpar@491
    86
      std::ostringstream os;
alpar@491
    87
#ifdef WIN32
alpar@491
    88
      SYSTEMTIME time;
alpar@491
    89
      GetSystemTime(&time);
alpar@491
    90
#if defined(_MSC_VER) && (_MSC_VER < 1500)
alpar@491
    91
      LPWSTR buf1, buf2, buf3;
alpar@491
    92
      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@491
    93
                        L"ddd MMM dd", buf1, 11) &&
alpar@491
    94
          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@491
    95
                        L"HH':'mm':'ss", buf2, 9) &&
alpar@491
    96
          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@491
    97
                        L"yyyy", buf3, 5)) {
alpar@491
    98
        os << buf1 << ' ' << buf2 << ' ' << buf3;
alpar@491
    99
      }
alpar@491
   100
#else
alpar@491
   101
      char buf1[11], buf2[9], buf3[5];
alpar@491
   102
      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@491
   103
                        "ddd MMM dd", buf1, 11) &&
alpar@491
   104
          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@491
   105
                        "HH':'mm':'ss", buf2, 9) &&
alpar@491
   106
          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
alpar@491
   107
                        "yyyy", buf3, 5)) {
alpar@491
   108
        os << buf1 << ' ' << buf2 << ' ' << buf3;
alpar@491
   109
      }
alpar@491
   110
#endif
alpar@491
   111
      else os << "unknown";
alpar@491
   112
#else
alpar@491
   113
      timeval tv;
alpar@491
   114
      gettimeofday(&tv, 0);
alpar@491
   115
alpar@491
   116
      char cbuf[26];
alpar@491
   117
      ctime_r(&tv.tv_sec,cbuf);
alpar@491
   118
      os << cbuf;
alpar@491
   119
#endif
alpar@491
   120
      return os.str();
alpar@491
   121
    }
alpar@491
   122
alpar@491
   123
    int getWinRndSeed()
alpar@491
   124
    {
alpar@491
   125
#ifdef WIN32
alpar@491
   126
      FILETIME time;
alpar@491
   127
      GetSystemTimeAsFileTime(&time);
alpar@491
   128
      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
alpar@491
   129
#else
alpar@491
   130
      timeval tv;
alpar@491
   131
      gettimeofday(&tv, 0);
alpar@491
   132
      return getpid() + tv.tv_sec + tv.tv_usec;
alpar@491
   133
#endif
alpar@491
   134
    }
alpar@491
   135
  }
alpar@491
   136
}