From cee97be8d444441256f3b26db2c8061baa767894 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sat, 5 Jan 2019 14:53:25 -0800 Subject: [PATCH] Add GncDateTime::timestamp(). To provide a C++ implementation of gnc_date_timestamp and to avoid using the expensive and localized GncDateTime::format(). --- libgnucash/engine/gnc-date.cpp | 2 +- libgnucash/engine/gnc-datetime.cpp | 15 +++++++++++++++ libgnucash/engine/gnc-datetime.hpp | 6 +++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/libgnucash/engine/gnc-date.cpp b/libgnucash/engine/gnc-date.cpp index cacc35f8e8..1e3236d5fc 100644 --- a/libgnucash/engine/gnc-date.cpp +++ b/libgnucash/engine/gnc-date.cpp @@ -1075,7 +1075,7 @@ qof_strftime(gchar *buf, gsize max, const gchar *format, const struct tm *tm) gchar * gnc_date_timestamp (void) { - return gnc_print_time64(gnc_time(nullptr), "%Y%m%d%H%M%S"); + return g_strdup(GncDateTime::timestamp().c_str()); } /********************************************************************\ diff --git a/libgnucash/engine/gnc-datetime.cpp b/libgnucash/engine/gnc-datetime.cpp index 70d927537c..a9c4f6be80 100644 --- a/libgnucash/engine/gnc-datetime.cpp +++ b/libgnucash/engine/gnc-datetime.cpp @@ -236,6 +236,7 @@ public: std::string format(const char* format) const; std::string format_zulu(const char* format) const; std::string format_iso8601() const; + static std::string timestamp(); private: LDT m_time; static const TD time_of_day[3]; @@ -466,6 +467,14 @@ GncDateTimeImpl::format_iso8601() const return str.substr(0, 19); } +std::string +GncDateTimeImpl::timestamp() +{ + GncDateTimeImpl gdt; + auto str = boost::posix_time::to_iso_string(gdt.m_time.local_time()); + return str.substr(0, 8) + str.substr(9, 15); +} + /* Member function definitions for GncDateImpl. */ GncDateImpl::GncDateImpl(const std::string str, const std::string fmt) : @@ -600,6 +609,12 @@ GncDateTime::format_iso8601() const return m_impl->format_iso8601(); } +std::string +GncDateTime::timestamp() +{ + return GncDateTimeImpl::timestamp(); +} + /* GncDate */ GncDate::GncDate() : m_impl{new GncDateImpl} {} GncDate::GncDate(int year, int month, int day) : diff --git a/libgnucash/engine/gnc-datetime.hpp b/libgnucash/engine/gnc-datetime.hpp index 8f714f598b..58d1f32fbd 100644 --- a/libgnucash/engine/gnc-datetime.hpp +++ b/libgnucash/engine/gnc-datetime.hpp @@ -152,7 +152,11 @@ public: * @return a std::string in the format YYYY-MM-DD HH:MM:SS. */ std::string format_iso8601() const; - +/** Get an undelimited string representing the current date and time. + * @return a std::string in the format YYYYMMDDHHMMSS. + */ + static std::string timestamp(); + private: std::unique_ptr m_impl; };