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