libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #ifndef MOOST_POSIX_TIME_TIMESTAMP_HPP__ 00029 #define MOOST_POSIX_TIME_TIMESTAMP_HPP__ 00030 00031 #include <string> 00032 #include <sstream> 00033 00034 #include <boost/regex.hpp> 00035 #include <boost/date_time/gregorian/conversion.hpp> 00036 #include <boost/date_time/posix_time/posix_time.hpp> 00037 #include <boost/operators.hpp> 00038 00039 namespace moost { namespace posix_time { 00040 00041 class universal_timebase 00042 { 00043 public: 00044 static boost::posix_time::ptime now() 00045 { 00046 return boost::posix_time::second_clock::universal_time(); 00047 } 00048 00049 static boost::posix_time::ptime base() 00050 { 00051 return boost::posix_time::ptime(boost::gregorian::date(1970,1,1)); 00052 } 00053 }; 00054 00055 template <class TimeBasePolicy> 00056 class basic_timestamp : private boost::less_than_comparable< basic_timestamp<TimeBasePolicy> > 00057 , boost::less_than_comparable< basic_timestamp<TimeBasePolicy>, boost::posix_time::ptime > 00058 , boost::less_than_comparable< basic_timestamp<TimeBasePolicy>, time_t > 00059 , boost::equality_comparable< basic_timestamp<TimeBasePolicy> > 00060 , boost::equality_comparable< basic_timestamp<TimeBasePolicy>, boost::posix_time::ptime > 00061 , boost::equality_comparable< basic_timestamp<TimeBasePolicy>, time_t > 00062 { 00063 public: 00064 basic_timestamp() 00065 { 00066 } 00067 00068 explicit basic_timestamp(const boost::posix_time::ptime& t) 00069 : m_time(t) 00070 { 00071 } 00072 00073 explicit basic_timestamp(time_t t) 00074 { 00075 assign(t); 00076 } 00077 00078 basic_timestamp(const std::string& str) 00079 { 00080 assign(str); 00081 } 00082 00083 void assign(time_t t) 00084 { 00085 m_time = boost::posix_time::from_time_t(t); 00086 } 00087 00088 void assign(const boost::posix_time::ptime& t) 00089 { 00090 m_time = t; 00091 } 00092 00093 void assign(const std::string& str) 00094 { 00095 if (boost::regex_match(str, boost::regex("\\d+"))) 00096 { 00097 std::istringstream iss(str); 00098 time_t t; 00099 iss >> t; 00100 assign(t); 00101 } 00102 else 00103 { 00104 boost::smatch what; 00105 00106 if (boost::regex_match(str, what, boost::regex("([+-]?)(\\d+)\\s*(h(?:ours?)?|d(?:ays?)?|w(?:eeks?)?|m(?:onths?)?|y(?:ears?)?)"))) 00107 { 00108 std::istringstream iss(what[2]); 00109 int h; 00110 iss >> h; 00111 00112 switch (what.str(3).at(0)) 00113 { 00114 case 'y': h *= 24*365; break; 00115 case 'm': h *= 24*30; break; 00116 case 'w': h *= 24*7; break; 00117 case 'd': h *= 24; break; 00118 default: 00119 break; 00120 } 00121 00122 m_time = TimeBasePolicy::now(); 00123 00124 if (what[1] == "-") 00125 { 00126 m_time -= boost::posix_time::hours(h); 00127 } 00128 else 00129 { 00130 m_time += boost::posix_time::hours(h); 00131 } 00132 } 00133 else 00134 { 00135 try 00136 { 00137 m_time = boost::posix_time::from_iso_string(str); 00138 } 00139 catch (const std::bad_cast&) 00140 { 00141 m_time = boost::posix_time::time_from_string(str); 00142 } 00143 } 00144 } 00145 } 00146 00147 time_t as_time_t() const 00148 { 00149 return static_cast<time_t>((m_time - TimeBasePolicy::base()).total_seconds()); 00150 } 00151 00152 std::string as_iso_string() const 00153 { 00154 return boost::posix_time::to_iso_string(m_time); 00155 } 00156 00157 const boost::posix_time::ptime& as_ptime() const 00158 { 00159 return m_time; 00160 } 00161 00162 void operator=(time_t t) 00163 { 00164 assign(t); 00165 } 00166 00167 void operator=(const boost::posix_time::ptime& t) 00168 { 00169 assign(t); 00170 } 00171 00172 void operator=(const std::string& str) 00173 { 00174 assign(str); 00175 } 00176 00177 bool operator==(const basic_timestamp& ts) const { return m_time == ts.m_time; } 00178 bool operator<(const basic_timestamp& ts) const { return m_time < ts.m_time; } 00179 00180 bool operator==(const boost::posix_time::ptime& t) const { return m_time == t; } 00181 bool operator<(const boost::posix_time::ptime& t) const { return m_time < t; } 00182 bool operator>(const boost::posix_time::ptime& t) const { return m_time > t; } 00183 00184 bool operator==(time_t t) const { return m_time == boost::posix_time::from_time_t(t); } 00185 bool operator<(time_t t) const { return m_time < boost::posix_time::from_time_t(t); } 00186 bool operator>(time_t t) const { return m_time > boost::posix_time::from_time_t(t); } 00187 00188 private: 00189 boost::posix_time::ptime m_time; 00190 }; 00191 00192 typedef basic_timestamp<universal_timebase> timestamp; 00193 00194 }} 00195 00196 #endif