1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
11 #include <anna/config/defines.hpp>
12 #include <anna/core/functions.hpp>
14 #include <anna/dbms/Date.hpp>
18 using namespace anna::dbms;
20 Date::Date(const bool isNulleable, const char* format) :
21 Data(Type::Date, MaxDateSize, isNulleable) {
22 Data::setBuffer(a_buffer);
24 a_format = (format == NULL) ? NULL : strdup(format);
25 anna_memset(&a_value, 0, sizeof(a_value));
28 Date::Date(const Data::Type::_v type, const bool isNulleable, const char* format) :
29 Data(type, MaxDateSize, isNulleable) {
30 Data::setBuffer(a_buffer);
32 a_format = (format == NULL) ? NULL : strdup(format);
33 anna_memset(&a_value, 0, sizeof(a_value));
36 Date::Date(const Date& other) :
38 Data::setBuffer(a_buffer);
40 a_format = (other.a_format == NULL) ? NULL : strdup(other.a_format);
41 anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
49 const char* dbms::Date::getCStringValue() const
53 if((format = a_format) == NULL)
54 format = "%d/%m/%Y %H:%M:%S";
56 return (strftime(const_cast <Date*>(this)->a_buffer, MaxDateSize, format, &a_value) == 0) ? NULL : a_buffer;
59 Date& Date::operator = (const Date & other)
60 throw(RuntimeException) {
62 if(other.isNull() == true) {
64 anna_memset(&a_value, 0, sizeof(a_value));
67 anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
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);
83 char* r = strptime(str, a_format, &aux);
86 string msg(asString());
89 msg += " | Can't be interpreted as valid date";
90 throw RuntimeException(msg, ANNA_FILE_LOCATION);
94 anna_memcpy(&a_value, &aux, sizeof(a_value));
97 void Date::setValue(const Second &second)
98 throw(RuntimeException) {
99 tm* aux = localtime((time_t*) & second);
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);
108 Data::setNull(false);
109 anna_memcpy(&a_value, aux, sizeof(a_value));
112 void dbms::Date::set(const char* what, int& variable, const int value, const int min, const int max)
113 throw(RuntimeException) {
116 msg += functions::asText(" must be greater than or equal to ", min);
117 throw RuntimeException(msg, ANNA_FILE_LOCATION);
120 if(value > max && max != -1) {
122 msg += functions::asText(" must be less than or equal to ", max);
123 throw RuntimeException(msg, ANNA_FILE_LOCATION);
126 Data::setNull(false);
130 string dbms::Date::asString() const
133 string result("dbms::Date { ");
134 result += dbms::Data::asString();
135 result += " | Format: ";
136 result += (a_format == NULL) ? "<null>" : a_format;
137 result += " | Value: ";
139 if(Data::isNull() == false) {
140 if((cstring = getCStringValue()) == NULL)
141 result += "<not valid>";
147 return result += " }";