postgresql/common.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #include <soci-platform.h>
9 #include <soci-backend.h>
10 #include <cstdlib>
11 #include <ctime>
12 #include "common.h"
13 
14 namespace // anonymous
15 {
16 
17 // helper function for parsing decimal data (for std::tm)
18 long parse10(char const * & p1, char * & p2, char const * msg)
19 {
20  long v = std::strtol(p1, &p2, 10);
21  if (p2 != p1)
22  {
23  p1 = p2 + 1;
24  return v;
25  }
26  else
27  {
28  throw soci::soci_error(msg);
29  }
30 }
31 
32 } // namespace anonymous
33 
34 
35 void soci::details::postgresql::parse_std_tm(char const * buf, std::tm & t)
36 {
37  char const * p1 = buf;
38  char * p2;
39  char separator;
40  long a, b, c;
41  long year = 1900, month = 1, day = 1;
42  long hour = 0, minute = 0, second = 0;
43 
44  char const * errMsg = "Cannot convert data to std::tm.";
45 
46  a = parse10(p1, p2, errMsg);
47  separator = *p2;
48  b = parse10(p1, p2, errMsg);
49  c = parse10(p1, p2, errMsg);
50 
51  if (*p2 == ' ')
52  {
53  // there are more elements to parse
54  // - assume that what was already parsed is a date part
55  // and that the remaining elements describe the time of day
56  year = a;
57  month = b;
58  day = c;
59  hour = parse10(p1, p2, errMsg);
60  minute = parse10(p1, p2, errMsg);
61  second = parse10(p1, p2, errMsg);
62  }
63  else
64  {
65  // only three values have been parsed
66  if (separator == '-')
67  {
68  // assume the date value was read
69  // (leave the time of day as 00:00:00)
70  year = a;
71  month = b;
72  day = c;
73  }
74  else
75  {
76  // assume the time of day was read
77  // (leave the date part as 1900-01-01)
78  hour = a;
79  minute = b;
80  second = c;
81  }
82  }
83 
84  t.tm_isdst = -1;
85  t.tm_year = year - 1900;
86  t.tm_mon = month - 1;
87  t.tm_mday = day;
88  t.tm_hour = hour;
89  t.tm_min = minute;
90  t.tm_sec = second;
91 
92  std::mktime(&t);
93 }
94 
96 {
97  double t;
98  int n;
99  int const converted = sscanf(buf, "%lf%n", &t, &n);
100  if (converted == 1 && static_cast<std::size_t>(n) == strlen(buf))
101  {
102  // successfully converted to double
103  // and no other characters were found in the buffer
104 
105  return t;
106  }
107  else
108  {
109  throw soci_error("Cannot convert data.");
110  }
111 }
void parse_std_tm(char const *buf, std::tm &t)
double string_to_double(char const *buf)


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40