lemon/bits/windows.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 20 Dec 2011 18:15:14 +0100
changeset 964 2b6bffe0e7e8
parent 877 141f9c0db4a3
parent 940 64260c0f58eb
child 979 43a91b33f374
permissions -rw-r--r--
Merge
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@877
     5
 * Copyright (C) 2003-2010
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
tapolcai@493
    31
#ifdef UNICODE
tapolcai@493
    32
#undef UNICODE
tapolcai@493
    33
#endif
alpar@491
    34
#include <windows.h>
tapolcai@493
    35
#ifdef LOCALE_INVARIANT
tapolcai@493
    36
#define MY_LOCALE LOCALE_INVARIANT
tapolcai@493
    37
#else
tapolcai@493
    38
#define MY_LOCALE LOCALE_NEUTRAL
tapolcai@493
    39
#endif
alpar@491
    40
#else
alpar@491
    41
#include <unistd.h>
alpar@491
    42
#include <ctime>
alpar@940
    43
#ifndef WIN32
alpar@491
    44
#include <sys/times.h>
alpar@940
    45
#endif
alpar@491
    46
#include <sys/time.h>
alpar@491
    47
#endif
alpar@491
    48
alpar@491
    49
#include <cmath>
alpar@491
    50
#include <sstream>
alpar@491
    51
alpar@491
    52
namespace lemon {
alpar@491
    53
  namespace bits {
alpar@491
    54
    void getWinProcTimes(double &rtime,
alpar@491
    55
                         double &utime, double &stime,
alpar@491
    56
                         double &cutime, double &cstime)
alpar@491
    57
    {
alpar@491
    58
#ifdef WIN32
alpar@491
    59
      static const double ch = 4294967296.0e-7;
alpar@491
    60
      static const double cl = 1.0e-7;
alpar@491
    61
alpar@491
    62
      FILETIME system;
alpar@491
    63
      GetSystemTimeAsFileTime(&system);
alpar@491
    64
      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
alpar@491
    65
alpar@491
    66
      FILETIME create, exit, kernel, user;
alpar@491
    67
      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
alpar@491
    68
        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
alpar@491
    69
        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
alpar@491
    70
        cutime = 0;
alpar@491
    71
        cstime = 0;
alpar@491
    72
      } else {
alpar@491
    73
        rtime = 0;
alpar@491
    74
        utime = 0;
alpar@491
    75
        stime = 0;
alpar@491
    76
        cutime = 0;
alpar@491
    77
        cstime = 0;
alpar@491
    78
      }
alpar@491
    79
#else
alpar@491
    80
      timeval tv;
alpar@491
    81
      gettimeofday(&tv, 0);
alpar@491
    82
      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
alpar@491
    83
alpar@491
    84
      tms ts;
alpar@491
    85
      double tck=sysconf(_SC_CLK_TCK);
alpar@491
    86
      times(&ts);
alpar@491
    87
      utime=ts.tms_utime/tck;
alpar@491
    88
      stime=ts.tms_stime/tck;
alpar@491
    89
      cutime=ts.tms_cutime/tck;
alpar@491
    90
      cstime=ts.tms_cstime/tck;
alpar@491
    91
#endif
alpar@491
    92
    }
alpar@491
    93
alpar@491
    94
    std::string getWinFormattedDate()
alpar@491
    95
    {
alpar@491
    96
      std::ostringstream os;
alpar@491
    97
#ifdef WIN32
alpar@491
    98
      SYSTEMTIME time;
alpar@491
    99
      GetSystemTime(&time);
tapolcai@493
   100
      char buf1[11], buf2[9], buf3[5];
alpar@877
   101
          if (GetDateFormat(MY_LOCALE, 0, &time,
tapolcai@493
   102
                        ("ddd MMM dd"), buf1, 11) &&
tapolcai@493
   103
          GetTimeFormat(MY_LOCALE, 0, &time,
tapolcai@493
   104
                        ("HH':'mm':'ss"), buf2, 9) &&
tapolcai@493
   105
          GetDateFormat(MY_LOCALE, 0, &time,
tapolcai@493
   106
                        ("yyyy"), buf3, 5)) {
alpar@491
   107
        os << buf1 << ' ' << buf2 << ' ' << buf3;
alpar@491
   108
      }
alpar@491
   109
      else os << "unknown";
alpar@491
   110
#else
alpar@491
   111
      timeval tv;
alpar@491
   112
      gettimeofday(&tv, 0);
alpar@491
   113
alpar@491
   114
      char cbuf[26];
alpar@491
   115
      ctime_r(&tv.tv_sec,cbuf);
alpar@491
   116
      os << cbuf;
alpar@491
   117
#endif
alpar@491
   118
      return os.str();
alpar@491
   119
    }
alpar@491
   120
alpar@491
   121
    int getWinRndSeed()
alpar@491
   122
    {
alpar@491
   123
#ifdef WIN32
alpar@491
   124
      FILETIME time;
alpar@491
   125
      GetSystemTimeAsFileTime(&time);
alpar@491
   126
      return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
alpar@491
   127
#else
alpar@491
   128
      timeval tv;
alpar@491
   129
      gettimeofday(&tv, 0);
alpar@491
   130
      return getpid() + tv.tv_sec + tv.tv_usec;
alpar@491
   131
#endif
alpar@491
   132
    }
alpar@491
   133
  }
alpar@491
   134
}