From ce187e614a9c61cad0d15385c6a5e337890a5ab4 Mon Sep 17 00:00:00 2001 From: Erik Hofman Date: Thu, 22 Oct 2020 10:20:14 +0200 Subject: [PATCH] Fix the way struct tm works, year is since 1900 (so fix a year-2000 problem...) and month is 0-11 instead of 1-12. Return a string we constructed ourselves to prevent a possible buffer overrflow. Luckily the function isn't used in active code. --- simgear/timing/sg_time.cxx | 15 ++++++++------- simgear/timing/sg_time.hxx | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/simgear/timing/sg_time.cxx b/simgear/timing/sg_time.cxx index bbabd1a4..898604d8 100644 --- a/simgear/timing/sg_time.cxx +++ b/simgear/timing/sg_time.cxx @@ -361,9 +361,9 @@ time_t sgTimeGetGMT(int year, int month, int day, int hour, int min, int sec) { struct tm mt; - mt.tm_mon = month; + mt.tm_mon = month - 1; mt.tm_mday = day; - mt.tm_year = year; + mt.tm_year = year - 1900; mt.tm_hour = hour; mt.tm_min = min; mt.tm_sec = sec; @@ -379,10 +379,11 @@ time_t sgTimeGetGMT(int year, int month, int day, int hour, int min, int sec) } // format time -char* sgTimeFormatTime( const struct tm* p, char* buf ) +std::string sgTimeFormatTime( const struct tm* p ) { - sprintf( buf, "%d/%d/%2d %d:%02d:%02d", - p->tm_mon, p->tm_mday, p->tm_year, - p->tm_hour, p->tm_min, p->tm_sec); - return buf; + std::ostringstream buf; + + buf << 1900+p->tm_year << '-' << p->tm_mon+1 << '-' << p->tm_mday << 'T'; + buf << p->tm_hour << ':' << p->tm_min << ':' << p->tm_sec; + return buf.str(); } diff --git a/simgear/timing/sg_time.hxx b/simgear/timing/sg_time.hxx index 0990d0e2..cbfd2b8e 100644 --- a/simgear/timing/sg_time.hxx +++ b/simgear/timing/sg_time.hxx @@ -260,7 +260,7 @@ double sgTimeCalcGST( double mjd ); * @param buf buffer space to contain the result * @return pointer to character array containt the result */ -char* sgTimeFormatTime( const struct tm* p, char* buf ); +std::string sgTimeFormatTime( const struct tm* p); #endif // _SG_TIME_HXX