00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: Date.cpp 1197 2014-10-14 22:26:11Z sella $";
00010
00011 #include "Date.h"
00012
00013 using namespace sella::variant;
00014
00015 const boost::regex Date::Pattern("^([0-9]{4})-([0-9]{2})-([0-9]{2})$");
00016
00017 Date::Date() throw () : Nullable() { }
00018
00019 Date::Date(const std::string &value) throw (TypeCastException) : Nullable() {
00020 boost::smatch matcher;
00021 struct tm stamp;
00022
00023 if (boost::regex_match(value, matcher, Date::Pattern)) {
00024 memset(&stamp, 0, sizeof(stamp));
00025
00026 stamp.tm_year = strtol(matcher[1].str().c_str(), NULL, 10) - 1900;
00027 stamp.tm_mon = strtol(matcher[2].str().c_str(), NULL, 10) - 1;
00028 stamp.tm_mday = strtol(matcher[3].str().c_str(), NULL, 10);
00029
00030 this->value = std::make_shared<int64_t>(mktime(&stamp) * 1000000LL);
00031 } else {
00032 THROW(TypeCastException, "unable to cast '%s' to a date value", value.c_str());
00033 }
00034 }
00035
00036 Date::Date(int64_t value) throw () : Nullable(std::make_shared<int64_t>(value)) { }
00037
00038 Date::Date(const Date &other) throw () : Nullable(other) {
00039 if (!other.isNull()) {
00040 this->value = std::make_shared<int64_t>(*other.getUsec());
00041 }
00042 }
00043
00044 Date::~Date() throw () { }
00045
00046 bool Date::operator==(const Date &other) const throw () {
00047 return (*std::static_pointer_cast<int64_t>(this->value) == *std::static_pointer_cast<int64_t>(other.value));
00048 }
00049
00050 bool Date::operator!=(const Date &other) const throw () {
00051 return !(*this == other);
00052 }
00053
00054 bool Date::operator>(const Date &other) const throw () {
00055 return (*std::static_pointer_cast<int64_t>(this->value) > *std::static_pointer_cast<int64_t>(other.value));
00056 }
00057
00058 bool Date::operator<(const Date &other) const throw () {
00059 return (*std::static_pointer_cast<int64_t>(this->value) < *std::static_pointer_cast<int64_t>(other.value));
00060 }
00061
00062 Date& Date::operator=(const Date &other) throw () {
00063 if (this != &other) {
00064 Nullable::operator=(other);
00065 if (!other.isNull()) {
00066 this->value = std::make_shared<int64_t>(*other.getUsec());
00067 }
00068 }
00069
00070 return *this;
00071 }
00072
00073 Date& Date::operator+=(int64_t value) throw () {
00074 cache.clear();
00075 (*std::static_pointer_cast<int64_t>(this->value)) += value;
00076
00077 return *this;
00078 }
00079
00080 Date& Date::operator-=(int64_t value) throw () {
00081 cache.clear();
00082 (*std::static_pointer_cast<int64_t>(this->value)) -= value;
00083
00084 return *this;
00085 }
00086
00087 Date& Date::operator*=(int64_t value) throw () {
00088 cache.clear();
00089 (*std::static_pointer_cast<int64_t>(this->value)) *= value;
00090
00091 return *this;
00092 }
00093
00094 Date& Date::operator/=(int64_t value) throw () {
00095 cache.clear();
00096 (*std::static_pointer_cast<int64_t>(this->value)) /= value;
00097
00098 return *this;
00099 }
00100
00101 Date& Date::operator^=(int64_t value) throw () {
00102 cache.clear();
00103 (*std::static_pointer_cast<int64_t>(this->value)) = pow((*std::static_pointer_cast<int64_t>(this->value)), value);
00104
00105 return *this;
00106 }
00107
00108 Date& Date::operator%=(int64_t value) throw () {
00109 cache.clear();
00110 (*std::static_pointer_cast<int64_t>(this->value)) %= value;
00111
00112 return *this;
00113 }
00114
00115 Date& Date::operator&=(int64_t value) throw () {
00116 cache.clear();
00117 (*std::static_pointer_cast<int64_t>(this->value)) &= value;
00118
00119 return *this;
00120 }
00121
00122 Date& Date::operator|=(int64_t value) throw () {
00123 cache.clear();
00124 (*std::static_pointer_cast<int64_t>(this->value)) |= value;
00125
00126 return *this;
00127 }
00128
00129 const int64_t* Date::getUsec() const throw (UnsupportedOperatorException) {
00130 if (isNull()) {
00131 THROW(UnsupportedOperatorException, "unable to take reference of null value");
00132 }
00133
00134 return &(*std::static_pointer_cast<int64_t>(this->value));
00135 }
00136
00137 const std::string& Date::str() const throw () {
00138 if (cache.empty()) {
00139 if (this->isNull()) {
00140 cache = "null";
00141 } else {
00142 char buffer[4096];
00143 time_t epoch = *(this->getUsec()) / 1000000LL;
00144 struct tm human;
00145
00146 gmtime_r(&epoch, &human);
00147 snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d", human.tm_year + 1900, human.tm_mon + 1, human.tm_mday);
00148 cache = buffer;
00149 }
00150 }
00151
00152 return cache;
00153 }
00154
00155
00156
00157