lemon/bits/windows.cc
changeset 491 879c55700cd4
child 493 3f0ddf255524
equal deleted inserted replaced
-1:000000000000 0:50900dbcc37b
       
     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-2009
       
     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 #include <windows.h>
       
    32 #else
       
    33 #include <unistd.h>
       
    34 #include <ctime>
       
    35 #include <sys/times.h>
       
    36 #include <sys/time.h>
       
    37 #endif
       
    38 
       
    39 #include <cmath>
       
    40 #include <sstream>
       
    41 
       
    42 namespace lemon {
       
    43   namespace bits {
       
    44     void getWinProcTimes(double &rtime,
       
    45                          double &utime, double &stime,
       
    46                          double &cutime, double &cstime)
       
    47     {
       
    48 #ifdef WIN32
       
    49       static const double ch = 4294967296.0e-7;
       
    50       static const double cl = 1.0e-7;
       
    51 
       
    52       FILETIME system;
       
    53       GetSystemTimeAsFileTime(&system);
       
    54       rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
       
    55 
       
    56       FILETIME create, exit, kernel, user;
       
    57       if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
       
    58         utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
       
    59         stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
       
    60         cutime = 0;
       
    61         cstime = 0;
       
    62       } else {
       
    63         rtime = 0;
       
    64         utime = 0;
       
    65         stime = 0;
       
    66         cutime = 0;
       
    67         cstime = 0;
       
    68       }
       
    69 #else
       
    70       timeval tv;
       
    71       gettimeofday(&tv, 0);
       
    72       rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
       
    73 
       
    74       tms ts;
       
    75       double tck=sysconf(_SC_CLK_TCK);
       
    76       times(&ts);
       
    77       utime=ts.tms_utime/tck;
       
    78       stime=ts.tms_stime/tck;
       
    79       cutime=ts.tms_cutime/tck;
       
    80       cstime=ts.tms_cstime/tck;
       
    81 #endif
       
    82     }
       
    83 
       
    84     std::string getWinFormattedDate()
       
    85     {
       
    86       std::ostringstream os;
       
    87 #ifdef WIN32
       
    88       SYSTEMTIME time;
       
    89       GetSystemTime(&time);
       
    90 #if defined(_MSC_VER) && (_MSC_VER < 1500)
       
    91       LPWSTR buf1, buf2, buf3;
       
    92       if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
       
    93                         L"ddd MMM dd", buf1, 11) &&
       
    94           GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
       
    95                         L"HH':'mm':'ss", buf2, 9) &&
       
    96           GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
       
    97                         L"yyyy", buf3, 5)) {
       
    98         os << buf1 << ' ' << buf2 << ' ' << buf3;
       
    99       }
       
   100 #else
       
   101       char buf1[11], buf2[9], buf3[5];
       
   102       if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
       
   103                         "ddd MMM dd", buf1, 11) &&
       
   104           GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
       
   105                         "HH':'mm':'ss", buf2, 9) &&
       
   106           GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
       
   107                         "yyyy", buf3, 5)) {
       
   108         os << buf1 << ' ' << buf2 << ' ' << buf3;
       
   109       }
       
   110 #endif
       
   111       else os << "unknown";
       
   112 #else
       
   113       timeval tv;
       
   114       gettimeofday(&tv, 0);
       
   115 
       
   116       char cbuf[26];
       
   117       ctime_r(&tv.tv_sec,cbuf);
       
   118       os << cbuf;
       
   119 #endif
       
   120       return os.str();
       
   121     }
       
   122 
       
   123     int getWinRndSeed()
       
   124     {
       
   125 #ifdef WIN32
       
   126       FILETIME time;
       
   127       GetSystemTimeAsFileTime(&time);
       
   128       return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
       
   129 #else
       
   130       timeval tv;
       
   131       gettimeofday(&tv, 0);
       
   132       return getpid() + tv.tv_sec + tv.tv_usec;
       
   133 #endif
       
   134     }
       
   135   }
       
   136 }