pion-net
4.0.9
|
00001 // ----------------------------------------------------------------------- 00002 // pion-common: a collection of common libraries used by the Pion Platform 00003 // ----------------------------------------------------------------------- 00004 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com) 00005 // 00006 // Distributed under the Boost Software License, Version 1.0. 00007 // See http://www.boost.org/LICENSE_1_0.txt 00008 // 00009 00010 #ifndef __PION_PIONDATETIME_HEADER__ 00011 #define __PION_PIONDATETIME_HEADER__ 00012 00013 #include <string> 00014 #include <istream> 00015 #include <ostream> 00016 #include <sstream> 00017 #include <boost/date_time/posix_time/posix_time.hpp> 00018 #include <boost/date_time/gregorian/greg_date.hpp> 00019 #include <pion/PionConfig.hpp> 00020 00021 00022 namespace pion { // begin namespace pion 00023 00024 00026 typedef boost::posix_time::ptime PionDateTime; 00027 00028 00029 00030 00034 class PionTimeFacet 00035 { 00036 public: 00037 00038 // Function that converts a ptime into a time_t 00039 // Note: this is quick & dirty -- does not handle invalid dates, 00040 // other calendars, pre-epoch dates, ... 00041 static inline boost::uint32_t to_time_t(const PionDateTime& t) 00042 { 00043 static const boost::posix_time::ptime start(boost::gregorian::date(1970,1,1)); 00044 return (t-start).total_seconds(); 00045 } 00046 00047 00049 PionTimeFacet(void) {} 00050 00052 virtual ~PionTimeFacet(void) {} 00053 00059 explicit PionTimeFacet(const std::string& format) { 00060 setFormat(format); 00061 } 00062 00064 PionTimeFacet(const PionTimeFacet& f) { 00065 setFormat(f.getFormat()); 00066 } 00067 00069 PionTimeFacet& operator=(const PionTimeFacet& f) { 00070 setFormat(f.getFormat()); 00071 return *this; 00072 } 00073 00080 template <class charT, class traits> 00081 inline void read(std::basic_istream<charT,traits>& input, PionDateTime& t) { 00082 input.imbue(std::locale(input.getloc(), new boost::posix_time::time_input_facet(m_format.c_str()))); 00083 input >> t; 00084 } 00085 00092 template <class charT, class traits> 00093 inline void write(std::basic_ostream<charT,traits>& output, const PionDateTime& t) { 00094 output.imbue(std::locale(output.getloc(), new boost::posix_time::time_facet(m_format.c_str()))); 00095 output << t; 00096 } 00097 00104 inline void fromString(const std::string& str, PionDateTime& t) { 00105 m_input_stream.str(str); 00106 m_input_stream >> t; 00107 } 00108 00115 inline void fromString(const char *str, PionDateTime& t) { 00116 m_input_stream.str(str); 00117 m_input_stream >> t; 00118 } 00119 00126 inline PionDateTime fromString(const std::string& str) { 00127 PionDateTime t; 00128 m_input_stream.str(str); 00129 m_input_stream >> t; 00130 return t; 00131 } 00132 00139 inline PionDateTime fromString(const char *str) { 00140 PionDateTime t; 00141 m_input_stream.str(str); 00142 m_input_stream >> t; 00143 return t; 00144 } 00145 00152 inline void toString(std::string& str, const PionDateTime& t) { 00153 m_output_stream.str(""); 00154 m_output_stream << t; 00155 str = m_output_stream.str(); 00156 } 00157 00164 inline std::string toString(const PionDateTime& t) { 00165 m_output_stream.str(""); 00166 m_output_stream << t; 00167 return m_output_stream.str(); 00168 } 00169 00171 inline void setFormat(const std::string& format) { 00172 m_format = format; 00173 m_input_stream.imbue(std::locale(m_input_stream.getloc(), new boost::posix_time::time_input_facet(m_format.c_str()))); 00174 m_output_stream.imbue(std::locale(m_output_stream.getloc(), new boost::posix_time::time_facet(m_format.c_str()))); 00175 } 00176 00178 inline const std::string& getFormat(void) const { return m_format; } 00179 00180 00181 private: 00182 00184 std::string m_format; 00185 00187 std::stringstream m_input_stream; 00188 00190 std::stringstream m_output_stream; 00191 }; 00192 00193 00194 } // end namespace pion 00195 00196 #endif 00197