Change format syntax to a simple custom %f/%#f scheme

This commit is contained in:
Vladimir Panteleev
2017-12-10 04:44:13 +00:00
parent fd8a36d3ea
commit 15b1f8d8fa
4 changed files with 18 additions and 24 deletions
+4 -6
View File
@@ -79,13 +79,11 @@ class CUtils {
static CString CTime(time_t t, const CString& sTZ);
static CString FormatTime(time_t t, const CString& sFormat,
const CString& sTZ);
/** Supports additional format specifiers for formatting sub-second values:
/** Supports an additional format specifier for formatting sub-second values:
*
* - %L - millisecond, 3 digits (ruby extension)
* - %N - sub-second fraction (ruby extension)
* - %3N - millisecond
* - %6N - microsecond
* - %9N - nanosecond (default if no digit specified)
* - %f - sub-second fraction
* - %3f - millisecond (default, if no width is specified)
* - %6f - microsecond
*
* However, note that timeval only supports microsecond precision
* (thus, formatting with higher-than-microsecond precision will
+1 -1
View File
@@ -157,7 +157,7 @@ class CListSockets : public CModule {
timeval tv;
tv.tv_sec = iStartTime / 1000;
tv.tv_usec = iStartTime % 1000 * 1000;
return CUtils::FormatTime(tv, "%Y-%m-%d %H:%M:%S.%L",
return CUtils::FormatTime(tv, "%Y-%m-%d %H:%M:%S.%f",
GetUser()->GetTimezone());
}
+4 -7
View File
@@ -506,8 +506,8 @@ CString CUtils::FormatTime(const timeval& tv, const CString& sFormat,
// specifiers is undefined.
CString sFormat2;
// Make sure %% is parsed correctly, i.e. %%L is passed through to
// strftime to become %L, and not 123.
// Make sure %% is parsed correctly, i.e. %%f is passed through to
// strftime to become %f, and not 123.
bool bInFormat = false;
int iDigits;
CString::size_type uLastCopied = 0, uFormatStart;
@@ -517,7 +517,7 @@ CString CUtils::FormatTime(const timeval& tv, const CString& sFormat,
if (sFormat[i] == '%') {
uFormatStart = i;
bInFormat = true;
iDigits = 9;
iDigits = 3;
}
} else {
switch (sFormat[i]) {
@@ -525,10 +525,7 @@ CString CUtils::FormatTime(const timeval& tv, const CString& sFormat,
case '5': case '6': case '7': case '8': case '9':
iDigits = sFormat[i] - '0';
break;
case 'L':
iDigits = 3;
// fall-through
case 'N': {
case 'f': {
int iVal = tv.tv_usec;
int iDigitDelta = iDigits - 6; // tv_user is in 10^-6 seconds
for (; iDigitDelta > 0; iDigitDelta--)
+9 -10
View File
@@ -123,21 +123,20 @@ class TimeTest : public testing::TestWithParam<
TEST_P(TimeTest, FormatTime) {
timeval tv = std::get<0>(GetParam());
EXPECT_EQ(std::get<1>(GetParam()), CUtils::FormatTime(tv, "%s.%L", "UTC"));
EXPECT_EQ(std::get<2>(GetParam()), CUtils::FormatTime(tv, "%s.%N", "UTC"));
EXPECT_EQ(std::get<3>(GetParam()), CUtils::FormatTime(tv, "%s.%3N", "UTC"));
EXPECT_EQ(std::get<1>(GetParam()), CUtils::FormatTime(tv, "%s.%f", "UTC"));
EXPECT_EQ(std::get<2>(GetParam()), CUtils::FormatTime(tv, "%s.%6f", "UTC"));
EXPECT_EQ(std::get<3>(GetParam()), CUtils::FormatTime(tv, "%s.%9f", "UTC"));
}
INSTANTIATE_TEST_CASE_P(
TimeTest, TimeTest,
testing::Values(
// leading zeroes
std::make_tuple(timeval{42, 12345}, "42.012", "42.012345000", "42.012"),
std::make_tuple(timeval{42, 12345}, "42.012", "42.012345", "42.012345000"),
// (no) rounding
std::make_tuple(timeval{42, 999999}, "42.999", "42.999999000",
"42.999"),
std::make_tuple(timeval{42, 999999}, "42.999", "42.999999", "42.999999000"),
// no tv_usec part
std::make_tuple(timeval{42, 0}, "42.000", "42.000000000", "42.000")));
std::make_tuple(timeval{42, 0}, "42.000", "42.000000", "42.000000000")));
TEST(UtilsTest, FormatTime) {
// Test passthrough
@@ -151,10 +150,10 @@ TEST(UtilsTest, FormatTime) {
timeval tv2;
tv2.tv_sec = 42;
tv2.tv_usec = 123456;
CString str2 = CUtils::FormatTime(tv2, "%%L", "UTC");
EXPECT_EQ("%L", str2);
CString str2 = CUtils::FormatTime(tv2, "%%f", "UTC");
EXPECT_EQ("%f", str2);
// Test suffix
CString str3 = CUtils::FormatTime(tv2, "a%Lb", "UTC");
CString str3 = CUtils::FormatTime(tv2, "a%fb", "UTC");
EXPECT_EQ("a123b", str3);
}