diff --git a/MiniScript-cpp/src/DateTimeUtils.cpp b/MiniScript-cpp/src/DateTimeUtils.cpp index b0da71d..8cf5984 100644 --- a/MiniScript-cpp/src/DateTimeUtils.cpp +++ b/MiniScript-cpp/src/DateTimeUtils.cpp @@ -13,7 +13,9 @@ #if _WIN32 || _WIN64 struct tm *localtime_r( const time_t *timer, struct tm *buf ) { - *buf = *_localtime64(timer); + struct tm *newtime = _localtime64(timer); + if (newtime == nullptr) return nullptr; + *buf = *newtime; return buf; } #endif @@ -31,7 +33,8 @@ static bool Match(const String s, size_t *posB, const String match) { String FormatDate(time_t t, String formatSpec) { tm dateTime; - localtime_r(&t, &dateTime); + struct tm *newtime = localtime_r(&t, &dateTime); + if (newtime == nullptr) return ""; // arg t too large const int BUFSIZE = 128; char buffer[BUFSIZE]; diff --git a/MiniScript-cpp/tests/testDateTimeDateStr.ms b/MiniScript-cpp/tests/testDateTimeDateStr.ms new file mode 100644 index 0000000..b2ff00a --- /dev/null +++ b/MiniScript-cpp/tests/testDateTimeDateStr.ms @@ -0,0 +1,8 @@ +import "qa" + +testDateTimeDateStr = function + qa.assertEqual _dateStr(0), "2000-01-01 00:00:00" + qa.assertEqual _dateStr(1e20), "" +end function + +if refEquals(locals, globals) then testDateTimeDateStr