Warning, file /sdk/codevis/thirdparty/soci/src/core/common.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
0003 // Copyright (C) 2017 Vadim Zeitlin.
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 
0009 #define SOCI_SOURCE
0010 #include "soci/error.h"
0011 #include "soci-mktime.h"
0012 #include <climits>
0013 #include <cstdlib>
0014 #include <ctime>
0015 
0016 namespace // anonymous
0017 {
0018 
0019 // helper function for parsing decimal data (for std::tm)
0020 int parse10(char const * & p1, char * & p2)
0021 {
0022     long v = std::strtol(p1, &p2, 10);
0023     if (p2 != p1)
0024     {
0025         if (v < 0)
0026             throw soci::soci_error("Negative date/time field component.");
0027 
0028         if (v > INT_MAX)
0029             throw soci::soci_error("Out of range date/time field component.");
0030 
0031         p1 = p2 + 1;
0032 
0033         // Cast is safe due to check above.
0034         return static_cast<int>(v);
0035     }
0036     else
0037     {
0038         throw soci::soci_error("Cannot parse date/time field component.");
0039 
0040     }
0041 }
0042 
0043 } // namespace anonymous
0044 
0045 void soci::details::parse_std_tm(char const * buf, std::tm & t)
0046 {
0047     char const * p1 = buf;
0048     char * p2;
0049     char separator;
0050     int a, b, c;
0051     int year = 1900, month = 1, day = 1;
0052     int hour = 0, minute = 0, second = 0;
0053 
0054     a = parse10(p1, p2);
0055     separator = *p2;
0056     b = parse10(p1, p2);
0057     c = parse10(p1, p2);
0058 
0059     if (*p2 == ' ')
0060     {
0061         // there are more elements to parse
0062         // - assume that what was already parsed is a date part
0063         // and that the remaining elements describe the time of day
0064         year = a;
0065         month = b;
0066         day = c;
0067         hour   = parse10(p1, p2);
0068         minute = parse10(p1, p2);
0069         second = parse10(p1, p2);
0070     }
0071     else
0072     {
0073         // only three values have been parsed
0074         if (separator == '-')
0075         {
0076             // assume the date value was read
0077             // (leave the time of day as 00:00:00)
0078             year = a;
0079             month = b;
0080             day = c;
0081         }
0082         else
0083         {
0084             // assume the time of day was read
0085             // (leave the date part as 1900-01-01)
0086             hour = a;
0087             minute = b;
0088             second = c;
0089         }
0090     }
0091 
0092     mktime_from_ymdhms(t, year, month, day, hour, minute, second);
0093 }