From bb0f860989f84b901f80017ae0139a3fc0446dc1 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 9 May 2011 20:22:34 +0000 Subject: Refactored the sys::localtime routines. --- src/sys/sys.cc | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'src/sys/sys.cc') diff --git a/src/sys/sys.cc b/src/sys/sys.cc index 39700b9..2169b4a 100644 --- a/src/sys/sys.cc +++ b/src/sys/sys.cc @@ -125,6 +125,15 @@ void signal(int signum, signalfunc handler) #endif } +void sleep(long milliseconds) +{ +#ifndef _WIN32 + ::usleep((useconds_t)(milliseconds * 1000)); +#else + Sleep((DWORD)milliseconds); +#endif +} + /* POSIX: @@ -156,36 +165,15 @@ WIN32: */ -unsigned long time() -{ -#ifndef _WIN32 - struct ::tm localtime; - time_t epochtime = ::time(0); - ::localtime_r(&epochtime, &localtime); - return ((unsigned long)(localtime.tm_sec + localtime.tm_min*60 + localtime.tm_hour*3600)); -#else - SYSTEMTIME localtime; - ::GetLocalTime(&localtime); - - return ((unsigned long)(localtime.wSecond + localtime.wMinute*60 + localtime.wHour*3600)); -#endif -} - -void sleep(float seconds) -{ -#ifndef _WIN32 - ::usleep((useconds_t)(seconds * 1000000.0f)); -#else - Sleep((DWORD)(seconds*1000.0f)); -#endif -} - -void get_datetime(int &year, int & month, int & day, int & hours, int & minutes, int &seconds) +void get_localtime(int & year, int & month, int & day, int & hours, int & minutes, int & seconds, int & milliseconds) { #ifndef _WIN32 + struct ::timeval tv; + struct ::timezone tz; struct ::tm localtime; - time_t epochtime = ::time(0); - ::localtime_r(&epochtime, &localtime); + + ::gettimeofday(&tv, &tz); + ::localtime_r(&tv.tv_sec, &localtime); year = localtime.tm_year + 1900; month = localtime.tm_mon + 1; @@ -194,6 +182,7 @@ void get_datetime(int &year, int & month, int & day, int & hours, int & minutes, hours = localtime.tm_hour; minutes = localtime.tm_min; seconds = localtime.tm_sec; + milliseconds = + tv.tv_usec / 1000; #else SYSTEMTIME localtime; ::GetLocalTime(&localtime); @@ -205,9 +194,15 @@ void get_datetime(int &year, int & month, int & day, int & hours, int & minutes, hours = localtime.wHour; minutes = localtime.wMinute; seconds = localtime.wSecond; + milliseconds = localtime.wMilliseconds; #endif } +void get_localtime(int & hours, int & minutes, int & seconds) { + int year, month, day, milliseconds; + get_localtime(year, month, day, hours, minutes, seconds, milliseconds); +} + void quit(int status) { ::exit(status); -- cgit v1.2.3