Add no-deprecated to warnings due to dynamic exceptions.
[anna.git] / source / dbms / Date.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <string.h>
10
11 #include <anna/config/defines.hpp>
12 #include <anna/core/functions.hpp>
13
14 #include <anna/dbms/Date.hpp>
15
16 using namespace std;
17 using namespace anna;
18 using namespace anna::dbms;
19
20 Date::Date(const bool isNulleable, const char* format) :
21   Data(Type::Date, MaxDateSize, isNulleable) {
22   Data::setBuffer(a_buffer);
23   a_buffer [0] = 0;
24   a_format = (format == NULL) ? NULL : strdup(format);
25   anna_memset(&a_value, 0, sizeof(a_value));
26 }
27
28 Date::Date(const Data::Type::_v type,  const bool isNulleable, const char* format) :
29   Data(type, MaxDateSize, isNulleable) {
30   Data::setBuffer(a_buffer);
31   a_buffer [0] = 0;
32   a_format = (format == NULL) ? NULL : strdup(format);
33   anna_memset(&a_value, 0, sizeof(a_value));
34 }
35
36 Date::Date(const Date& other) :
37   Data(other) {
38   Data::setBuffer(a_buffer);
39   a_buffer [0] = 0;
40   a_format = (other.a_format == NULL) ? NULL : strdup(other.a_format);
41   anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
42 }
43
44 Date::~Date() {
45   if(a_format != NULL)
46     free(a_format);
47 }
48
49 const char* dbms::Date::getCStringValue() const
50 throw() {
51   const char* format;
52
53   if((format = a_format) == NULL)
54     format = "%d/%m/%Y %H:%M:%S";
55
56   return (strftime(const_cast <Date*>(this)->a_buffer, MaxDateSize, format, &a_value) == 0) ? NULL : a_buffer;
57 }
58
59 Date& Date::operator = (const Date & other)
60 throw(RuntimeException) {
61   if(this != &other) {
62     if(other.isNull() == true) {
63       setNull(true);
64       anna_memset(&a_value, 0, sizeof(a_value));
65     } else {
66       setNull(false);
67       anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
68     }
69   }
70
71   return *this;
72 }
73
74 void Date::setValue(const char* str)
75 throw(RuntimeException) {
76   if(a_format == NULL) {
77     string msg(asString());
78     msg += "  | anna::dbms::Data::setValue (const char*) requires format especification";
79     throw RuntimeException(msg, ANNA_FILE_LOCATION);
80   }
81
82   tm aux;
83   char* r = strptime(str, a_format, &aux);
84
85   if(r == NULL) {
86     string msg(asString());
87     msg += " | String: ";
88     msg += str;
89     msg += " | Can't be interpreted as valid date";
90     throw RuntimeException(msg, ANNA_FILE_LOCATION);
91   }
92
93   Data::setNull(false);
94   anna_memcpy(&a_value, &aux, sizeof(a_value));
95 }
96
97 void Date::setValue(const Second &second)
98 throw(RuntimeException) {
99   tm* aux = localtime((time_t*) & second);
100
101   if(aux == NULL) {
102     string msg(asString());
103     msg += functions::asText(" | Second: ", (int) second);
104     msg += " | Can't be interpreted as valid date";
105     throw RuntimeException(msg, ANNA_FILE_LOCATION);
106   }
107
108   Data::setNull(false);
109   anna_memcpy(&a_value, aux, sizeof(a_value));
110 }
111
112 void dbms::Date::set(const char* what, int& variable, const int value, const int min, const int max)
113 throw(RuntimeException) {
114   if(value < min) {
115     string msg(what);
116     msg += functions::asText(" must be greater than or equal to ", min);
117     throw RuntimeException(msg, ANNA_FILE_LOCATION);
118   }
119
120   if(value > max && max != -1) {
121     string msg(what);
122     msg += functions::asText(" must be less than or equal to ", max);
123     throw RuntimeException(msg, ANNA_FILE_LOCATION);
124   }
125
126   Data::setNull(false);
127   variable = value;
128 }
129
130 string dbms::Date::asString() const
131 throw() {
132   const char* cstring;
133   string result("dbms::Date { ");
134   result += dbms::Data::asString();
135   result += " | Format: ";
136   result += (a_format == NULL) ? "<null>" : a_format;
137   result += " | Value: ";
138
139   if(Data::isNull() == false) {
140     if((cstring = getCStringValue()) == NULL)
141       result += "<not valid>";
142     else
143       result += cstring;
144   } else
145     result += "<null>";
146
147   return result += " }";
148 }
149