@@ -284,11 +284,36 @@ Date Date::fromDbStringLocal(const std::string &datetime)
284
284
unsigned int year = {0 }, month = {0 }, day = {0 }, hour = {0 }, minute = {0 },
285
285
second = {0 }, microSecond = {0 };
286
286
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 )
288
298
{
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
292
317
{
293
318
year = std::stol (date[0 ]);
294
319
month = std::stol (date[1 ]);
@@ -314,9 +339,16 @@ Date Date::fromDbStringLocal(const std::string &datetime)
314
339
}
315
340
}
316
341
}
342
+ catch (...)
343
+ {
344
+ throw std::invalid_argument (" Invalid date string: " + datetime);
345
+ }
346
+ return Date (year, month, day, hour, minute, second, microSecond);
317
347
}
318
- return trantor::Date (year, month, day, hour, minute, second, microSecond);
348
+
349
+ throw std::invalid_argument (" Invalid date string: " + datetime);
319
350
}
351
+
320
352
Date Date::fromDbString (const std::string &datetime)
321
353
{
322
354
return fromDbStringLocal (datetime).after (
0 commit comments