9 static const char rcsid[] __attribute__((used)) =
"$Id: Date.cpp 1911 2017-09-02 17:37:35Z sella $";
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 Date::Date() throw () :
Nullable(std::make_shared<int64_t>(0)) { }
18 Date::Date(
const std::string &value)
throw (TypeCastException) :
Nullable() {
19 boost::smatch matcher;
22 if (boost::regex_match(value, matcher, Date::Pattern())) {
23 memset(&stamp, 0,
sizeof(stamp));
25 stamp.tm_year = strtol(matcher[1].str().c_str(), NULL, 10) - 1900;
26 stamp.tm_mon = strtol(matcher[2].str().c_str(), NULL, 10) - 1;
27 stamp.tm_mday = strtol(matcher[3].str().c_str(), NULL, 10);
29 this->value = std::make_shared<int64_t>(mktime(&stamp) * 1000000LL);
31 THROW(TypeCastException,
"unable to cast '%s' to a date value", value.c_str());
35 Date::Date(int64_t value)
throw () :
Nullable(std::make_shared<int64_t>(value)) { }
39 if (!other.isNull()) {
41 this->value = std::make_shared<int64_t>(other.getUsec());
43 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
51 Date::~Date() throw () { }
53 bool Date::operator==(
const Date &other)
const throw () {
54 return (*std::static_pointer_cast<int64_t>(this->value) == *std::static_pointer_cast<int64_t>(other.value));
57 bool Date::operator!=(
const Date &other)
const throw () {
58 return !(*
this == other);
61 bool Date::operator>(
const Date &other)
const throw () {
62 return (*std::static_pointer_cast<int64_t>(this->value) > *std::static_pointer_cast<int64_t>(other.value));
65 bool Date::operator<(
const Date &other)
const throw () {
66 return (*std::static_pointer_cast<int64_t>(this->value) < *std::static_pointer_cast<int64_t>(other.value));
69 Date& Date::operator=(
const Date &other)
throw () {
71 if (!other.isNull()) {
73 this->value = std::make_shared<int64_t>(other.getUsec());
75 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
85 Date& Date::operator+=(int64_t value)
throw () {
86 (*std::static_pointer_cast<int64_t>(this->value)) += value;
91 Date& Date::operator-=(int64_t value)
throw () {
92 (*std::static_pointer_cast<int64_t>(this->value)) -= value;
97 Date& Date::operator*=(int64_t value)
throw () {
98 (*std::static_pointer_cast<int64_t>(this->value)) *= value;
103 Date& Date::operator/=(int64_t value)
throw () {
104 (*std::static_pointer_cast<int64_t>(this->value)) /= value;
109 Date& Date::operator^=(int64_t value)
throw () {
110 (*std::static_pointer_cast<int64_t>(this->value)) = pow((*std::static_pointer_cast<int64_t>(this->value)), value);
115 Date& Date::operator%=(int64_t value)
throw () {
116 (*std::static_pointer_cast<int64_t>(this->value)) %= value;
121 Date& Date::operator&=(int64_t value)
throw () {
122 (*std::static_pointer_cast<int64_t>(this->value)) &= value;
127 Date& Date::operator|=(int64_t value)
throw () {
128 (*std::static_pointer_cast<int64_t>(this->value)) |= value;
133 int64_t Date::getUsec()
const throw (UnsupportedOperatorException) {
135 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
138 return *std::static_pointer_cast<int64_t>(this->value);
144 if (UNLIKELY(this->isNull())) {
145 c = mpack.append(offset);
147 c = mpack.append(offset, this->str(JSON));
152 if (UNLIKELY(c == 0)) {
153 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
157 void Date::msgpackBufferSize(
size_t &size)
const throw () {
158 if (UNLIKELY(isNull())) {
167 std::string Date::str(
int type)
const throw () {
168 if (UNLIKELY(this->isNull())) {
173 time_t epoch = this->getUsec() / 1000000LL;
176 gmtime_r(&epoch, &human);
177 snprintf(buffer,
sizeof(buffer),
"%04d-%02d-%02d", human.tm_year + 1900, human.tm_mon + 1, human.tm_mday);
182 bool Date::isType(
const char *value,
size_t size)
throw () {
183 return (size == 10 && value[4] ==
'-' && value[7] ==
'-' && boost::regex_match(std::string(value, size), Pattern()));