alpar@482: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@482:  *
alpar@482:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@482:  *
alpar@877:  * Copyright (C) 2003-2010
alpar@482:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@482:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@482:  *
alpar@482:  * Permission to use, modify and distribute this software is granted
alpar@482:  * provided that this copyright notice appears in all copies. For
alpar@482:  * precise terms see the accompanying LICENSE file.
alpar@482:  *
alpar@482:  * This software is provided "AS IS" with no warranty of any kind,
alpar@482:  * express or implied, and with no claim as to its suitability for any
alpar@482:  * purpose.
alpar@482:  *
alpar@482:  */
alpar@482: 
alpar@482: ///\file
alpar@482: ///\brief Some basic non-inline functions and static global data.
alpar@482: 
alpar@482: #include<lemon/bits/windows.h>
alpar@482: 
alpar@482: #ifdef WIN32
alpar@482: #ifndef WIN32_LEAN_AND_MEAN
alpar@482: #define WIN32_LEAN_AND_MEAN
alpar@482: #endif
alpar@482: #ifndef NOMINMAX
alpar@482: #define NOMINMAX
alpar@482: #endif
tapolcai@483: #ifdef UNICODE
tapolcai@483: #undef UNICODE
tapolcai@483: #endif
alpar@482: #include <windows.h>
tapolcai@483: #ifdef LOCALE_INVARIANT
tapolcai@483: #define MY_LOCALE LOCALE_INVARIANT
tapolcai@483: #else
tapolcai@483: #define MY_LOCALE LOCALE_NEUTRAL
tapolcai@483: #endif
alpar@482: #else
alpar@482: #include <unistd.h>
alpar@482: #include <ctime>
alpar@940: #ifndef WIN32
alpar@482: #include <sys/times.h>
alpar@940: #endif
alpar@482: #include <sys/time.h>
alpar@482: #endif
alpar@482: 
alpar@482: #include <cmath>
alpar@482: #include <sstream>
alpar@482: 
alpar@482: namespace lemon {
alpar@482:   namespace bits {
alpar@482:     void getWinProcTimes(double &rtime,
alpar@482:                          double &utime, double &stime,
alpar@482:                          double &cutime, double &cstime)
alpar@482:     {
alpar@482: #ifdef WIN32
alpar@482:       static const double ch = 4294967296.0e-7;
alpar@482:       static const double cl = 1.0e-7;
alpar@482: 
alpar@482:       FILETIME system;
alpar@482:       GetSystemTimeAsFileTime(&system);
alpar@482:       rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
alpar@482: 
alpar@482:       FILETIME create, exit, kernel, user;
alpar@482:       if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
alpar@482:         utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
alpar@482:         stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
alpar@482:         cutime = 0;
alpar@482:         cstime = 0;
alpar@482:       } else {
alpar@482:         rtime = 0;
alpar@482:         utime = 0;
alpar@482:         stime = 0;
alpar@482:         cutime = 0;
alpar@482:         cstime = 0;
alpar@482:       }
alpar@482: #else
alpar@482:       timeval tv;
alpar@482:       gettimeofday(&tv, 0);
alpar@482:       rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
alpar@482: 
alpar@482:       tms ts;
alpar@482:       double tck=sysconf(_SC_CLK_TCK);
alpar@482:       times(&ts);
alpar@482:       utime=ts.tms_utime/tck;
alpar@482:       stime=ts.tms_stime/tck;
alpar@482:       cutime=ts.tms_cutime/tck;
alpar@482:       cstime=ts.tms_cstime/tck;
alpar@482: #endif
alpar@482:     }
alpar@482: 
alpar@482:     std::string getWinFormattedDate()
alpar@482:     {
alpar@482:       std::ostringstream os;
alpar@482: #ifdef WIN32
alpar@482:       SYSTEMTIME time;
alpar@482:       GetSystemTime(&time);
tapolcai@483:       char buf1[11], buf2[9], buf3[5];
alpar@877:           if (GetDateFormat(MY_LOCALE, 0, &time,
tapolcai@483:                         ("ddd MMM dd"), buf1, 11) &&
tapolcai@483:           GetTimeFormat(MY_LOCALE, 0, &time,
tapolcai@483:                         ("HH':'mm':'ss"), buf2, 9) &&
tapolcai@483:           GetDateFormat(MY_LOCALE, 0, &time,
tapolcai@483:                         ("yyyy"), buf3, 5)) {
alpar@482:         os << buf1 << ' ' << buf2 << ' ' << buf3;
alpar@482:       }
alpar@482:       else os << "unknown";
alpar@482: #else
alpar@482:       timeval tv;
alpar@482:       gettimeofday(&tv, 0);
alpar@482: 
alpar@482:       char cbuf[26];
alpar@482:       ctime_r(&tv.tv_sec,cbuf);
alpar@482:       os << cbuf;
alpar@482: #endif
alpar@482:       return os.str();
alpar@482:     }
alpar@482: 
alpar@482:     int getWinRndSeed()
alpar@482:     {
alpar@482: #ifdef WIN32
alpar@482:       FILETIME time;
alpar@482:       GetSystemTimeAsFileTime(&time);
alpar@482:       return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
alpar@482: #else
alpar@482:       timeval tv;
alpar@482:       gettimeofday(&tv, 0);
alpar@482:       return getpid() + tv.tv_sec + tv.tv_usec;
alpar@482: #endif
alpar@482:     }
alpar@482:   }
alpar@482: }