From 6e9e739d81a72c771345e7688b35a225898977c3 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Fri, 15 Sep 2017 14:13:35 +0200
Subject: [PATCH 1/4] fix(date): Remove string length limitation
Fixes #744
---
include/modules/date.hpp | 6 ++++++
src/modules/date.cpp | 26 ++++++++++++++++++--------
2 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/include/modules/date.hpp b/include/modules/date.hpp
index f20c076f..e13a7510 100644
--- a/include/modules/date.hpp
+++ b/include/modules/date.hpp
@@ -3,6 +3,10 @@
#include "modules/meta/input_handler.hpp"
#include "modules/meta/timer_module.hpp"
+#include
+#include
+#include
+
POLYBAR_NS
namespace modules {
@@ -34,6 +38,8 @@ namespace modules {
string m_time;
std::atomic m_toggled{false};
+
+ void set_stream_locale(std::stringstream &stream);
};
}
diff --git a/src/modules/date.cpp b/src/modules/date.cpp
index e86de6be..3c143e56 100644
--- a/src/modules/date.cpp
+++ b/src/modules/date.cpp
@@ -38,26 +38,36 @@ namespace modules {
}
}
+ void date_module::set_stream_locale(std::stringstream &stream) {
+ if(!m_bar.locale.empty()) {
+ stream.imbue(std::locale(m_bar.locale.c_str()));
+ }
+ }
+
bool date_module::update() {
auto time = std::time(nullptr);
auto date_format = m_toggled ? m_dateformat_alt : m_dateformat;
- char date_buffer[64]{'\0'};
- strftime(date_buffer, sizeof(date_buffer), date_format.c_str(), localtime(&time));
+ std::stringstream date_stream;
+ set_stream_locale(date_stream);
+ date_stream << std::put_time(localtime(&time), date_format.c_str());
+ auto date_string = date_stream.str();
auto time_format = m_toggled ? m_timeformat_alt : m_timeformat;
- char time_buffer[64]{'\0'};
- strftime(time_buffer, sizeof(time_buffer), time_format.c_str(), localtime(&time));
+ std::stringstream time_stream;
+ set_stream_locale(time_stream);
+ time_stream << std::put_time(localtime(&time), time_format.c_str());
+ auto time_string = time_stream.str();
- bool date_changed{strncmp(date_buffer, m_date.c_str(), sizeof(date_buffer)) != 0};
- bool time_changed{strncmp(time_buffer, m_time.c_str(), sizeof(time_buffer)) != 0};
+ bool date_changed{strncmp(date_string.c_str(), m_date.c_str(), sizeof(date_string)) != 0};
+ bool time_changed{strncmp(time_string.c_str(), m_time.c_str(), sizeof(time_string)) != 0};
if (!date_changed && !time_changed) {
return false;
}
- m_date = string{date_buffer};
- m_time = string{time_buffer};
+ m_date = date_string;
+ m_time = time_string;
if (m_label) {
m_label->reset_tokens();
From 1bc9933a8811eb8cb00e02bf38a7458efba4e854 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Fri, 15 Sep 2017 14:21:38 +0200
Subject: [PATCH 2/4] refactor(date): Use single stringstream
Reduces code repetition and removes the need for setting the locale in
every update
---
include/modules/date.hpp | 5 +++--
src/modules/date.cpp | 23 +++++++++--------------
2 files changed, 12 insertions(+), 16 deletions(-)
diff --git a/include/modules/date.hpp b/include/modules/date.hpp
index e13a7510..bcf0802a 100644
--- a/include/modules/date.hpp
+++ b/include/modules/date.hpp
@@ -37,9 +37,10 @@ namespace modules {
string m_date;
string m_time;
- std::atomic m_toggled{false};
+ // Single stringstream to be used to gather the results of std::put_time
+ std::stringstream datetime_stream;
- void set_stream_locale(std::stringstream &stream);
+ std::atomic m_toggled{false};
};
}
diff --git a/src/modules/date.cpp b/src/modules/date.cpp
index 3c143e56..2f46cd1b 100644
--- a/src/modules/date.cpp
+++ b/src/modules/date.cpp
@@ -11,6 +11,7 @@ namespace modules {
date_module::date_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) {
if (!m_bar.locale.empty()) {
setlocale(LC_TIME, m_bar.locale.c_str());
+ datetime_stream.imbue(std::locale(m_bar.locale.c_str()));
}
m_dateformat = string_util::trim(m_conf.get(name(), "date", ""s), '"');
@@ -38,26 +39,20 @@ namespace modules {
}
}
- void date_module::set_stream_locale(std::stringstream &stream) {
- if(!m_bar.locale.empty()) {
- stream.imbue(std::locale(m_bar.locale.c_str()));
- }
- }
-
bool date_module::update() {
auto time = std::time(nullptr);
auto date_format = m_toggled ? m_dateformat_alt : m_dateformat;
- std::stringstream date_stream;
- set_stream_locale(date_stream);
- date_stream << std::put_time(localtime(&time), date_format.c_str());
- auto date_string = date_stream.str();
+ // Clear stream contents
+ datetime_stream.str("");
+ datetime_stream << std::put_time(localtime(&time), date_format.c_str());
+ auto date_string = datetime_stream.str();
auto time_format = m_toggled ? m_timeformat_alt : m_timeformat;
- std::stringstream time_stream;
- set_stream_locale(time_stream);
- time_stream << std::put_time(localtime(&time), time_format.c_str());
- auto time_string = time_stream.str();
+ // Clear stream contents
+ datetime_stream.str("");
+ datetime_stream << std::put_time(localtime(&time), time_format.c_str());
+ auto time_string = datetime_stream.str();
bool date_changed{strncmp(date_string.c_str(), m_date.c_str(), sizeof(date_string)) != 0};
bool time_changed{strncmp(time_string.c_str(), m_time.c_str(), sizeof(time_string)) != 0};
From 0024e1e9e790b7501df2f6e972b67e56c84a4936 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Fri, 15 Sep 2017 14:22:42 +0200
Subject: [PATCH 3/4] refactor(date): Remove obsolescent setlocale call
---
src/modules/date.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/modules/date.cpp b/src/modules/date.cpp
index 2f46cd1b..d108de39 100644
--- a/src/modules/date.cpp
+++ b/src/modules/date.cpp
@@ -10,7 +10,6 @@ namespace modules {
date_module::date_module(const bar_settings& bar, string name_) : timer_module(bar, move(name_)) {
if (!m_bar.locale.empty()) {
- setlocale(LC_TIME, m_bar.locale.c_str());
datetime_stream.imbue(std::locale(m_bar.locale.c_str()));
}
From b6a2c575fdd606388041b5a8adca68cb03f0e260 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sat, 16 Sep 2017 00:09:26 +0200
Subject: [PATCH 4/4] refactor(date): Use cpp string comparison
---
src/modules/date.cpp | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/src/modules/date.cpp b/src/modules/date.cpp
index d108de39..048605bd 100644
--- a/src/modules/date.cpp
+++ b/src/modules/date.cpp
@@ -53,10 +53,7 @@ namespace modules {
datetime_stream << std::put_time(localtime(&time), time_format.c_str());
auto time_string = datetime_stream.str();
- bool date_changed{strncmp(date_string.c_str(), m_date.c_str(), sizeof(date_string)) != 0};
- bool time_changed{strncmp(time_string.c_str(), m_time.c_str(), sizeof(time_string)) != 0};
-
- if (!date_changed && !time_changed) {
+ if (m_date == date_string && m_time == time_string) {
return false;
}