00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: TimeStamp.cpp 1197 2014-10-14 22:26:11Z sella $";
00010
00011 #include "TimeStamp.h"
00012
00013 using namespace sella::variant;
00014
00015 const boost::regex TimeStamp::Pattern("^([0-9]{4})-([0-9]{2})-([0-9]{2})T(([01][0-9])|(2[0-3])):([0-5][0-9])(:([0-5][0-9])(\\.([0-9]{1,6}))?)?$");
00016
00017 TimeStamp::TimeStamp() throw () : Nullable() { }
00018
00019 TimeStamp::TimeStamp(const std::string &value) throw (TypeCastException) : Nullable() {
00020 boost::smatch matcher;
00021
00022 struct tm stamp;
00023 int64_t usec;
00024
00025 if (boost::regex_match(value, matcher, TimeStamp::Pattern)) {
00026 memset(&stamp, 0, sizeof(stamp));
00027
00028 stamp.tm_year = strtol(matcher[1].str().c_str(), NULL, 10) - 1900;
00029 stamp.tm_mon = strtol(matcher[2].str().c_str(), NULL, 10) - 1;
00030 stamp.tm_mday = strtol(matcher[3].str().c_str(), NULL, 10);
00031 stamp.tm_hour = strtol(matcher[4].str().c_str(), NULL, 10);
00032 stamp.tm_min = strtol(matcher[7].str().c_str(), NULL, 10);
00033 stamp.tm_sec = (matcher.length(9) > 0) ? strtol(matcher[9].str().c_str(), NULL, 10) : 0;
00034
00035 usec = mktime(&stamp) * 1000000LL;
00036
00037 switch (matcher.length(11)) {
00038 case 0:
00039 break;
00040
00041 case 1:
00042 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 100000LL);
00043 break;
00044
00045 case 2:
00046 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 10000LL);
00047 break;
00048
00049 case 3:
00050 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 1000LL);
00051 break;
00052
00053 case 4:
00054 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 100LL);
00055 break;
00056
00057 case 5:
00058 usec += (strtol(matcher[11].str().c_str(), NULL, 10) * 10LL);
00059 break;
00060
00061 case 6:
00062 usec += (strtol(matcher[11].str().c_str(), NULL, 10));
00063 break;
00064 }
00065 this->value = std::make_shared<int64_t>(usec);
00066 } else {
00067 THROW(TypeCastException, "unable to cast '%s' to a timestamp value", value.c_str());
00068 }
00069 }
00070
00071 TimeStamp::TimeStamp(int64_t value) throw () : Nullable(std::make_shared<int64_t>(value)) { }
00072
00073 TimeStamp::TimeStamp(const TimeStamp &other) throw () : Nullable(other) {
00074 if (!other.isNull()) {
00075 this->value = std::make_shared<int64_t>(*other.getUsec());
00076 }
00077 }
00078
00079 TimeStamp::~TimeStamp() throw () { }
00080
00081 bool TimeStamp::operator==(const TimeStamp &other) const throw () {
00082 return (*std::static_pointer_cast<int64_t>(this->value) == *std::static_pointer_cast<int64_t>(other.value));
00083 }
00084
00085 bool TimeStamp::operator!=(const TimeStamp &other) const throw () {
00086 return !(*this == other);
00087 }
00088
00089 bool TimeStamp::operator>(const TimeStamp &other) const throw () {
00090 return (*std::static_pointer_cast<int64_t>(this->value) > *std::static_pointer_cast<int64_t>(other.value));
00091 }
00092
00093 bool TimeStamp::operator<(const TimeStamp &other) const throw () {
00094 return (*std::static_pointer_cast<int64_t>(this->value) < *std::static_pointer_cast<int64_t>(other.value));
00095 }
00096
00097 TimeStamp& TimeStamp::operator=(const TimeStamp &other) throw () {
00098 if (this != &other) {
00099 Nullable::operator=(other);
00100 if (!other.isNull()) {
00101 this->value = std::make_shared<int64_t>(*other.getUsec());
00102 }
00103 }
00104
00105 return *this;
00106 }
00107
00108 TimeStamp& TimeStamp::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 TimeStamp& TimeStamp::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 TimeStamp& TimeStamp::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 TimeStamp& TimeStamp::operator/=(int64_t value) throw () {
00130 cache.clear();
00131 (*std::static_pointer_cast<int64_t>(this->value)) /= value;
00132
00133 return *this;
00134 }
00135
00136 TimeStamp& TimeStamp::operator^=(int64_t value) throw () {
00137 cache.clear();
00138 (*std::static_pointer_cast<int64_t>(this->value)) = pow((*std::static_pointer_cast<int64_t>(this->value)), value);
00139
00140 return *this;
00141 }
00142
00143 TimeStamp& TimeStamp::operator%=(int64_t value) throw () {
00144 cache.clear();
00145 (*std::static_pointer_cast<int64_t>(this->value)) %= value;
00146
00147 return *this;
00148 }
00149
00150 TimeStamp& TimeStamp::operator&=(int64_t value) throw () {
00151 cache.clear();
00152 (*std::static_pointer_cast<int64_t>(this->value)) &= value;
00153
00154 return *this;
00155 }
00156
00157 TimeStamp& TimeStamp::operator|=(int64_t value) throw () {
00158 cache.clear();
00159 (*std::static_pointer_cast<int64_t>(this->value)) |= value;
00160
00161 return *this;
00162 }
00163
00164 const int64_t* TimeStamp::getUsec() const throw (UnsupportedOperatorException) {
00165 if (isNull()) {
00166 THROW(UnsupportedOperatorException, "unable to take reference of null value");
00167 }
00168
00169 return &(*std::static_pointer_cast<int64_t>(this->value));
00170 }
00171
00172 const std::string& TimeStamp::str() const throw () {
00173 if (cache.empty()) {
00174 if (this->isNull()) {
00175 cache = "null";
00176 } else {
00177 char buffer[4096];
00178
00179 time_t epoch = *(this->getUsec()) / 1000000LL;
00180 struct tm human;
00181
00182 gmtime_r(&epoch, &human);
00183 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);
00184 cache = buffer;
00185 }
00186 }
00187
00188 return cache;
00189 }
00190
00191
00192
00193