File tree Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,19 @@ TEST(Date, constructorTest)
23
23
.toCustomedFormattedStringLocal (" %Y-%m-%d %H:%M:%S" , true )
24
24
.c_str ());
25
25
}
26
+ TEST (Date, DatabaseStringTest)
27
+ {
28
+ auto now = trantor::Date::now ();
29
+ EXPECT_EQ (now, trantor::Date::fromDbStringLocal (now.toDbStringLocal ()));
30
+ std::string dbString = " 2018-01-01 00:00:00.123" ;
31
+ auto dbDate = trantor::Date::fromDbStringLocal (dbString);
32
+ auto ms = (dbDate.microSecondsSinceEpoch () % 1000000 ) / 1000 ;
33
+ EXPECT_EQ (ms, 123 );
34
+ dbString = " 2018-01-01 00:00:00" ;
35
+ dbDate = trantor::Date::fromDbStringLocal (dbString);
36
+ ms = (dbDate.microSecondsSinceEpoch () % 1000000 ) / 1000 ;
37
+ EXPECT_EQ (ms, 0 );
38
+ }
26
39
int main (int argc, char **argv)
27
40
{
28
41
testing::InitGoogleTest (&argc, argv);
Original file line number Diff line number Diff line change @@ -293,10 +293,19 @@ Date Date::fromDbStringLocal(const std::string &datetime)
293
293
{
294
294
hour = std::stol (time[0 ]);
295
295
minute = std::stol (time[1 ]);
296
- second = std::stol (time[2 ]);
297
- if (3 < time.size ())
296
+ auto seconds = splitString (time[2 ], " ." );
297
+ second = std::stol (seconds[0 ]);
298
+ if (1 < seconds.size ())
298
299
{
299
- microSecond = std::stol (time[3 ]);
300
+ if (seconds[1 ].length () > 6 )
301
+ {
302
+ seconds[1 ].resize (6 );
303
+ }
304
+ else if (seconds[1 ].length () < 6 )
305
+ {
306
+ seconds[1 ].append (6 - seconds[1 ].length (), ' 0' );
307
+ }
308
+ microSecond = std::stol (seconds[1 ]);
300
309
}
301
310
}
302
311
}
You can’t perform that action at this time.
0 commit comments