9 static const char rcsid[] __attribute__((used)) =
"$Id: TimeStamp.cpp 1911 2017-09-02 17:37:35Z sella $";
11 #include "TimeStamp.h"
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 TimeStamp::TimeStamp() throw () :
Nullable(std::make_shared<int64_t>(0)) { }
18 TimeStamp::TimeStamp(
const std::string &value)
throw (TypeCastException) :
Nullable() {
19 boost::smatch matcher;
24 if (boost::regex_match(value, matcher, TimeStamp::Pattern())) {
25 memset(&stamp, 0,
sizeof(stamp));
27 stamp.tm_year = strtol(matcher[1].str().c_str(), NULL, 10) - 1900;
28 stamp.tm_mon = strtol(matcher[2].str().c_str(), NULL, 10) - 1;
29 stamp.tm_mday = strtol(matcher[3].str().c_str(), NULL, 10);
30 stamp.tm_hour = strtol(matcher[4].str().c_str(), NULL, 10);
31 stamp.tm_min = strtol(matcher[7].str().c_str(), NULL, 10);
32 stamp.tm_sec = (matcher.length(9) > 0) ? strtol(matcher[9].str().c_str(), NULL, 10) : 0;
34 usec = mktime(&stamp) * 1000000LL;
36 switch (matcher.length(11)) {
41 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 100000LL);
45 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 10000LL);
49 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 1000LL);
53 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 100LL);
57 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 10LL);
61 usec += (strtol(matcher[11].str().c_str(), NULL, 10));
64 this->value = std::make_shared<int64_t>(usec);
66 THROW(TypeCastException,
"unable to cast '%s' to a timestamp value", value.c_str());
70 TimeStamp::TimeStamp(int64_t value)
throw () :
Nullable(std::make_shared<int64_t>(value)) { }
74 if (!other.isNull()) {
76 this->value = std::make_shared<int64_t>(other.getUsec());
78 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
86 TimeStamp::~TimeStamp() throw () { }
88 bool TimeStamp::operator==(
const TimeStamp &other)
const throw () {
89 return (*std::static_pointer_cast<int64_t>(this->value) == *std::static_pointer_cast<int64_t>(other.value));
92 bool TimeStamp::operator!=(
const TimeStamp &other)
const throw () {
93 return !(*
this == other);
96 bool TimeStamp::operator>(
const TimeStamp &other)
const throw () {
97 return (*std::static_pointer_cast<int64_t>(this->value) > *std::static_pointer_cast<int64_t>(other.value));
100 bool TimeStamp::operator<(
const TimeStamp &other)
const throw () {
101 return (*std::static_pointer_cast<int64_t>(this->value) < *std::static_pointer_cast<int64_t>(other.value));
105 if (
this != &other) {
106 if (!other.isNull()) {
108 this->value = std::make_shared<int64_t>(other.getUsec());
110 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
120 TimeStamp& TimeStamp::operator+=(int64_t value)
throw () {
121 (*std::static_pointer_cast<int64_t>(this->value)) += value;
126 TimeStamp& TimeStamp::operator-=(int64_t value)
throw () {
127 (*std::static_pointer_cast<int64_t>(this->value)) -= value;
132 TimeStamp& TimeStamp::operator*=(int64_t value)
throw () {
133 (*std::static_pointer_cast<int64_t>(this->value)) *= value;
138 TimeStamp& TimeStamp::operator/=(int64_t value)
throw () {
139 (*std::static_pointer_cast<int64_t>(this->value)) /= value;
144 TimeStamp& TimeStamp::operator^=(int64_t value)
throw () {
145 (*std::static_pointer_cast<int64_t>(this->value)) = pow((*std::static_pointer_cast<int64_t>(this->value)), value);
150 TimeStamp& TimeStamp::operator%=(int64_t value)
throw () {
151 (*std::static_pointer_cast<int64_t>(this->value)) %= value;
156 TimeStamp& TimeStamp::operator&=(int64_t value)
throw () {
157 (*std::static_pointer_cast<int64_t>(this->value)) &= value;
162 TimeStamp& TimeStamp::operator|=(int64_t value)
throw () {
163 (*std::static_pointer_cast<int64_t>(this->value)) |= value;
168 int64_t TimeStamp::getUsec()
const throw (UnsupportedOperatorException) {
170 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
173 return *std::static_pointer_cast<int64_t>(this->value);
179 if (this->isNull()) {
180 if ((c = mpack.append(offset)) == 0) {
181 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
184 if ((c = mpack.append(offset, this->str(JSON))) == 0) {
185 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
192 void TimeStamp::msgpackBufferSize(
size_t &size)
const throw () {
193 if (UNLIKELY(isNull())) {
202 std::string TimeStamp::str(
int type)
const throw () {
203 if (UNLIKELY(this->isNull())) {
209 time_t epoch = this->getUsec() / 1000000LL;
212 gmtime_r(&epoch, &human);
213 snprintf(buffer,
sizeof(buffer),
"%04d-%02d-%02dT%02d:%02d:%02d.%06lld", human.tm_year + 1900, human.tm_mon + 1, human.tm_mday, human.tm_hour, human.tm_min, human.tm_sec, this->getUsec() % 1000000LL);
218 bool TimeStamp::isType(
const char *value,
size_t size)
throw () {
219 return (size > 10 && value[10] ==
'T' && boost::regex_match(std::string(value, size), Pattern()));