DateTime.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__DATE_TIME
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__DATE_TIME
19 
20 #include <Balau/Type/ToString.hpp>
21 #include <Balau/System/Clock.hpp>
22 #include <Balau/ThirdParty/Date/date.hpp>
23 
24 #include <boost/date_time/posix_time/posix_time.hpp>
25 #include <boost/date_time/posix_time/posix_time_io.hpp>
26 
27 namespace Balau::Util {
28 
32 struct DateTime final {
41  template <typename AllocatorT, typename Clock, typename Dur>
42  static U8String<AllocatorT> toString(const char * format, const std::chrono::time_point<Clock, Dur> & tp) {
44  Date::to_stream(str, format, tp);
45  return str.str();
46  }
47 
56  template <typename Clock, typename Dur>
57  static std::string toString(const char * format, const std::chrono::time_point<Clock, Dur> & tp) {
58  std::ostringstream str;
59  Date::to_stream(str, format, tp);
60  return str.str();
61  }
62 
71  template <typename Clock, typename Dur>
72  static void toString(std::ostream & stream, const char * format, const std::chrono::time_point<Clock, Dur> & tp) {
73  Date::to_stream(stream, format, tp);
74  }
75 
84  template <typename AllocatorT, typename Rep, typename Period>
85  static U8String<AllocatorT> toString(const char * format, const std::chrono::duration<Rep, Period> & d) {
87 
88  // TODO??
89  // The HH date library appears to do an abs(d), so
90  Date::to_stream(str, format, d);
91  return str.str();
92  }
93 
102  template <typename Rep, typename Period>
103  static std::string toString(const char * format, const std::chrono::duration<Rep, Period> & d) {
104  std::ostringstream str;
105 
106  // TODO??
107  // The HH date library appears to do an abs(d), so
108  Date::to_stream(str, format, d);
109  return str.str();
110  }
111 
120  template <typename Rep, typename Period>
121  static void toString(std::ostream & stream, const char * format, const std::chrono::duration<Rep, Period> & d) {
122  Date::to_stream(stream, format, d);
123  }
124 
133  template <typename Rep, typename Period>
134  static std::chrono::duration<Rep, Period> toDuration(const char * format, const std::string & s) {
135  std::istringstream str(s);
136  std::chrono::duration<Rep, Period> ret {};
137  Date::from_stream(str, format, ret);
138  return ret;
139  }
140 
142 
143  DateTime() = delete;
144  DateTime(const DateTime & copy) = delete;
145  DateTime & operator = (const DateTime & copy) = delete;
146 };
147 
148 } // namespace Balau::Util
149 
151 
155 template<typename Rep, typename Period>
156 inline std::string toString(const std::chrono::duration<Rep, Period> & d) {
157  using namespace Balau::Date;
158  std::ostringstream s;
159  s << d;
160  return s.str();
161 }
162 
166 template<typename Clock, typename Dur>
167 inline std::string toString(const std::chrono::time_point<Clock, Dur> & tp) {
168  using namespace Balau::Date;
169  std::ostringstream s;
170  s << tp;
171  return s.str();
172 }
173 
174 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__DATE_TIME
static std::chrono::duration< Rep, Period > toDuration(const char *format, const std::string &s)
Create a duration from the supplied string which is in the specified format.
Definition: DateTime.hpp:134
Pre-defined universal to-string functions.
Utility functions.
static void toString(std::ostream &stream, const char *format, const std::chrono::duration< Rep, Period > &d)
Format the duration as a string with the specified format into the supplied output stream...
Definition: DateTime.hpp:121
static U8String< AllocatorT > toString(const char *format, const std::chrono::duration< Rep, Period > &d)
Format the duration as a string with the specified format.
Definition: DateTime.hpp:85
std::basic_ostringstream< char, std::char_traits< char >, AllocatorT > U8OStringStream
UTF-8 output string stream type with selectable allocator.
Definition: ToStringA.hpp:59
static std::string toString(const char *format, const std::chrono::duration< Rep, Period > &d)
Format the duration as a string with the specified format.
Definition: DateTime.hpp:103
static std::string toString(const char *format, const std::chrono::time_point< Clock, Dur > &tp)
Format the time point as a string with the specified format.
Definition: DateTime.hpp:57
std::basic_string< char, std::char_traits< char >, AllocatorT > U8String
UTF-8 string type with selectable allocator.
Definition: ToStringA.hpp:41
static U8String< AllocatorT > toString(const char *format, const std::chrono::time_point< Clock, Dur > &tp)
Format the time point as a string with the specified format.
Definition: DateTime.hpp:42
static void toString(std::ostream &stream, const char *format, const std::chrono::time_point< Clock, Dur > &tp)
Format the time point as a string with the specified format into the supplied output stream...
Definition: DateTime.hpp:72
Date and time utilities simplified.
Definition: DateTime.hpp:32
Base interface of clocks.