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