@@ -284,11 +284,36 @@ Date Date::fromDbStringLocal(const std::string &datetime)
284284 unsigned int year = {0 }, month = {0 }, day = {0 }, hour = {0 }, minute = {0 },
285285 second = {0 }, microSecond = {0 };
286286 std::vector<std::string> &&v = splitString (datetime, " " );
287- if (2 == v.size ())
287+
288+ if (v.size () == 0 )
289+ {
290+ throw std::invalid_argument (" Invalid date string: " + datetime);
291+ }
292+ const std::vector<std::string> date = splitString (v[0 ], " -" );
293+ if (date.size () != 3 )
294+ {
295+ throw std::invalid_argument (" Invalid date string: " + datetime);
296+ }
297+ if (v.size () == 1 )
288298 {
289- // date
290- std::vector<std::string> date = splitString (v[0 ], " -" );
291- if (3 == date.size ())
299+ // Fromat YYYY-MM-DD is given
300+ try
301+ {
302+ year = std::stol (date[0 ]);
303+ month = std::stol (date[1 ]);
304+ day = std::stol (date[2 ]);
305+ }
306+ catch (...)
307+ {
308+ throw std::invalid_argument (" Invalid date string: " + datetime);
309+ }
310+ return Date (year, month, day, hour, minute, second, microSecond);
311+ }
312+
313+ if (v.size () == 2 )
314+ {
315+ // Format YYYY-MM-DD HH:MM:SS[.UUUUUU] is given
316+ try
292317 {
293318 year = std::stol (date[0 ]);
294319 month = std::stol (date[1 ]);
@@ -314,9 +339,16 @@ Date Date::fromDbStringLocal(const std::string &datetime)
314339 }
315340 }
316341 }
342+ catch (...)
343+ {
344+ throw std::invalid_argument (" Invalid date string: " + datetime);
345+ }
346+ return Date (year, month, day, hour, minute, second, microSecond);
317347 }
318- return trantor::Date (year, month, day, hour, minute, second, microSecond);
348+
349+ throw std::invalid_argument (" Invalid date string: " + datetime);
319350}
351+
320352Date Date::fromDbString (const std::string &datetime)
321353{
322354 return fromDbStringLocal (datetime).after (
0 commit comments