Add dumpsys finish timestamp

Dumpsys are very helpful to debug issues.
Nevertheless a recurrent problem is that the dumpsys take time especially
if multiple are taken like during a bug report generation.
Due to this delay, it can be hard to understand during
which logs a dumpsys was taken.

This commit changes the last line of a dumpsys to contain the time it
was generated.
Here is an example:

[... normal dumpsys...]
    - Mix ID 8 I/O handle 37
  - 1 sinks:
    - Device ID 7 AUDIO_DEVICE_OUT_TELEPHONY_TX
Audio Policy Mix:
--------- 0.036s was the duration of dumpsys media.audio_policy, ending at: 2017-08-14 20:35:43

This patch adds the last part:
*, ending at: 2017-08-14 20:35:43*

Bug: 64699427
Test: adb bugreport
Test:  mmm -j32 frameworks/native/cmds/dumpsys && adb sync data && adb shell /data/nativetest/dumpsys_test/dumpsys_test --gtest_filter=-DumpsysTest.DumpRunningServiceTimeout
       # DumpRunningServiceTimeout is broken independently of this patch
Change-Id: I9873e7dd915b4f7cdd6eac5c44487ae5740fa805
Signed-off-by: Kevin Rocard <krocard@google.com>
diff --git a/cmds/dumpsys/dumpsys.cpp b/cmds/dumpsys/dumpsys.cpp
index f0e7200..fa6f6df 100644
--- a/cmds/dumpsys/dumpsys.cpp
+++ b/cmds/dumpsys/dumpsys.cpp
@@ -16,6 +16,7 @@
 
 #include <algorithm>
 #include <chrono>
+#include <iomanip>
 #include <thread>
 
 #include <android-base/file.h>
@@ -282,7 +283,14 @@
               std::chrono::duration<double> elapsed_seconds =
                   std::chrono::steady_clock::now() - start;
               aout << StringPrintf("--------- %.3fs ", elapsed_seconds.count()).c_str()
-                   << "was the duration of dumpsys " << service_name << endl;
+                   << "was the duration of dumpsys " << service_name;
+
+              using std::chrono::system_clock;
+              const auto finish = system_clock::to_time_t(system_clock::now());
+              std::tm finish_tm;
+              localtime_r(&finish, &finish_tm);
+              aout << ", ending at: " << std::put_time(&finish_tm, "%Y-%m-%d %H:%M:%S")
+                   << endl;
             }
         } else {
             aerr << "Can't find service: " << service_name << endl;