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.

This commit is contained in:
Erik Hofman 2020-10-22 10:20:14 +02:00
parent 83500dd413
commit ce187e614a
2 changed files with 9 additions and 8 deletions

View File

@ -361,9 +361,9 @@ time_t sgTimeGetGMT(int year, int month, int day, int hour, int min, int sec)
{ {
struct tm mt; struct tm mt;
mt.tm_mon = month; mt.tm_mon = month - 1;
mt.tm_mday = day; mt.tm_mday = day;
mt.tm_year = year; mt.tm_year = year - 1900;
mt.tm_hour = hour; mt.tm_hour = hour;
mt.tm_min = min; mt.tm_min = min;
mt.tm_sec = sec; 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 // 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", std::ostringstream buf;
p->tm_mon, p->tm_mday, p->tm_year,
p->tm_hour, p->tm_min, p->tm_sec); buf << 1900+p->tm_year << '-' << p->tm_mon+1 << '-' << p->tm_mday << 'T';
return buf; buf << p->tm_hour << ':' << p->tm_min << ':' << p->tm_sec;
return buf.str();
} }

View File

@ -260,7 +260,7 @@ double sgTimeCalcGST( double mjd );
* @param buf buffer space to contain the result * @param buf buffer space to contain the result
* @return pointer to character array containt 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 #endif // _SG_TIME_HXX