11//
22// DateTimeFormatter.h
33//
4- // $Id: //poco/svn /Foundation/include/Poco/DateTimeFormatter.h#2 $
4+ // $Id: //poco/Main /Foundation/include/Poco/DateTimeFormatter.h#3 $
55//
66// Library: Foundation
77// Package: DateTime
4141
4242
4343#include " Poco/Foundation.h"
44+ #include " Poco/DateTime.h"
45+ #include " Poco/LocalDateTime.h"
4446
4547
4648namespace Poco {
4749
4850
49- class DateTime ;
50- class LocalDateTime ;
5151class Timestamp ;
5252class Timespan ;
5353
5454
5555class Foundation_API DateTimeFormatter
5656 // / This class converts dates and times into strings, supporting a
5757 // / variety of standard and custom formats.
58+ // /
59+ // / There are two kind of static member functions:
60+ // / * format* functions return a std::string containing
61+ // / the formatted value.
62+ // / * append* functions append the formatted value to
63+ // / an existing string.
5864{
5965public:
6066 enum
@@ -119,6 +125,26 @@ class Foundation_API DateTimeFormatter
119125 // / * %c - centisecond (0 .. 9)
120126 // / * %% - percent sign
121127
128+ static void append (std::string& str, const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential = UTC);
129+ // / Formats the given timestamp according to the given format and appends it to str.
130+ // /
131+ // / See format() for documentation of the formatting string.
132+
133+ static void append (std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential = UTC);
134+ // / Formats the given date and time according to the given format and appends it to str.
135+ // /
136+ // / See format() for documentation of the formatting string.
137+
138+ static void append (std::string& str, const LocalDateTime& dateTime, const std::string& fmt);
139+ // / Formats the given local date and time according to the given format and appends it to str.
140+ // /
141+ // / See format() for documentation of the formatting string.
142+
143+ static void append (std::string& str, const Timespan& timespan, const std::string& fmt = " %dd %H:%M:%S.%i" );
144+ // / Formats the given timespan according to the given format and appends it to str.
145+ // /
146+ // / See format() for documentation of the formatting string.
147+
122148 static std::string tzdISO (int timeZoneDifferential);
123149 // / Formats the given timezone differential in ISO format.
124150 // / If timeZoneDifferential is UTC, "Z" is returned,
@@ -128,9 +154,80 @@ class Foundation_API DateTimeFormatter
128154 // / Formats the given timezone differential in RFC format.
129155 // / If timeZoneDifferential is UTC, "GMT" is returned,
130156 // / otherwise ++HHMM (or -HHMM) is returned.
157+
158+ static void tzdISO (std::string& str, int timeZoneDifferential);
159+ // / Formats the given timezone differential in ISO format
160+ // / and appends it to the given string.
161+ // / If timeZoneDifferential is UTC, "Z" is returned,
162+ // / otherwise, +HH.MM (or -HH.MM) is returned.
163+
164+ static void tzdRFC (std::string& str, int timeZoneDifferential);
165+ // / Formats the given timezone differential in RFC format
166+ // / and appends it to the given string.
167+ // / If timeZoneDifferential is UTC, "GMT" is returned,
168+ // / otherwise ++HHMM (or -HHMM) is returned.
131169};
132170
133171
172+ //
173+ // inlines
174+ //
175+ inline std::string DateTimeFormatter::format (const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
176+ {
177+ DateTime dateTime (timestamp);
178+ return format (dateTime, fmt, timeZoneDifferential);
179+ }
180+
181+
182+ inline std::string DateTimeFormatter::format (const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
183+ {
184+ std::string result;
185+ result.reserve (64 );
186+ append (result, dateTime, fmt, timeZoneDifferential);
187+ return result;
188+ }
189+
190+
191+ inline std::string DateTimeFormatter::format (const LocalDateTime& dateTime, const std::string& fmt)
192+ {
193+ return format (dateTime._dateTime , fmt, dateTime._tzd );
194+ }
195+
196+
197+ inline std::string DateTimeFormatter::format (const Timespan& timespan, const std::string& fmt)
198+ {
199+ std::string result;
200+ result.reserve (32 );
201+ append (result, timespan, fmt);
202+ return result;
203+ }
204+
205+
206+ inline void DateTimeFormatter::append (std::string& str, const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
207+ {
208+ DateTime dateTime (timestamp);
209+ append (str, dateTime, fmt, timeZoneDifferential);
210+ }
211+
212+
213+ inline std::string DateTimeFormatter::tzdISO (int timeZoneDifferential)
214+ {
215+ std::string result;
216+ result.reserve (8 );
217+ tzdISO (result, timeZoneDifferential);
218+ return result;
219+ }
220+
221+
222+ inline std::string DateTimeFormatter::tzdRFC (int timeZoneDifferential)
223+ {
224+ std::string result;
225+ result.reserve (8 );
226+ tzdRFC (result, timeZoneDifferential);
227+ return result;
228+ }
229+
230+
134231} // namespace Poco
135232
136233
0 commit comments