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