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