X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Futils.cpp;h=b59a8af0ea944fdde78cf77d4ee5c38cf4b6aa15;hp=00c389d93acd4d8ae55edf14d3cd042e420e7da1;hb=03bacb2343eb5d17819732582c0866f087e9ce27;hpb=e1ea831a640fba534e7e42e399f04cdf681ef8d3 diff --git a/source/time/utils.cpp b/source/time/utils.cpp index 00c389d..b59a8af 100644 --- a/source/time/utils.cpp +++ b/source/time/utils.cpp @@ -1,50 +1,79 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ +#ifdef WIN32 +#include +#else #include #include +#include +#endif +#include +#include "datetime.h" +#include "rawtime_private.h" #include "timedelta.h" #include "timestamp.h" #include "units.h" #include "utils.h" +using namespace std; + namespace Msp { namespace Time { -/** -Returns the current timestamp. -*/ TimeStamp now() { +#ifndef WIN32 timeval tv; gettimeofday(&tv, 0); return TimeStamp(tv.tv_sec*1000000LL+tv.tv_usec); +#else + static RawTime epoch = 0; + if(!epoch) + { + SYSTEMTIME st; + st.wYear = 1970; + st.wMonth = 1; + st.wDay = 1; + st.wHour = 0; + st.wMinute = 0; + st.wSecond = 0; + st.wMilliseconds = 0; + + FILETIME ft; + SystemTimeToFileTime(&st, &ft); + epoch = (ft.dwLowDateTime+(static_cast(ft.dwHighDateTime)<<32))/10; + } + + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + return TimeStamp((ft.dwLowDateTime+(static_cast(ft.dwHighDateTime)<<32))/10-epoch); +#endif +} + +string format_now(const string &fmt) +{ + return DateTime(now()).format(fmt); } -/** -Returns the CPU time used by the program so far. -*/ TimeDelta get_cpu_time() { +#ifndef WIN32 rusage ru; getrusage(RUSAGE_SELF, &ru); return (ru.ru_utime.tv_sec+ru.ru_stime.tv_sec)*sec + (ru.ru_utime.tv_usec+ru.ru_stime.tv_usec)*usec; +#else + //XXX Figure out the function to use on Win32 + return TimeDelta(); +#endif } -/** -Sleeps for the given time. -*/ -int sleep(const TimeDelta &d) +void sleep(const TimeDelta &d) { #ifndef WIN32 - timespec ts; - d.fill_timespec(ts); - return nanosleep(&ts, 0); + timespec ts = rawtime_to_timespec(d.raw()); + while(nanosleep(&ts, 0)==-1) + if(errno!=EINTR) + throw system_error("nanosleep"); #else - Sleep(d/msec); - return 0; + Sleep((DWORD)(d/msec)); #endif }