From 6ead63d02dce9dbeb130c96e2c513c0195a4edef Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Fri, 15 Jan 2016 18:15:05 -0800 Subject: [PATCH] Extract timestamp formatting to string_format. --- firmware/application/log_file.cpp | 9 +-------- firmware/application/string_format.cpp | 8 ++++++++ firmware/application/string_format.hpp | 6 ++++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/firmware/application/log_file.cpp b/firmware/application/log_file.cpp index 8cf4c3c2..43528aa4 100644 --- a/firmware/application/log_file.cpp +++ b/firmware/application/log_file.cpp @@ -67,14 +67,7 @@ bool LogFile::is_ready() { } bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) { - std::string timestamp = - to_string_dec_uint(datetime.year(), 4, '0') + - to_string_dec_uint(datetime.month(), 2, '0') + - to_string_dec_uint(datetime.day(), 2, '0') + - to_string_dec_uint(datetime.hour(), 2, '0') + - to_string_dec_uint(datetime.minute(), 2, '0') + - to_string_dec_uint(datetime.second(), 2, '0'); - + std::string timestamp = to_string_timestamp(datetime); return write(timestamp + " " + entry + "\r\n"); } diff --git a/firmware/application/string_format.cpp b/firmware/application/string_format.cpp index be5f988c..596cc47c 100644 --- a/firmware/application/string_format.cpp +++ b/firmware/application/string_format.cpp @@ -112,3 +112,11 @@ std::string to_string_hex(const uint32_t n, const int32_t l) { return p; } +std::string to_string_timestamp(const rtc::RTC& value) { + return to_string_dec_uint(value.year(), 4, '0') + + to_string_dec_uint(value.month(), 2, '0') + + to_string_dec_uint(value.day(), 2, '0') + + to_string_dec_uint(value.hour(), 2, '0') + + to_string_dec_uint(value.minute(), 2, '0') + + to_string_dec_uint(value.second(), 2, '0'); +} diff --git a/firmware/application/string_format.hpp b/firmware/application/string_format.hpp index 0eba017f..6f758478 100644 --- a/firmware/application/string_format.hpp +++ b/firmware/application/string_format.hpp @@ -25,10 +25,16 @@ #include #include +// BARF! rtc::RTC is leaking everywhere. +#include "lpc43xx_cpp.hpp" +using namespace lpc43xx; + // TODO: Allow l=0 to not fill/justify? Already using this way in ui_spectrum.hpp... std::string to_string_dec_uint(const uint32_t n, const int32_t l = 0, const char fill = 0); std::string to_string_dec_int(const int32_t n, const int32_t l = 0, const char fill = 0); std::string to_string_hex(const uint32_t n, const int32_t l = 0); +std::string to_string_timestamp(const rtc::RTC& value); + #endif/*__STRING_FORMAT_H__*/