From 15b1f8d8fa39f931c41580e55eb1514e86563a31 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Sun, 10 Dec 2017 04:44:13 +0000 Subject: [PATCH] Change format syntax to a simple custom %f/%#f scheme --- include/znc/Utils.h | 10 ++++------ modules/listsockets.cpp | 2 +- src/Utils.cpp | 11 ++++------- test/UtilsTest.cpp | 19 +++++++++---------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/include/znc/Utils.h b/include/znc/Utils.h index 2ba62996..8029f249 100644 --- a/include/znc/Utils.h +++ b/include/znc/Utils.h @@ -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 diff --git a/modules/listsockets.cpp b/modules/listsockets.cpp index c35013c4..d3ec7170 100644 --- a/modules/listsockets.cpp +++ b/modules/listsockets.cpp @@ -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()); } diff --git a/src/Utils.cpp b/src/Utils.cpp index 72d49f58..6f40d6ed 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -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--) diff --git a/test/UtilsTest.cpp b/test/UtilsTest.cpp index 0fe06e92..aed89deb 100644 --- a/test/UtilsTest.cpp +++ b/test/UtilsTest.cpp @@ -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); }