From fe97d3415643eb4e542637b7deeff4c5784494f7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 15 Aug 2020 12:52:35 -0400 Subject: [PATCH] common/time_zone: Simplify GetOsTimeZoneOffset() We can simplify this function down into a single line with the use of fmt. A benefit with the fmt approach is that the fmt variant of localtime is thread-safe as well, making GetOsTimeZoneOffset() thread-safe as well. --- src/common/time_zone.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index ce239eb63..7aa1b59ea 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp @@ -3,8 +3,9 @@ // Refer to the license.txt file included. #include -#include -#include +#include + +#include #include "common/logging/log.h" #include "common/time_zone.h" @@ -16,13 +17,8 @@ std::string GetDefaultTimeZone() { } static std::string GetOsTimeZoneOffset() { - const std::time_t t{std::time(nullptr)}; - const std::tm tm{*std::localtime(&t)}; - - std::stringstream ss; - ss << std::put_time(&tm, "%z"); // Get the current timezone offset, e.g. "-400", as a string - - return ss.str(); + // Get the current timezone offset, e.g. "-400", as a string + return fmt::format("%z", fmt::localtime(std::time(nullptr))); } static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) {