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