From 78ffbb8b8920d569c57c2e2548a3887eb73cd4c0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Klemens=20Sch=C3=B6lhorn?=
 <klemens.schoelhorn@advantest.com>
Date: Thu, 5 Sep 2019 17:18:46 +0200
Subject: [PATCH] CachedDateFormat: Correct fix for  handling ts with 654 as ms
 part

The previous commit [1] tried to fix the issue that findMillisecondStart
does not work correctly when the millisecond part of the timestamp is
654 ms. However, it only fixed the issue for the special case when the
millisecond part was 654 and the microsecond part was 0, even though the
microseconds should not matter.

Fix this by comparing in milliseconds instead of in microseconds.

Fixes LOGCXX-506.

[1]: 396c8abfe92136db6ba2453bf804ba8ca48a923d
---
 src/main/cpp/cacheddateformat.cpp                 | 7 +++----
 src/test/cpp/helpers/cacheddateformattestcase.cpp | 8 ++++++++
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/main/cpp/cacheddateformat.cpp b/src/main/cpp/cacheddateformat.cpp
index c813eceb..6413baf3 100644
--- a/src/main/cpp/cacheddateformat.cpp
+++ b/src/main/cpp/cacheddateformat.cpp
@@ -124,13 +124,12 @@ int CachedDateFormat::findMillisecondStart(
 		slotBegin -= 1000000;
 	}
 
-	int micros = (int) (time - slotBegin);
+	int millis = (int) (time - slotBegin) / 1000;
 
 	// the magic numbers are in microseconds
 	int magic = magic1;
 	LogString magicString(magicString1);
-
-	if (micros == magic1)
+	if (millis == magic1 / 1000)
 	{
 		magic = magic2;
 		magicString = magicString2;
@@ -158,7 +157,7 @@ int CachedDateFormat::findMillisecondStart(
 				//   determine the expected digits for the base time
 				const logchar abc[] = { 0x41, 0x42, 0x43, 0 };
 				LogString formattedMillis(abc);
-				millisecondFormat(micros / 1000, formattedMillis, 0);
+				millisecondFormat(millis, formattedMillis, 0);
 
 				LogString plusZero;
 				formatter->format(plusZero, slotBegin, pool);
diff --git a/src/test/cpp/helpers/cacheddateformattestcase.cpp b/src/test/cpp/helpers/cacheddateformattestcase.cpp
index 7148834a..74d180ec 100644
--- a/src/test/cpp/helpers/cacheddateformattestcase.cpp
+++ b/src/test/cpp/helpers/cacheddateformattestcase.cpp
@@ -662,6 +662,14 @@ void test22() {
     formatted.clear();
     isoFormat.format(formatted, 999000, p);
     LOGUNIT_ASSERT_EQUAL(LOG4CXX_STR("1970-01-01 00:00:00,999"), formatted);
+
+    formatted.clear();
+    isoFormat.format(formatted, 1654010, p);
+    LOGUNIT_ASSERT_EQUAL(LOG4CXX_STR("1970-01-01 00:00:01,654"), formatted);
+
+    formatted.clear();
+    isoFormat.format(formatted, 1999010, p);
+    LOGUNIT_ASSERT_EQUAL(LOG4CXX_STR("1970-01-01 00:00:01,999"), formatted);
 }
 
 };
-- 
2.16.1.2.g9d582f143

