// // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // #include "common.h" #include #include #include namespace // anonymous { // helper function for parsing decimal data (for std::tm) long parse10(char const * & p1, char * & p2, char const * msg) { long v = std::strtol(p1, &p2, 10); if (p2 != p1) { p1 = p2 + 1; return v; } else { throw soci::soci_error(msg); } } } // namespace anonymous void soci::details::postgresql::parse_std_tm(char const * buf, std::tm & t) { char const * p1 = buf; char * p2; long year, month, day; 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); if (*p2 != '\0') { // there is also the time of day available hour = parse10(p1, p2, errMsg); minute = parse10(p1, p2, errMsg); second = parse10(p1, p2, errMsg); } t.tm_isdst = -1; t.tm_year = year - 1900; t.tm_mon = month - 1; t.tm_mday = day; t.tm_hour = hour; t.tm_min = minute; t.tm_sec = second; std::mktime(&t); } double soci::details::postgresql::string_to_double(char const * buf) { double t; int n; int const converted = sscanf(buf, "%lf%n", &t, &n); if (converted == 1 && static_cast(n) == strlen(buf)) { // successfully converted to double // and no other characters were found in the buffer return t; } else { throw soci_error("Cannot convert data."); } }