Updated license
[anna.git] / source / dbms / Date.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <string.h>
38
39 #include <anna/config/defines.hpp>
40 #include <anna/core/functions.hpp>
41
42 #include <anna/dbms/Date.hpp>
43
44 using namespace std;
45 using namespace anna;
46 using namespace anna::dbms;
47
48 Date::Date(const bool isNulleable, const char* format) :
49   Data(Type::Date, MaxDateSize, isNulleable) {
50   Data::setBuffer(a_buffer);
51   a_buffer [0] = 0;
52   a_format = (format == NULL) ? NULL : strdup(format);
53   anna_memset(&a_value, 0, sizeof(a_value));
54 }
55
56 Date::Date(const Data::Type::_v type,  const bool isNulleable, const char* format) :
57   Data(type, MaxDateSize, isNulleable) {
58   Data::setBuffer(a_buffer);
59   a_buffer [0] = 0;
60   a_format = (format == NULL) ? NULL : strdup(format);
61   anna_memset(&a_value, 0, sizeof(a_value));
62 }
63
64 Date::Date(const Date& other) :
65   Data(other) {
66   Data::setBuffer(a_buffer);
67   a_buffer [0] = 0;
68   a_format = (other.a_format == NULL) ? NULL : strdup(other.a_format);
69   anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
70 }
71
72 Date::~Date() {
73   if(a_format != NULL)
74     free(a_format);
75 }
76
77 const char* dbms::Date::getCStringValue() const
78 throw() {
79   const char* format;
80
81   if((format = a_format) == NULL)
82     format = "%d/%m/%Y %H:%M:%S";
83
84   return (strftime(const_cast <Date*>(this)->a_buffer, MaxDateSize, format, &a_value) == 0) ? NULL : a_buffer;
85 }
86
87 Date& Date::operator = (const Date & other)
88 throw(RuntimeException) {
89   if(this != &other) {
90     if(other.isNull() == true) {
91       setNull(true);
92       anna_memset(&a_value, 0, sizeof(a_value));
93     } else {
94       setNull(false);
95       anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
96     }
97   }
98
99   return *this;
100 }
101
102 void Date::setValue(const char* str)
103 throw(RuntimeException) {
104   if(a_format == NULL) {
105     string msg(asString());
106     msg += "  | anna::dbms::Data::setValue (const char*) requires format especification";
107     throw RuntimeException(msg, ANNA_FILE_LOCATION);
108   }
109
110   tm aux;
111   char* r = strptime(str, a_format, &aux);
112
113   if(r == NULL) {
114     string msg(asString());
115     msg += " | String: ";
116     msg += str;
117     msg += " | Can't be interpreted as valid date";
118     throw RuntimeException(msg, ANNA_FILE_LOCATION);
119   }
120
121   Data::setNull(false);
122   anna_memcpy(&a_value, &aux, sizeof(a_value));
123 }
124
125 void Date::setValue(const Second &second)
126 throw(RuntimeException) {
127   tm* aux = localtime((time_t*) & second);
128
129   if(aux == NULL) {
130     string msg(asString());
131     msg += functions::asText(" | Second: ", (int) second);
132     msg += " | Can't be interpreted as valid date";
133     throw RuntimeException(msg, ANNA_FILE_LOCATION);
134   }
135
136   Data::setNull(false);
137   anna_memcpy(&a_value, aux, sizeof(a_value));
138 }
139
140 void dbms::Date::set(const char* what, int& variable, const int value, const int min, const int max)
141 throw(RuntimeException) {
142   if(value < min) {
143     string msg(what);
144     msg += functions::asText(" must be greater than or equal to ", min);
145     throw RuntimeException(msg, ANNA_FILE_LOCATION);
146   }
147
148   if(value > max && max != -1) {
149     string msg(what);
150     msg += functions::asText(" must be less than or equal to ", max);
151     throw RuntimeException(msg, ANNA_FILE_LOCATION);
152   }
153
154   Data::setNull(false);
155   variable = value;
156 }
157
158 string dbms::Date::asString() const
159 throw() {
160   const char* cstring;
161   string result("dbms::Date { ");
162   result += dbms::Data::asString();
163   result += " | Format: ";
164   result += (a_format == NULL) ? "<null>" : a_format;
165   result += " | Value: ";
166
167   if(Data::isNull() == false) {
168     if((cstring = getCStringValue()) == NULL)
169       result += "<not valid>";
170     else
171       result += cstring;
172   } else
173     result += "<null>";
174
175   return result += " }";
176 }
177