Merge pull request #745 from patrick96/issues/744

fix(date): date and time string limited to 63 chars
This commit is contained in:
NBonaparte 2017-09-17 13:20:55 -07:00 committed by GitHub
commit 27f53eb129
2 changed files with 19 additions and 11 deletions

View File

@ -3,6 +3,10 @@
#include "modules/meta/input_handler.hpp" #include "modules/meta/input_handler.hpp"
#include "modules/meta/timer_module.hpp" #include "modules/meta/timer_module.hpp"
#include <iostream>
#include <iomanip>
#include <ctime>
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
@ -33,6 +37,9 @@ namespace modules {
string m_date; string m_date;
string m_time; string m_time;
// Single stringstream to be used to gather the results of std::put_time
std::stringstream datetime_stream;
std::atomic<bool> m_toggled{false}; std::atomic<bool> m_toggled{false};
}; };
} }

View File

@ -10,7 +10,7 @@ namespace modules {
date_module::date_module(const bar_settings& bar, string name_) : timer_module<date_module>(bar, move(name_)) { date_module::date_module(const bar_settings& bar, string name_) : timer_module<date_module>(bar, move(name_)) {
if (!m_bar.locale.empty()) { 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), '"'); m_dateformat = string_util::trim(m_conf.get(name(), "date", ""s), '"');
@ -42,22 +42,23 @@ namespace modules {
auto time = std::time(nullptr); auto time = std::time(nullptr);
auto date_format = m_toggled ? m_dateformat_alt : m_dateformat; auto date_format = m_toggled ? m_dateformat_alt : m_dateformat;
char date_buffer[64]{'\0'}; // Clear stream contents
strftime(date_buffer, sizeof(date_buffer), date_format.c_str(), localtime(&time)); 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; auto time_format = m_toggled ? m_timeformat_alt : m_timeformat;
char time_buffer[64]{'\0'}; // Clear stream contents
strftime(time_buffer, sizeof(time_buffer), time_format.c_str(), localtime(&time)); 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_buffer, m_date.c_str(), sizeof(date_buffer)) != 0}; if (m_date == date_string && m_time == time_string) {
bool time_changed{strncmp(time_buffer, m_time.c_str(), sizeof(time_buffer)) != 0};
if (!date_changed && !time_changed) {
return false; return false;
} }
m_date = string{date_buffer}; m_date = date_string;
m_time = string{time_buffer}; m_time = time_string;
if (m_label) { if (m_label) {
m_label->reset_tokens(); m_label->reset_tokens();