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