sqlite3/common.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 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 "common.h"
10 #include "soci-backend.h"
11 // std
12 #include <cstdlib>
13 #include <ctime>
14 
15 
16 namespace // anonymous
17 {
18 
19 // helper function for parsing decimal data (for std::tm)
20 long parse10(char const *&p1, char *&p2, char const* const msg)
21 {
22  long v = std::strtol(p1, &p2, 10);
23  if (p2 != p1)
24  {
25  p1 = p2 + 1;
26  return v;
27  }
28  else
29  {
30  throw soci::soci_error(msg);
31  }
32 }
33 
34 } // namespace anonymous
35 
36 
37 void soci::details::sqlite3::parse_std_tm(char const *buf, std::tm &t)
38 {
39  char const *p1 = buf;
40  char *p2 = 0;
41 
42  char const* const errMsg = "Cannot convert data to std::tm.";
43 
44  long year = parse10(p1, p2, errMsg);
45  long month = parse10(p1, p2, errMsg);
46  long day = parse10(p1, p2, errMsg);
47 
48  long hour = 0, minute = 0, second = 0;
49  if (*p2 != '\0')
50  {
51  // there is also the time of day available
52  hour = parse10(p1, p2, errMsg);
53  minute = parse10(p1, p2, errMsg);
54  second = parse10(p1, p2, errMsg);
55  }
56 
57  t.tm_isdst = -1;
58  t.tm_year = year - 1900;
59  t.tm_mon = month - 1;
60  t.tm_mday = day;
61  t.tm_hour = hour;
62  t.tm_min = minute;
63  t.tm_sec = second;
64 
65  std::mktime(&t);
66 }
67 
void parse_std_tm(char const *buf, std::tm &t)


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