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/consoleinterface.cc | 8 ++++---- src/sys/sys.cc | 49 ++++++++++++++++++++------------------------- src/sys/sys.h | 47 ++++++++++++++++++++++++++++--------------- 3 files changed, 57 insertions(+), 47 deletions(-) (limited to 'src/sys') diff --git a/src/sys/consoleinterface.cc b/src/sys/consoleinterface.cc index ce82047..77510f4 100644 --- a/src/sys/consoleinterface.cc +++ b/src/sys/consoleinterface.cc @@ -46,8 +46,8 @@ void set_ansi(const bool ansi) void fallback_print_timestamp() { - int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0; - get_datetime(year, month, day, hour, min, sec); + int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, milliseconds = 0; + get_localtime(year, month, day, hour, min, sec, milliseconds); std::cout << year << "-" << month << "-" << day << " " << hour << ":" << min << ":" << sec << " "; } @@ -165,8 +165,8 @@ int ConsoleBuffer::overflow(int c) */ if (con_timestamps && !ConsoleInterface::instance()->rcon()) { std::ostringstream str; - int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0; - get_datetime(year, month, day, hour, min, sec); + int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, milliseconds = 0; + get_localtime(year, month, day, hour, min, sec, milliseconds); str << "^B" << std::setw(4) << std::setfill('0') << year << "-" 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); diff --git a/src/sys/sys.h b/src/sys/sys.h index 99c9291..575d02d 100644 --- a/src/sys/sys.h +++ b/src/sys/sys.h @@ -14,40 +14,55 @@ /// maximum line size throught the game #define MAXCMDSIZE 1024 -/// contains operating system dependent functions -/** sys is a core subsystem - */ +/** + * @brief platform dependent functions + * */ namespace sys { + +/** + * @brief type definition for a signal handler function pointer + * */ typedef void(* signalfunc)(int signum); -/// returns true if a path exists and it is a directory +/** + * @brief returns true if a path exists and is a directory + * */ bool directory_exists(const std::string &path); -/// returns true if a file exists +/** + * @brief returns true if a file exists + * */ bool file_exists(const std::string &filename); -/// create a directory +/** + * @brief create a directory + * */ void mkdir(const std::string &path); -/// intercept OS signals +/** + * @brief install an OS signal handler + * */ void signal(int signum, signalfunc handler); - /** * @brief operation system exit() application - * @param status return value */ void quit(int status); -/// suspend process for a number of seconds -void sleep(float seconds); +/** + * @brief suspend the current process for a number of milliseconds + * 1 second is 1000 milliseconds + * */ +void sleep(long milliseconds); + +/** + * @brief get the local system date and time + * */ +void get_localtime(int & hours, int & minutes, int & seconds); -/// return the current system time of day, in seconds after midnight -unsigned long time(); +void get_localtime(int &year, int & month, int & day, int & hours, int & minutes, int & seconds, int & milliseconds); -/// get the current system date and time -void get_datetime(int &year, int & month, int & day, int & hours, int & minutes, int &seconds); -} +} // namespace sys #include "sys/consoleinterface.h" -- cgit v1.2.3