Skip to content

Commit fa1658b

Browse files
committed
integrated improvements from 1.3.4 (NumberFormatter::append(), DateTimeFormatter::append()
1 parent 60e1433 commit fa1658b

23 files changed

Lines changed: 1173 additions & 598 deletions

Foundation/include/Poco/ArchiveStrategy.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//
22
// ArchiveStrategy.h
33
//
4-
// $Id: //poco/svn/Foundation/include/Poco/ArchiveStrategy.h#2 $
4+
// $Id: //poco/Main/Foundation/include/Poco/ArchiveStrategy.h#5 $
55
//
66
// Library: Foundation
77
// Package: Logging
88
// Module: FileChannel
99
//
1010
// Definition of the ArchiveStrategy class and subclasses.
1111
//
12-
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
12+
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
1313
// and Contributors.
1414
//
1515
// Permission is hereby granted, free of charge, to any person or organization
@@ -120,7 +120,7 @@ class ArchiveByTimestampStrategy: public ArchiveStrategy
120120
delete pFile;
121121
std::string archPath = path;
122122
archPath.append(".");
123-
archPath.append(DateTimeFormatter::format(DT().timestamp(), "%Y%m%d%H%M%S%i"));
123+
DateTimeFormatter::append(archPath, DT().timestamp(), "%Y%m%d%H%M%S%i");
124124

125125
if (exists(archPath)) archiveByNumber(archPath);
126126
else moveFile(path, archPath);
@@ -129,7 +129,6 @@ class ArchiveByTimestampStrategy: public ArchiveStrategy
129129
}
130130

131131
private:
132-
133132
void archiveByNumber(const std::string& basePath)
134133
/// A monotonic increasing number is appended to the
135134
/// log file name. The most recent archived file
@@ -141,7 +140,7 @@ class ArchiveByTimestampStrategy: public ArchiveStrategy
141140
{
142141
path = basePath;
143142
path.append(".");
144-
path.append(NumberFormatter::format(++n));
143+
NumberFormatter::append(path, ++n);
145144
}
146145
while (exists(path));
147146

@@ -151,16 +150,15 @@ class ArchiveByTimestampStrategy: public ArchiveStrategy
151150
if (n > 0)
152151
{
153152
oldPath.append(".");
154-
oldPath.append(NumberFormatter::format(n - 1));
153+
NumberFormatter::append(oldPath, n - 1);
155154
}
156155
std::string newPath = basePath;
157156
newPath.append(".");
158-
newPath.append(NumberFormatter::format(n));
157+
NumberFormatter::append(newPath, n);
159158
moveFile(oldPath, newPath);
160159
--n;
161160
}
162161
}
163-
164162
};
165163

166164

Foundation/include/Poco/DateTimeFormatter.h

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
@@ -41,20 +41,26 @@
4141

4242

4343
#include "Poco/Foundation.h"
44+
#include "Poco/DateTime.h"
45+
#include "Poco/LocalDateTime.h"
4446

4547

4648
namespace Poco {
4749

4850

49-
class DateTime;
50-
class LocalDateTime;
5151
class Timestamp;
5252
class Timespan;
5353

5454

5555
class 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
{
5965
public:
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

Comments
 (0)