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/client/client.cc | 4 ++-- src/client/infowidget.cc | 30 ++++++++++++++------------- src/core/gameserver.cc | 17 ++++++++-------- src/dedicated/console.cc | 25 +++++++++++++++-------- src/render/screenshot.cc | 27 +++++++++++-------------- src/sys/consoleinterface.cc | 8 ++++---- src/sys/sys.cc | 49 ++++++++++++++++++++------------------------- src/sys/sys.h | 47 ++++++++++++++++++++++++++++--------------- 8 files changed, 112 insertions(+), 95 deletions(-) diff --git a/src/client/client.cc b/src/client/client.cc index 5b72146..d761e6e 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -196,13 +196,13 @@ void Client::run() } if (cl_framerate->value()) { - client_frame_lenght = (Uint32) roundf(1000.0f / cl_framerate->value()); + client_frame_lenght = (Uint32) floorf(1000.0f / cl_framerate->value()); } else { client_frame_lenght = 0; } // only advance per microsecond frame - Uint32 d = client_current_timestamp - client_previous_timestamp; + const Uint32 d = client_current_timestamp - client_previous_timestamp; if ((d > 0)) { if (d >= client_frame_lenght) { frame(client_current_timestamp); diff --git a/src/client/infowidget.cc b/src/client/infowidget.cc index ed118a8..52af5ef 100644 --- a/src/client/infowidget.cc +++ b/src/client/infowidget.cc @@ -199,22 +199,24 @@ void ClockInfoWidget::draw() { if (mode() == ClockOff) return; + + int hours, minutes, seconds; + sys::get_localtime(hours, minutes, seconds); - std::ostringstream clockstr; - const unsigned long current_time = sys::time(); - unsigned long hours = current_time / 3600; - unsigned long minutes = (current_time % 3600) / 60; - //unsigned long seconds = current_time % 60; - + std::ostringstream clockstr; clockstr << std::setfill('0') << std::setw(2); - if (mode() == Clock12Hours) { - if (hours > 12) { - clockstr << hours-12 << ":" << std::setfill('0') << std::setw(2) << minutes << "pm"; - } else { - if (hours == 0) - hours +=12; - clockstr << hours << ":" << std::setfill('0') << std::setw(2) << minutes << "am"; - } + + if (mode() == Clock12Hours) { + std::string suffix("am"); + + if (hours >= 12) { + suffix[1] = 'p'; + hours -= 12; + } + if (hours == 0) { + hours += 12; + } + clockstr << hours << ":" << std::setfill('0') << std::setw(2) << minutes << suffix; } else { clockstr << " " << hours << ":" << std::setfill('0') << std::setw(2) << minutes; } diff --git a/src/core/gameserver.cc b/src/core/gameserver.cc index 6a20317..85a956d 100644 --- a/src/core/gameserver.cc +++ b/src/core/gameserver.cc @@ -41,16 +41,15 @@ void func_time(std::string const &args) { using namespace std; + // FIXME unify with dedicated server clock int minutes = (int) floorf(server()->time() / 60.0f); int seconds = (int) floorf(server()->time() - (float) minutes * 60.0f); - int syshours = sys::time() / 3600; - int sysminutes = (sys::time() - syshours * 3600) / 60; - int sysseconds = sys::time() % 60; - - con_print << - "Uptime " << minutes << ":" << setfill('0') << setw(2) << seconds << - " Local time " << setfill(' ') << setw(2) << syshours << ":" << setfill('0') << setw(2) << sysminutes << ":" << setw(2) << sysseconds << setfill(' ') << std::endl; + int syshours, sysminutes, sysseconds; + sys::get_localtime(syshours, sysminutes, sysseconds); + + con_print << "Uptime " << minutes << ":" << setfill('0') << setw(2) << seconds << + " Local time " << setfill(' ') << setw(2) << syshours << ":" << setfill('0') << setw(2) << sysminutes << ":" << setw(2) << sysseconds << setfill(' ') << std::endl; } void func_mute(std::string const &args) @@ -525,8 +524,8 @@ void GameServer::frame(unsigned long timestamp) if ((Cvar::sv_dedicated->value() || Cvar::sv_private->value())) { if (core::Cvar::sv_framerate->value()) { - float f = 1000.0f / core::Cvar::sv_framerate->value(); - if (server_startup + this->timestamp() + f > timestamp) { + float fps = ::floorf(1000.0f / core::Cvar::sv_framerate->value()); + if (server_startup + this->timestamp() + (unsigned long) fps > timestamp) { return; } } diff --git a/src/dedicated/console.cc b/src/dedicated/console.cc index d33fb0b..fd61ffe 100644 --- a/src/dedicated/console.cc +++ b/src/dedicated/console.cc @@ -210,8 +210,8 @@ void Console::draw_status() std::stringstream status; // system time - int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0; - sys::get_datetime(year, month, day, hour, min, sec); + int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, milliseconds = 0; + sys::get_localtime(year, month, day, hour, min, sec, milliseconds); status << std::setw(4) << std::setfill('0') << year << "-" << std::setw(2) << std::setfill('0') << month << "-" @@ -221,15 +221,24 @@ void Console::draw_status() << std::setw(2) << sec << " " << std::setw(2) << " "; - // uptime - int uptime_days = (int) floorf(core::game()->time() / (24.0f * 3600.0f)); - int uptime_hours = (int) floorf(core::game()->time() / 3600.0f); - int uptime_minutes = (int) floorf(core::game()->time() / 60.0f); - int uptime_seconds = (int) floorf(core::game()->time() - (float) uptime_minutes * 60.0f); + // uptime + // FIXME this is plain wrong, but the timing routines need to be refactored first + float uptime = core::game()->time(); + + const int uptime_days = (int) floorf(uptime / (24.0f * 3600.0f)); + uptime -= uptime_days * 24.0f * 3600.0f; + + const int uptime_hours = (int) floorf(uptime / 3600.0f); + uptime -= uptime_hours * 3600.0f; + + const int uptime_minutes = (int) floorf(uptime / 60.0f); + uptime -= uptime_minutes * 60.0f; + + const int uptime_seconds = (int) floorf(uptime); status << "uptime "; if (uptime_days > 0) { - status << uptime_days << aux::plural("day", uptime_days); + status << uptime_days << " " << aux::plural("day", uptime_days); } status << std::setfill('0') << std::setw(2) << uptime_hours << ":" << std::setfill('0') << std::setw(2) << uptime_minutes << ":" diff --git a/src/render/screenshot.cc b/src/render/screenshot.cc index f75cbe8..9018a7d 100644 --- a/src/render/screenshot.cc +++ b/src/render/screenshot.cc @@ -68,27 +68,25 @@ void Screenshot::save() // check if the date has changed since the previous screenshot; std::stringstream filenamestr; - int day, month, year, hour, min, sec; - int date_serial; - - sys::get_datetime(year, month, day, hour, min, sec); - date_serial = (day - 1) + (month - 1) * 31 + (year - 2000) * 31 * 12; - if (current_date != date_serial) { - current_number = 0; - current_date = date_serial; - } - + int day, month, year, hour, min, sec, msec; + bool available = false; do { + sys::get_localtime(year, month, day, hour, min, sec, msec); std::stringstream filenamestr; + // screenshots directory filenamestr << filesystem::writedir() << "screenshots/"; // date - filenamestr << std::setw(4) << std::setfill('0') << year << "-"; - filenamestr << std::setw(2) << month << "-"; + filenamestr << std::setw(4) << std::setfill('0') << year << '-'; + filenamestr << std::setw(2) << month << '-'; filenamestr << std::setw(2) << day << '-'; - // sequence number - filenamestr << std::setw(4) << std::setfill('0') << current_number; + //time + filenamestr << std::setw(2) << hour << '-'; + filenamestr << std::setw(2) << min << '-'; + filenamestr << std::setw(2) << sec << '-'; + filenamestr << std::setw(2) << msec / 10; + // extension filenamestr << "." << screenshotformat->str(); @@ -100,7 +98,6 @@ void Screenshot::save() } else { available = true; } - current_number++; } while (!available); render::Image image(State::width(), State::height(), 3); 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