Commit 4e8b962c authored by Maciej Sobczak's avatar Maciej Sobczak

Extended the datetime parser to allow date, time and datetime values.

parent afacd427
......@@ -36,22 +36,50 @@ void soci::details::postgresql::parse_std_tm(char const * buf, std::tm & t)
{
char const * p1 = buf;
char * p2;
long year, month, day;
char separator;
long a, b, c;
long year = 1900, month = 1, day = 1;
long hour = 0, minute = 0, second = 0;
char const * errMsg = "Cannot convert data to std::tm.";
year = parse10(p1, p2, errMsg);
month = parse10(p1, p2, errMsg);
day = parse10(p1, p2, errMsg);
a = parse10(p1, p2, errMsg);
separator = *p2;
b = parse10(p1, p2, errMsg);
c = parse10(p1, p2, errMsg);
if (*p2 != '\0')
{
// there is also the time of day available
// there are mote elements to parse
// - assume that what was already parsed is a date part
// and that the remaining elements describe the time of day
year = a;
month = b;
day = c;
hour = parse10(p1, p2, errMsg);
minute = parse10(p1, p2, errMsg);
second = parse10(p1, p2, errMsg);
}
else
{
// only three values have been parsed
if (separator == '-')
{
// assume the date value was read
// (leave the time of day as 00:00:00)
year = a;
month = b;
day = c;
}
else
{
// assume the time of day was read
// (leave the date part as 1900-01-01)
hour = a;
minute = b;
second = c;
}
}
t.tm_isdst = -1;
t.tm_year = year - 1900;
......
......@@ -409,6 +409,46 @@ void test9()
std::cout << "test 9 passed" << std::endl;
}
// test for date, time and timestamp parsing
void test10()
{
{
session sql(backEnd, connectString);
std::string someDate = "2009-06-17 22:51:03";
std::tm t1, t2, t3;
sql << "select :sd::date, :sd::time, :sd::timestamp",
use(someDate, "sd"), into(t1), into(t2), into(t3);
// t1 should contain only the date part
assert(t1.tm_year == 2009 - 1900);
assert(t1.tm_mon == 6 - 1);
assert(t1.tm_mday == 17);
assert(t1.tm_hour == 0);
assert(t1.tm_min == 0);
assert(t1.tm_sec == 0);
// t2 should contain only the time of day part
assert(t2.tm_year == 0);
assert(t2.tm_mon == 0);
assert(t2.tm_mday == 1);
assert(t2.tm_hour == 22);
assert(t2.tm_min == 51);
assert(t2.tm_sec == 3);
// t3 should contain all information
assert(t3.tm_year == 2009 - 1900);
assert(t3.tm_mon == 6 - 1);
assert(t3.tm_mday == 17);
assert(t3.tm_hour == 22);
assert(t3.tm_min == 51);
assert(t3.tm_sec == 3);
}
std::cout << "test 10 passed" << std::endl;
}
// DDL Creation objects for common tests
struct table_creator_one : public table_creator_base
{
......@@ -521,6 +561,7 @@ int main(int argc, char** argv)
test7();
test8();
test9();
test10();
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment