9 static const char rcsid[] __attribute__((used)) =
"$Id: Time.cpp 1911 2017-09-02 17:37:35Z sella $";
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 Time::Time() throw () :
Nullable(std::make_shared<int64_t>(0)) { }
18 Time::Time(
const std::string &value)
throw (TypeCastException) :
Nullable() {
19 boost::smatch matcher;
21 if (boost::regex_match(value, matcher, Time::Pattern())) {
22 int64_t usec = (strtol(matcher[1].str().c_str(), NULL, 10) * 3600000000LL);
24 usec += (strtol(matcher[4].str().c_str(), NULL, 10) * 60000000LL);
25 usec += ((matcher.length(6) > 0) ? (strtol(matcher[6].str().c_str(), NULL, 10) * 1000000LL) : 0);
27 switch (matcher.length(8)) {
32 usec += (strtol(matcher[8].str().c_str(), NULL, 10) * 100000LL);
36 usec += (strtol(matcher[8].str().c_str(), NULL, 10) * 10000LL);
40 usec += (strtol(matcher[8].str().c_str(), NULL, 10) * 1000LL);
44 usec += (strtol(matcher[8].str().c_str(), NULL, 10) * 100LL);
48 usec += (strtol(matcher[8].str().c_str(), NULL, 10) * 10LL);
52 usec += (strtol(matcher[8].str().c_str(), NULL, 10));
56 this->value = std::make_shared<int64_t>(usec);
58 THROW(TypeCastException,
"unable to cast '%s' to a time value", value.c_str());
62 Time::Time(int64_t value)
throw () :
Nullable(std::make_shared<int64_t>(value)) { }
66 if (!other.isNull()) {
68 this->value = std::make_shared<int64_t>(other.getUsec());
70 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
78 Time::~Time() throw () { }
80 bool Time::operator==(
const Time &other)
const throw () {
81 return (*std::static_pointer_cast<int64_t>(this->value) == *std::static_pointer_cast<int64_t>(other.value));
84 bool Time::operator!=(
const Time &other)
const throw () {
85 return !(*
this == other);
88 bool Time::operator>(
const Time &other)
const throw () {
89 return (*std::static_pointer_cast<int64_t>(this->value) > *std::static_pointer_cast<int64_t>(other.value));
92 bool Time::operator<(
const Time &other)
const throw () {
93 return (*std::static_pointer_cast<int64_t>(this->value) < *std::static_pointer_cast<int64_t>(other.value));
96 Time& Time::operator=(
const Time &other)
throw () {
98 if (!other.isNull()) {
100 this->value = std::make_shared<int64_t>(other.getUsec());
102 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
112 Time& Time::operator+=(int64_t value)
throw () {
113 (*std::static_pointer_cast<int64_t>(this->value)) += value;
118 Time& Time::operator-=(int64_t value)
throw () {
119 (*std::static_pointer_cast<int64_t>(this->value)) -= value;
124 Time& Time::operator*=(int64_t value)
throw () {
125 (*std::static_pointer_cast<int64_t>(this->value)) *= value;
130 Time& Time::operator/=(int64_t value)
throw () {
131 (*std::static_pointer_cast<int64_t>(this->value)) /= value;
136 Time& Time::operator^=(int64_t value)
throw () {
137 (*std::static_pointer_cast<int64_t>(this->value)) = pow((*std::static_pointer_cast<int64_t>(this->value)), value);
142 Time& Time::operator%=(int64_t value)
throw () {
143 (*std::static_pointer_cast<int64_t>(this->value)) %= value;
148 Time& Time::operator&=(int64_t value)
throw () {
149 (*std::static_pointer_cast<int64_t>(this->value)) &= value;
154 Time& Time::operator|=(int64_t value)
throw () {
155 (*std::static_pointer_cast<int64_t>(this->value)) |= value;
160 int64_t Time::getUsec()
const throw (UnsupportedOperatorException) {
162 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
165 return *std::static_pointer_cast<int64_t>(this->value);
171 if (UNLIKELY(this->isNull())) {
172 c = mpack.append(offset);
174 c = mpack.append(offset, this->str(JSON));
179 if (UNLIKELY(c == 0)) {
180 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
184 void Time::msgpackBufferSize(
size_t &size)
const throw () {
185 if (UNLIKELY(isNull())) {
194 std::string Time::str(
int type)
const throw () {
195 if (UNLIKELY(this->isNull())) {
200 time_t epoch = this->getUsec() / 1000000LL;
203 gmtime_r(&epoch, &human);
204 snprintf(buffer,
sizeof(buffer),
"%02d:%02d:%02d.%06lld", human.tm_hour, human.tm_min, human.tm_sec, this->getUsec() % 1000000LL);
209 bool Time::isType(
const char *value,
size_t size)
throw () {
210 return (size > 7 && value[2] ==
':' && value [5] ==
':' && boost::regex_match(std::string(value, size), Pattern()));