integrated improvements from 1.3.4 (NumberFormatter::append(), DateTimeFormatter::append()

This commit is contained in:
Guenter Obiltschnig
2009-02-21 10:59:58 +00:00
parent 60e1433f51
commit fa1658b23a
23 changed files with 1173 additions and 598 deletions

View File

@@ -1,7 +1,7 @@
//
// ArchiveStrategy.h
//
// $Id: //poco/svn/Foundation/include/Poco/ArchiveStrategy.h#2 $
// $Id: //poco/Main/Foundation/include/Poco/ArchiveStrategy.h#5 $
//
// Library: Foundation
// Package: Logging
@@ -9,7 +9,7 @@
//
// Definition of the ArchiveStrategy class and subclasses.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@@ -120,7 +120,7 @@ public:
delete pFile;
std::string archPath = path;
archPath.append(".");
archPath.append(DateTimeFormatter::format(DT().timestamp(), "%Y%m%d%H%M%S%i"));
DateTimeFormatter::append(archPath, DT().timestamp(), "%Y%m%d%H%M%S%i");
if (exists(archPath)) archiveByNumber(archPath);
else moveFile(path, archPath);
@@ -129,7 +129,6 @@ public:
}
private:
void archiveByNumber(const std::string& basePath)
/// A monotonic increasing number is appended to the
/// log file name. The most recent archived file
@@ -141,7 +140,7 @@ private:
{
path = basePath;
path.append(".");
path.append(NumberFormatter::format(++n));
NumberFormatter::append(path, ++n);
}
while (exists(path));
@@ -151,16 +150,15 @@ private:
if (n > 0)
{
oldPath.append(".");
oldPath.append(NumberFormatter::format(n - 1));
NumberFormatter::append(oldPath, n - 1);
}
std::string newPath = basePath;
newPath.append(".");
newPath.append(NumberFormatter::format(n));
NumberFormatter::append(newPath, n);
moveFile(oldPath, newPath);
--n;
}
}
};

View File

@@ -1,7 +1,7 @@
//
// DateTimeFormatter.h
//
// $Id: //poco/svn/Foundation/include/Poco/DateTimeFormatter.h#2 $
// $Id: //poco/Main/Foundation/include/Poco/DateTimeFormatter.h#3 $
//
// Library: Foundation
// Package: DateTime
@@ -41,13 +41,13 @@
#include "Poco/Foundation.h"
#include "Poco/DateTime.h"
#include "Poco/LocalDateTime.h"
namespace Poco {
class DateTime;
class LocalDateTime;
class Timestamp;
class Timespan;
@@ -55,6 +55,12 @@ class Timespan;
class Foundation_API DateTimeFormatter
/// This class converts dates and times into strings, supporting a
/// variety of standard and custom formats.
///
/// There are two kind of static member functions:
/// * format* functions return a std::string containing
/// the formatted value.
/// * append* functions append the formatted value to
/// an existing string.
{
public:
enum
@@ -119,6 +125,26 @@ public:
/// * %c - centisecond (0 .. 9)
/// * %% - percent sign
static void append(std::string& str, const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential = UTC);
/// Formats the given timestamp according to the given format and appends it to str.
///
/// See format() for documentation of the formatting string.
static void append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential = UTC);
/// Formats the given date and time according to the given format and appends it to str.
///
/// See format() for documentation of the formatting string.
static void append(std::string& str, const LocalDateTime& dateTime, const std::string& fmt);
/// Formats the given local date and time according to the given format and appends it to str.
///
/// See format() for documentation of the formatting string.
static void append(std::string& str, const Timespan& timespan, const std::string& fmt = "%dd %H:%M:%S.%i");
/// Formats the given timespan according to the given format and appends it to str.
///
/// See format() for documentation of the formatting string.
static std::string tzdISO(int timeZoneDifferential);
/// Formats the given timezone differential in ISO format.
/// If timeZoneDifferential is UTC, "Z" is returned,
@@ -128,9 +154,80 @@ public:
/// Formats the given timezone differential in RFC format.
/// If timeZoneDifferential is UTC, "GMT" is returned,
/// otherwise ++HHMM (or -HHMM) is returned.
static void tzdISO(std::string& str, int timeZoneDifferential);
/// Formats the given timezone differential in ISO format
/// and appends it to the given string.
/// If timeZoneDifferential is UTC, "Z" is returned,
/// otherwise, +HH.MM (or -HH.MM) is returned.
static void tzdRFC(std::string& str, int timeZoneDifferential);
/// Formats the given timezone differential in RFC format
/// and appends it to the given string.
/// If timeZoneDifferential is UTC, "GMT" is returned,
/// otherwise ++HHMM (or -HHMM) is returned.
};
//
// inlines
//
inline std::string DateTimeFormatter::format(const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
{
DateTime dateTime(timestamp);
return format(dateTime, fmt, timeZoneDifferential);
}
inline std::string DateTimeFormatter::format(const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
{
std::string result;
result.reserve(64);
append(result, dateTime, fmt, timeZoneDifferential);
return result;
}
inline std::string DateTimeFormatter::format(const LocalDateTime& dateTime, const std::string& fmt)
{
return format(dateTime._dateTime, fmt, dateTime._tzd);
}
inline std::string DateTimeFormatter::format(const Timespan& timespan, const std::string& fmt)
{
std::string result;
result.reserve(32);
append(result, timespan, fmt);
return result;
}
inline void DateTimeFormatter::append(std::string& str, const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
{
DateTime dateTime(timestamp);
append(str, dateTime, fmt, timeZoneDifferential);
}
inline std::string DateTimeFormatter::tzdISO(int timeZoneDifferential)
{
std::string result;
result.reserve(8);
tzdISO(result, timeZoneDifferential);
return result;
}
inline std::string DateTimeFormatter::tzdRFC(int timeZoneDifferential)
{
std::string result;
result.reserve(8);
tzdRFC(result, timeZoneDifferential);
return result;
}
} // namespace Poco

View File

@@ -1,7 +1,7 @@
//
// NumberFormatter.h
//
// $Id: //poco/svn/Foundation/include/Poco/NumberFormatter.h#2 $
// $Id: //poco/Main/Foundation/include/Poco/NumberFormatter.h#4 $
//
// Library: Foundation
// Package: Core
@@ -9,7 +9,7 @@
//
// Definition of the NumberFormatter class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@@ -49,6 +49,15 @@ namespace Poco {
class Foundation_API NumberFormatter
/// The NumberFormatter class provides static methods
/// for formatting numeric values into strings.
///
/// There are two kind of static member functions:
/// * format* functions return a std::string containing
/// the formatted value.
/// * append* functions append the formatted value to
/// an existing string.
///
/// Internally, std::sprintf() is used to do the actual
/// formatting.
{
public:
static std::string format(int value);
@@ -206,9 +215,454 @@ public:
/// Formats a pointer in an eight (32-bit architectures) or
/// sixteen (64-bit architectures) characters wide
/// field in hexadecimal notation.
static void append(std::string& str, int value);
/// Formats an integer value in decimal notation.
static void append(std::string& str, int value, int width);
/// Formats an integer value in decimal notation,
/// right justified in a field having at least
/// the specified width.
static void append0(std::string& str, int value, int width);
/// Formats an integer value in decimal notation,
/// right justified and zero-padded in a field
/// having at least the specified width.
static void appendHex(std::string& str, int value);
/// Formats an int value in hexadecimal notation.
/// The value is treated as unsigned.
static void appendHex(std::string& str, int value, int width);
/// Formats a int value in hexadecimal notation,
/// right justified and zero-padded in
/// a field having at least the specified width.
/// The value is treated as unsigned.
static void append(std::string& str, unsigned value);
/// Formats an unsigned int value in decimal notation.
static void append(std::string& str, unsigned value, int width);
/// Formats an unsigned long int in decimal notation,
/// right justified in a field having at least the
/// specified width.
static void append0(std::string& str, unsigned int value, int width);
/// Formats an unsigned int value in decimal notation,
/// right justified and zero-padded in a field having at
/// least the specified width.
static void appendHex(std::string& str, unsigned value);
/// Formats an unsigned int value in hexadecimal notation.
static void appendHex(std::string& str, unsigned value, int width);
/// Formats a int value in hexadecimal notation,
/// right justified and zero-padded in
/// a field having at least the specified width.
static void append(std::string& str, long value);
/// Formats a long value in decimal notation.
static void append(std::string& str, long value, int width);
/// Formats a long value in decimal notation,
/// right justified in a field having at least the
/// specified width.
static void append0(std::string& str, long value, int width);
/// Formats a long value in decimal notation,
/// right justified and zero-padded in a field
/// having at least the specified width.
static void appendHex(std::string& str, long value);
/// Formats an unsigned long value in hexadecimal notation.
/// The value is treated as unsigned.
static void appendHex(std::string& str, long value, int width);
/// Formats an unsigned long value in hexadecimal notation,
/// right justified and zero-padded in a field having at least the
/// specified width.
/// The value is treated as unsigned.
static void append(std::string& str, unsigned long value);
/// Formats an unsigned long value in decimal notation.
static void append(std::string& str, unsigned long value, int width);
/// Formats an unsigned long value in decimal notation,
/// right justified in a field having at least the specified
/// width.
static void append0(std::string& str, unsigned long value, int width);
/// Formats an unsigned long value in decimal notation,
/// right justified and zero-padded
/// in a field having at least the specified width.
static void appendHex(std::string& str, unsigned long value);
/// Formats an unsigned long value in hexadecimal notation.
static void appendHex(std::string& str, unsigned long value, int width);
/// Formats an unsigned long value in hexadecimal notation,
/// right justified and zero-padded in a field having at least the
/// specified width.
#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
static void append(std::string& str, Int64 value);
/// Formats a 64-bit integer value in decimal notation.
static void append(std::string& str, Int64 value, int width);
/// Formats a 64-bit integer value in decimal notation,
/// right justified in a field having at least the specified width.
static void append0(std::string& str, Int64 value, int width);
/// Formats a 64-bit integer value in decimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width.
static void appendHex(std::string& str, Int64 value);
/// Formats a 64-bit integer value in hexadecimal notation.
/// The value is treated as unsigned.
static void appendHex(std::string& str, Int64 value, int width);
/// Formats a 64-bit integer value in hexadecimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width.
/// The value is treated as unsigned.
static void append(std::string& str, UInt64 value);
/// Formats an unsigned 64-bit integer value in decimal notation.
static void append(std::string& str, UInt64 value, int width);
/// Formats an unsigned 64-bit integer value in decimal notation,
/// right justified in a field having at least the specified width.
static void append0(std::string& str, UInt64 value, int width);
/// Formats an unsigned 64-bit integer value in decimal notation,
/// right justified and zero-padded in a field having at least the
/// specified width.
static void appendHex(std::string& str, UInt64 value);
/// Formats a 64-bit integer value in hexadecimal notation.
static void appendHex(std::string& str, UInt64 value, int width);
/// Formats a 64-bit integer value in hexadecimal notation,
/// right justified and zero-padded in a field having at least
/// the specified width.
#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
static void append(std::string& str, float value);
/// Formats a float value in decimal floating-point notation,
/// according to std::printf's %g format with a precision of 8 fractional digits.
static void append(std::string& str, double value);
/// Formats a double value in decimal floating-point notation,
/// according to std::printf's %g format with a precision of 16 fractional digits.
static void append(std::string& str, double value, int precision);
/// Formats a double value in decimal floating-point notation,
/// according to std::printf's %f format with the given precision.
static void append(std::string& str, double value, int width, int precision);
/// Formats a double value in decimal floating-point notation,
/// right justified in a field of the specified width,
/// with the number of fractional digits given in precision.
static void append(std::string& str, const void* ptr);
/// Formats a pointer in an eight (32-bit architectures) or
/// sixteen (64-bit architectures) characters wide
/// field in hexadecimal notation.
};
//
// inlines
//
inline std::string NumberFormatter::format(int value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(int value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(int value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(int value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(int value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
inline std::string NumberFormatter::format(unsigned value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(unsigned value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(unsigned int value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(unsigned value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(unsigned value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
inline std::string NumberFormatter::format(long value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(long value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(long value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(long value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(long value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
inline std::string NumberFormatter::format(unsigned long value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(unsigned long value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(unsigned long value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(unsigned long value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(unsigned long value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
inline std::string NumberFormatter::format(Int64 value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(Int64 value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(Int64 value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(Int64 value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(Int64 value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
inline std::string NumberFormatter::format(UInt64 value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(UInt64 value, int width)
{
std::string result;
append(result, value, width);
return result;
}
inline std::string NumberFormatter::format0(UInt64 value, int width)
{
std::string result;
append0(result, value, width);
return result;
}
inline std::string NumberFormatter::formatHex(UInt64 value)
{
std::string result;
appendHex(result, value);
return result;
}
inline std::string NumberFormatter::formatHex(UInt64 value, int width)
{
std::string result;
appendHex(result, value, width);
return result;
}
#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
inline std::string NumberFormatter::format(float value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(double value)
{
std::string result;
append(result, value);
return result;
}
inline std::string NumberFormatter::format(double value, int precision)
{
std::string result;
append(result, value, precision);
return result;
}
inline std::string NumberFormatter::format(double value, int width, int precision)
{
std::string result;
append(result, value, width, precision);
return result;
}
inline std::string NumberFormatter::format(const void* ptr)
{
std::string result;
append(result, ptr);
return result;
}
} // namespace Poco

View File

@@ -1,7 +1,7 @@
//
// ArchiveStrategy.cpp
//
// $Id: //poco/svn/Foundation/src/ArchiveStrategy.cpp#2 $
// $Id: //poco/Main/Foundation/src/ArchiveStrategy.cpp#9 $
//
// Library: Foundation
// Package: Logging
@@ -186,7 +186,7 @@ LogFile* ArchiveByNumberStrategy::archive(LogFile* pFile)
{
path = basePath;
path.append(".");
path.append(NumberFormatter::format(++n));
NumberFormatter::append(path, ++n);
}
while (exists(path));
@@ -196,11 +196,11 @@ LogFile* ArchiveByNumberStrategy::archive(LogFile* pFile)
if (n > 0)
{
oldPath.append(".");
oldPath.append(NumberFormatter::format(n - 1));
NumberFormatter::append(oldPath, n - 1);
}
std::string newPath = basePath;
newPath.append(".");
newPath.append(NumberFormatter::format(n));
NumberFormatter::append(newPath, n);
moveFile(oldPath, newPath);
--n;
}

View File

@@ -1,7 +1,7 @@
//
// DateTimeFormatter.cpp
//
// $Id: //poco/svn/Foundation/src/DateTimeFormatter.cpp#2 $
// $Id: //poco/Main/Foundation/src/DateTimeFormatter.cpp#13 $
//
// Library: Foundation
// Package: DateTime
@@ -36,8 +36,6 @@
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/DateTime.h"
#include "Poco/LocalDateTime.h"
#include "Poco/Timestamp.h"
#include "Poco/NumberFormatter.h"
@@ -45,17 +43,8 @@
namespace Poco {
std::string DateTimeFormatter::format(const Timestamp& timestamp, const std::string& fmt, int timeZoneDifferential)
void DateTimeFormatter::append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
{
DateTime dateTime(timestamp);
return format(dateTime, fmt, timeZoneDifferential);
}
std::string DateTimeFormatter::format(const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
{
std::string result;
result.reserve(64);
std::string::const_iterator it = fmt.begin();
std::string::const_iterator end = fmt.end();
while (it != end)
@@ -66,49 +55,40 @@ std::string DateTimeFormatter::format(const DateTime& dateTime, const std::strin
{
switch (*it)
{
case 'w': result.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
case 'W': result.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
case 'b': result.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break;
case 'B': result.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
case 'd': result.append(NumberFormatter::format0(dateTime.day(), 2)); break;
case 'e': result.append(NumberFormatter::format(dateTime.day())); break;
case 'f': result.append(NumberFormatter::format(dateTime.day(), 2)); break;
case 'm': result.append(NumberFormatter::format0(dateTime.month(), 2)); break;
case 'n': result.append(NumberFormatter::format(dateTime.month())); break;
case 'o': result.append(NumberFormatter::format(dateTime.month(), 2)); break;
case 'y': result.append(NumberFormatter::format0(dateTime.year() % 100, 2)); break;
case 'Y': result.append(NumberFormatter::format0(dateTime.year(), 4)); break;
case 'H': result.append(NumberFormatter::format0(dateTime.hour(), 2)); break;
case 'h': result.append(NumberFormatter::format0(dateTime.hourAMPM(), 2)); break;
case 'a': result.append(dateTime.isAM() ? "am" : "pm"); break;
case 'A': result.append(dateTime.isAM() ? "AM" : "PM"); break;
case 'M': result.append(NumberFormatter::format0(dateTime.minute(), 2)); break;
case 'S': result.append(NumberFormatter::format0(dateTime.second(), 2)); break;
case 'i': result.append(NumberFormatter::format0(dateTime.millisecond(), 3)); break;
case 'c': result.append(NumberFormatter::format(dateTime.millisecond()/100)); break;
case 'z': result.append(tzdISO(timeZoneDifferential)); break;
case 'Z': result.append(tzdRFC(timeZoneDifferential)); break;
default: result += *it;
case 'w': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
case 'W': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
case 'b': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break;
case 'B': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
case 'd': NumberFormatter::append0(str, dateTime.day(), 2); break;
case 'e': NumberFormatter::append(str, dateTime.day()); break;
case 'f': NumberFormatter::append(str, dateTime.day(), 2); break;
case 'm': NumberFormatter::append0(str, dateTime.month(), 2); break;
case 'n': NumberFormatter::append(str, dateTime.month()); break;
case 'o': NumberFormatter::append(str, dateTime.month(), 2); break;
case 'y': NumberFormatter::append0(str, dateTime.year() % 100, 2); break;
case 'Y': NumberFormatter::append0(str, dateTime.year(), 4); break;
case 'H': NumberFormatter::append0(str, dateTime.hour(), 2); break;
case 'h': NumberFormatter::append0(str, dateTime.hourAMPM(), 2); break;
case 'a': str.append(dateTime.isAM() ? "am" : "pm"); break;
case 'A': str.append(dateTime.isAM() ? "AM" : "PM"); break;
case 'M': NumberFormatter::append0(str, dateTime.minute(), 2); break;
case 'S': NumberFormatter::append0(str, dateTime.second(), 2); break;
case 'i': NumberFormatter::append0(str, dateTime.millisecond(), 3); break;
case 'c': NumberFormatter::append(str, dateTime.millisecond()/100); break;
case 'z': tzdISO(str, timeZoneDifferential); break;
case 'Z': tzdRFC(str, timeZoneDifferential); break;
default: str += *it;
}
++it;
}
}
else result += *it++;
else str += *it++;
}
return result;
}
std::string DateTimeFormatter::format(const LocalDateTime& dateTime, const std::string& fmt)
void DateTimeFormatter::append(std::string& str, const Timespan& timespan, const std::string& fmt)
{
return format(dateTime._dateTime, fmt, dateTime._tzd);
}
std::string DateTimeFormatter::format(const Timespan& timespan, const std::string& fmt)
{
std::string result;
result.reserve(32);
std::string::const_iterator it = fmt.begin();
std::string::const_iterator end = fmt.end();
while (it != end)
@@ -119,73 +99,66 @@ std::string DateTimeFormatter::format(const Timespan& timespan, const std::strin
{
switch (*it)
{
case 'd': result.append(NumberFormatter::format(timespan.days())); break;
case 'H': result.append(NumberFormatter::format0(timespan.hours(), 2)); break;
case 'h': result.append(NumberFormatter::format(timespan.totalHours())); break;
case 'M': result.append(NumberFormatter::format0(timespan.minutes(), 2)); break;
case 'm': result.append(NumberFormatter::format(timespan.totalMinutes())); break;
case 'S': result.append(NumberFormatter::format0(timespan.seconds(), 2)); break;
case 's': result.append(NumberFormatter::format(timespan.totalSeconds())); break;
case 'i': result.append(NumberFormatter::format0(timespan.milliseconds(), 3)); break;
case 'c': result.append(NumberFormatter::format(timespan.milliseconds()/100)); break;
default: result += *it;
case 'd': NumberFormatter::append(str, timespan.days()); break;
case 'H': NumberFormatter::append0(str, timespan.hours(), 2); break;
case 'h': NumberFormatter::append(str, timespan.totalHours()); break;
case 'M': NumberFormatter::append0(str, timespan.minutes(), 2); break;
case 'm': NumberFormatter::append(str, timespan.totalMinutes()); break;
case 'S': NumberFormatter::append0(str, timespan.seconds(), 2); break;
case 's': NumberFormatter::append(str, timespan.totalSeconds()); break;
case 'i': NumberFormatter::append0(str, timespan.milliseconds(), 3); break;
case 'c': NumberFormatter::append(str, timespan.milliseconds()/100); break;
default: str += *it;
}
++it;
}
}
else result += *it++;
else str += *it++;
}
return result;
}
std::string DateTimeFormatter::tzdISO(int timeZoneDifferential)
void DateTimeFormatter::tzdISO(std::string& str, int timeZoneDifferential)
{
std::string tzd;
tzd.reserve(8);
if (timeZoneDifferential != UTC)
{
if (timeZoneDifferential >= 0)
{
tzd += '+';
tzd += NumberFormatter::format0(timeZoneDifferential/3600, 2);
tzd += ':';
tzd += NumberFormatter::format0((timeZoneDifferential%3600)/60, 2);
str += '+';
NumberFormatter::append0(str, timeZoneDifferential/3600, 2);
str += ':';
NumberFormatter::append0(str, (timeZoneDifferential%3600)/60, 2);
}
else
{
tzd += '-';
tzd += NumberFormatter::format0(-timeZoneDifferential/3600, 2);
tzd += ':';
tzd += NumberFormatter::format0((-timeZoneDifferential%3600)/60, 2);
str += '-';
NumberFormatter::append0(str, -timeZoneDifferential/3600, 2);
str += ':';
NumberFormatter::append0(str, (-timeZoneDifferential%3600)/60, 2);
}
}
else tzd = "Z";
return tzd;
else str += 'Z';
}
std::string DateTimeFormatter::tzdRFC(int timeZoneDifferential)
void DateTimeFormatter::tzdRFC(std::string& str, int timeZoneDifferential)
{
std::string tzd;
tzd.reserve(8);
if (timeZoneDifferential != UTC)
{
if (timeZoneDifferential >= 0)
{
tzd += '+';
tzd += NumberFormatter::format0(timeZoneDifferential/3600, 2);
tzd += NumberFormatter::format0((timeZoneDifferential%3600)/60, 2);
str += '+';
NumberFormatter::append0(str, timeZoneDifferential/3600, 2);
NumberFormatter::append0(str, (timeZoneDifferential%3600)/60, 2);
}
else
{
tzd += '-';
tzd += NumberFormatter::format0(-timeZoneDifferential/3600, 2);
tzd += NumberFormatter::format0((-timeZoneDifferential%3600)/60, 2);
str += '-';
NumberFormatter::append0(str, -timeZoneDifferential/3600, 2);
NumberFormatter::append0(str, (-timeZoneDifferential%3600)/60, 2);
}
}
else tzd = "GMT";
return tzd;
else str += "GMT";
}

View File

@@ -1,13 +1,13 @@
//
// NumberFormatter.cpp
//
// $Id: //poco/svn/Foundation/src/NumberFormatter.cpp#2 $
// $Id: //poco/Main/Foundation/src/NumberFormatter.cpp#13 $
//
// Library: Foundation
// Package: Core
// Module: NumberFormatter
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
@@ -51,325 +51,325 @@
namespace Poco {
std::string NumberFormatter::format(int value)
void NumberFormatter::append(std::string& str, int value)
{
char buffer[64];
std::sprintf(buffer, "%d", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(int value, int width)
void NumberFormatter::append(std::string& str, int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*d", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(int value, int width)
void NumberFormatter::append0(std::string& str, int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*d", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(int value)
void NumberFormatter::appendHex(std::string& str, int value)
{
char buffer[64];
std::sprintf(buffer, "%X", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(int value, int width)
void NumberFormatter::appendHex(std::string& str, int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*X", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(unsigned value)
void NumberFormatter::append(std::string& str, unsigned value)
{
char buffer[64];
std::sprintf(buffer, "%u", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(unsigned value, int width)
void NumberFormatter::append(std::string& str, unsigned value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*u", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(unsigned int value, int width)
void NumberFormatter::append0(std::string& str, unsigned int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*u", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(unsigned value)
void NumberFormatter::appendHex(std::string& str, unsigned value)
{
char buffer[64];
std::sprintf(buffer, "%X", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(unsigned value, int width)
void NumberFormatter::appendHex(std::string& str, unsigned value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*X", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(long value)
void NumberFormatter::append(std::string& str, long value)
{
char buffer[64];
std::sprintf(buffer, "%ld", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(long value, int width)
void NumberFormatter::append(std::string& str, long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*ld", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(long value, int width)
void NumberFormatter::append0(std::string& str, long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*ld", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(long value)
void NumberFormatter::appendHex(std::string& str, long value)
{
char buffer[64];
std::sprintf(buffer, "%lX", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(long value, int width)
void NumberFormatter::appendHex(std::string& str, long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*lX", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(unsigned long value)
void NumberFormatter::append(std::string& str, unsigned long value)
{
char buffer[64];
std::sprintf(buffer, "%lu", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(unsigned long value, int width)
void NumberFormatter::append(std::string& str, unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*lu", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(unsigned long value, int width)
void NumberFormatter::append0(std::string& str, unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*lu", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(unsigned long value)
void NumberFormatter::appendHex(std::string& str, unsigned long value)
{
char buffer[64];
std::sprintf(buffer, "%lX", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(unsigned long value, int width)
void NumberFormatter::appendHex(std::string& str, unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*lX", width, value);
return std::string(buffer);
str.append(buffer);
}
#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
std::string NumberFormatter::format(Int64 value)
void NumberFormatter::append(std::string& str, Int64 value)
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"d", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(Int64 value, int width)
void NumberFormatter::append(std::string& str, Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*"I64_FMT"d", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(Int64 value, int width)
void NumberFormatter::append0(std::string& str, Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"d", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(Int64 value)
void NumberFormatter::appendHex(std::string& str, Int64 value)
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"X", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(Int64 value, int width)
void NumberFormatter::appendHex(std::string& str, Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"X", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(UInt64 value)
void NumberFormatter::append(std::string& str, UInt64 value)
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"u", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(UInt64 value, int width)
void NumberFormatter::append(std::string& str, UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%*"I64_FMT"u", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format0(UInt64 value, int width)
void NumberFormatter::append0(std::string& str, UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"u", width, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(UInt64 value)
void NumberFormatter::appendHex(std::string& str, UInt64 value)
{
char buffer[64];
std::sprintf(buffer, "%"I64_FMT"X", value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::formatHex(UInt64 value, int width)
void NumberFormatter::appendHex(std::string& str, UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
std::sprintf(buffer, "%0*"I64_FMT"X", width, value);
return std::string(buffer);
str.append(buffer);
}
#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
std::string NumberFormatter::format(float value)
void NumberFormatter::append(std::string& str, float value)
{
char buffer[64];
std::sprintf(buffer, "%.*g", 10, (double) value);
return std::string(buffer);
std::sprintf(buffer, "%.*g", 8, (double) value);
str.append(buffer);
}
std::string NumberFormatter::format(double value)
void NumberFormatter::append(std::string& str, double value)
{
char buffer[64];
std::sprintf(buffer, "%.*g", 20, value);
return std::string(buffer);
std::sprintf(buffer, "%.*g", 16, value);
str.append(buffer);
}
std::string NumberFormatter::format(double value, int precision)
void NumberFormatter::append(std::string& str, double value, int precision)
{
poco_assert (precision >= 0 && precision < 32);
char buffer[64];
std::sprintf(buffer, "%.*f", precision, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(double value, int width, int precision)
void NumberFormatter::append(std::string& str, double value, int width, int precision)
{
poco_assert (width > 0 && width < 64 && precision >= 0 && precision < width);
char buffer[64];
std::sprintf(buffer, "%*.*f", width, precision, value);
return std::string(buffer);
str.append(buffer);
}
std::string NumberFormatter::format(const void* ptr)
void NumberFormatter::append(std::string& str, const void* ptr)
{
char buffer[24];
#if defined(POCO_PTR_IS_64_BIT)
@@ -377,7 +377,7 @@ std::string NumberFormatter::format(const void* ptr)
#else
std::sprintf(buffer, "%08lX", (UIntPtr) ptr);
#endif
return std::string(buffer);
str.append(buffer);
}

View File

@@ -1,7 +1,7 @@
//
// RotateStrategy.cpp
//
// $Id: //poco/1.3/Foundation/src/RotateStrategy.cpp#2 $
// $Id: //poco/Main/Foundation/src/RotateStrategy.cpp#8 $
//
// Library: Foundation
// Package: Logging
@@ -101,7 +101,7 @@ bool RotateByIntervalStrategy::mustRotate(LogFile* pFile)
{
_lastRotate.update();
std::string tag(ROTATE_TEXT);
tag += DateTimeFormatter::format(_lastRotate, DateTimeFormat::RFC1036_FORMAT);
DateTimeFormatter::append(tag, _lastRotate, DateTimeFormat::RFC1036_FORMAT);
pFile->write(tag);
}
}

View File

@@ -1,7 +1,7 @@
//
// URI.cpp
//
// $Id: //poco/svn/Foundation/src/URI.cpp#2 $
// $Id: //poco/Main/Foundation/src/URI.cpp#15 $
//
// Library: Foundation
// Package: URI
@@ -293,7 +293,7 @@ std::string URI::getAuthority() const
if (_port && !isWellKnownPort())
{
auth += ':';
auth.append(NumberFormatter::format(_port));
NumberFormatter::append(auth, _port);
}
return auth;
}