SOCI Logo Get SOCI at SourceForge.net. Fast, secure and Free Open Source software downloads

common.cpp

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
00003 // Distributed under the Boost Software License, Version 1.0.
00004 // (See accompanying file LICENSE_1_0.txt or copy at
00005 // http://www.boost.org/LICENSE_1_0.txt)
00006 //
00007 
00008 #include "common.h"
00009 //
00010 #if defined(SOCI_HEADERS_BURIED)
00011 #       include <soci/core/soci-backend.h>
00012 #else
00013 #       include <soci-backend.h>
00014 #endif
00015 // std
00016 #include <cstdlib>
00017 #include <ctime>
00018 
00019 
00020 namespace // anonymous
00021 {
00022 
00023 // helper function for parsing decimal data (for std::tm)
00024 long parse10(char const *&p1, char *&p2, char const* const msg)
00025 {
00026     long v = std::strtol(p1, &p2, 10);
00027     if (p2 != p1)
00028     {
00029         p1 = p2 + 1;
00030         return v;
00031     }
00032     else
00033     {
00034         throw soci::soci_error(msg);
00035     }
00036 }
00037 
00038 } // namespace anonymous
00039 
00040 
00041 void soci::details::sqlite3::parseStdTm(char const *buf, std::tm &t)
00042 {
00043     char const *p1 = buf;
00044     char *p2;
00045     long year, month, day;
00046     long hour = 0, minute = 0, second = 0;
00047 
00048     char const* const errMsg = "Cannot convert data to std::tm.";
00049 
00050     year  = parse10(p1, p2, errMsg);
00051     month = parse10(p1, p2, errMsg);
00052     day   = parse10(p1, p2, errMsg);
00053 
00054     if (*p2 != '\0')
00055     {
00056         // there is also the time of day available
00057         hour   = parse10(p1, p2, errMsg);
00058         minute = parse10(p1, p2, errMsg);
00059         second = parse10(p1, p2, errMsg);
00060     }
00061 
00062     t.tm_isdst = -1;
00063     t.tm_year = year - 1900;
00064     t.tm_mon  = month - 1;
00065     t.tm_mday = day;
00066     t.tm_hour = hour;
00067     t.tm_min  = minute;
00068     t.tm_sec  = second;
00069 
00070     std::mktime(&t);
00071 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
SourceForge Logo

Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1