pion-net  4.0.9
PionDateTime.hpp
1 // -----------------------------------------------------------------------
2 // pion-common: a collection of common libraries used by the Pion Platform
3 // -----------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_PIONDATETIME_HEADER__
11 #define __PION_PIONDATETIME_HEADER__
12 
13 #include <string>
14 #include <istream>
15 #include <ostream>
16 #include <sstream>
17 #include <boost/date_time/posix_time/posix_time.hpp>
18 #include <boost/date_time/gregorian/greg_date.hpp>
19 #include <pion/PionConfig.hpp>
20 
21 
22 namespace pion { // begin namespace pion
23 
24 
26 typedef boost::posix_time::ptime PionDateTime;
27 
28 
29 
30 
35 {
36 public:
37 
38  // Function that converts a ptime into a time_t
39  // Note: this is quick & dirty -- does not handle invalid dates,
40  // other calendars, pre-epoch dates, ...
41  static inline boost::uint32_t to_time_t(const PionDateTime& t)
42  {
43  static const boost::posix_time::ptime start(boost::gregorian::date(1970,1,1));
44  return (t-start).total_seconds();
45  }
46 
47 
49  PionTimeFacet(void) {}
50 
52  virtual ~PionTimeFacet(void) {}
53 
59  explicit PionTimeFacet(const std::string& format) {
60  setFormat(format);
61  }
62 
65  setFormat(f.getFormat());
66  }
67 
70  setFormat(f.getFormat());
71  return *this;
72  }
73 
80  template <class charT, class traits>
81  inline void read(std::basic_istream<charT,traits>& input, PionDateTime& t) {
82  input.imbue(std::locale(input.getloc(), new boost::posix_time::time_input_facet(m_format.c_str())));
83  input >> t;
84  }
85 
92  template <class charT, class traits>
93  inline void write(std::basic_ostream<charT,traits>& output, const PionDateTime& t) {
94  output.imbue(std::locale(output.getloc(), new boost::posix_time::time_facet(m_format.c_str())));
95  output << t;
96  }
97 
104  inline void fromString(const std::string& str, PionDateTime& t) {
105  m_input_stream.str(str);
106  m_input_stream >> t;
107  }
108 
115  inline void fromString(const char *str, PionDateTime& t) {
116  m_input_stream.str(str);
117  m_input_stream >> t;
118  }
119 
126  inline PionDateTime fromString(const std::string& str) {
127  PionDateTime t;
128  m_input_stream.str(str);
129  m_input_stream >> t;
130  return t;
131  }
132 
139  inline PionDateTime fromString(const char *str) {
140  PionDateTime t;
141  m_input_stream.str(str);
142  m_input_stream >> t;
143  return t;
144  }
145 
152  inline void toString(std::string& str, const PionDateTime& t) {
153  m_output_stream.str("");
154  m_output_stream << t;
155  str = m_output_stream.str();
156  }
157 
164  inline std::string toString(const PionDateTime& t) {
165  m_output_stream.str("");
166  m_output_stream << t;
167  return m_output_stream.str();
168  }
169 
171  inline void setFormat(const std::string& format) {
172  m_format = format;
173  m_input_stream.imbue(std::locale(m_input_stream.getloc(), new boost::posix_time::time_input_facet(m_format.c_str())));
174  m_output_stream.imbue(std::locale(m_output_stream.getloc(), new boost::posix_time::time_facet(m_format.c_str())));
175  }
176 
178  inline const std::string& getFormat(void) const { return m_format; }
179 
180 
181 private:
182 
184  std::string m_format;
185 
187  std::stringstream m_input_stream;
188 
190  std::stringstream m_output_stream;
191 };
192 
193 
194 } // end namespace pion
195 
196 #endif
197 
void fromString(const std::string &str, PionDateTime &t)
void read(std::basic_istream< charT, traits > &input, PionDateTime &t)
std::string toString(const PionDateTime &t)
void fromString(const char *str, PionDateTime &t)
PionDateTime fromString(const char *str)
PionDateTime fromString(const std::string &str)
PionTimeFacet(void)
default constructor
void toString(std::string &str, const PionDateTime &t)
virtual ~PionTimeFacet(void)
virtual destructor
the following enables use of the lock-free cache
PionTimeFacet & operator=(const PionTimeFacet &f)
assignment operator
PionTimeFacet(const PionTimeFacet &f)
copy constructor
void setFormat(const std::string &format)
sets the format used for I/O (see boost::date_time docs)
void write(std::basic_ostream< charT, traits > &output, const PionDateTime &t)
const std::string & getFormat(void) const
returns the format used for I/O
boost::posix_time::ptime PionDateTime
PionDateTime is a typedef for boost::posix_time::ptime.
PionTimeFacet(const std::string &format)