fix(date): Remove string length limitation

Fixes #744
This commit is contained in:
patrick96 2017-09-15 14:13:35 +02:00
parent ac355c3747
commit 6e9e739d81
No known key found for this signature in database
GPG Key ID: 521E5E03AEBCA1A7
2 changed files with 24 additions and 8 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 {
@ -34,6 +38,8 @@ namespace modules {
string m_time; string m_time;
std::atomic<bool> m_toggled{false}; std::atomic<bool> m_toggled{false};
void set_stream_locale(std::stringstream &stream);
}; };
} }

View File

@ -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() { bool date_module::update() {
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'}; std::stringstream date_stream;
strftime(date_buffer, sizeof(date_buffer), date_format.c_str(), localtime(&time)); 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; auto time_format = m_toggled ? m_timeformat_alt : m_timeformat;
char time_buffer[64]{'\0'}; std::stringstream time_stream;
strftime(time_buffer, sizeof(time_buffer), time_format.c_str(), localtime(&time)); 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 date_changed{strncmp(date_string.c_str(), m_date.c_str(), sizeof(date_string)) != 0};
bool time_changed{strncmp(time_buffer, m_time.c_str(), sizeof(time_buffer)) != 0}; bool time_changed{strncmp(time_string.c_str(), m_time.c_str(), sizeof(time_string)) != 0};
if (!date_changed && !time_changed) { 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();