Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d9e4544b authored by Chris Manton's avatar Chris Manton
Browse files

Add bluetooth::common::StringFormatTime

Bug: 163134718
Tag: #refactor
Test: gd/cert/run --host

Change-Id: I450dc10c832a11899e07da372a9cc14b5bef9ee9
parent 043699c0
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -90,5 +90,11 @@ std::string StringFormat(const std::string& format, Args... args) {
  return std::string(buffer, size);
}

inline std::string StringFormatTime(const std::string& format, const struct std::tm& tm) {
  std::ostringstream os;
  os << std::put_time(&tm, format.c_str());
  return os.str();
}

}  // namespace common
}  // namespace bluetooth
+10 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ using bluetooth::common::BoolFromString;
using bluetooth::common::FromHexString;
using bluetooth::common::Int64FromString;
using bluetooth::common::StringFormat;
using bluetooth::common::StringFormatTime;
using bluetooth::common::StringJoin;
using bluetooth::common::StringSplit;
using bluetooth::common::StringTrim;
@@ -174,4 +175,12 @@ TEST(StringsTest, string_format_test) {
  ASSERT_THAT(StringFormat("%d %.1f 0x%02x", 42, 43.123, 0x8), StrEq("42 43.1 0x08"));
}

TEST(StringsTest, string_format_time_test) {
  std::string format("%Y-%m-%d %H:%M:%S");
  time_t then = 123456789;
  struct std::tm tm;
  localtime_r(&then, &tm);
  ASSERT_THAT(StringFormatTime(format, tm), StrEq("1973-11-29 13:33:09"));
}

}  // namespace testing