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