diff --git a/include/private/soci-mktime.h b/include/private/soci-mktime.h index 485958fd8..be72286a9 100644 --- a/include/private/soci-mktime.h +++ b/include/private/soci-mktime.h @@ -62,6 +62,17 @@ mktime_from_ymdhms(tm& t, // Throws if the string in buf couldn't be parsed as a date or a time string. SOCI_DECL void parse_std_tm(char const *buf, std::tm &t); +// Reverse function for formatting datetime values. +// +// The string returned in the buffer uses YYYY-MM-DD HH:MM:SS format. +inline int format_std_tm(std::tm const& t, char* buf, std::size_t len) +{ + return snprintf(buf, len, + "%d-%02d-%02d %02d:%02d:%02d", + t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, + t.tm_hour, t.tm_min, t.tm_sec); +} + } // namespace details } // namespace soci diff --git a/src/backends/mysql/standard-use-type.cpp b/src/backends/mysql/standard-use-type.cpp index c0b4c17eb..56684fc34 100644 --- a/src/backends/mysql/standard-use-type.cpp +++ b/src/backends/mysql/standard-use-type.cpp @@ -12,6 +12,7 @@ #include "soci/soci-platform.h" #include "soci-dtocstr.h" #include "soci-exchange-cast.h" +#include "soci-mktime.h" #include "soci/blob.h" // std #include @@ -153,11 +154,12 @@ void mysql_standard_use_type_backend::pre_use(indicator const *ind) std::size_t const bufSize = 80; buf_ = new char[bufSize]; - std::tm const& t = exchange_type_cast(data_); - snprintf(buf_, bufSize, - "\'%d-%02d-%02d %02d:%02d:%02d\'", - t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, - t.tm_hour, t.tm_min, t.tm_sec); + int n = 0; + buf_[n++] = '\''; + n += format_std_tm(exchange_type_cast(data_), + buf_ + n, bufSize - n - 1); + buf_[n++] = '\''; + buf_[n] = '\0'; } break; case x_blob: diff --git a/src/backends/mysql/vector-use-type.cpp b/src/backends/mysql/vector-use-type.cpp index 456f2a9b2..7f1b8e257 100644 --- a/src/backends/mysql/vector-use-type.cpp +++ b/src/backends/mysql/vector-use-type.cpp @@ -11,6 +11,7 @@ #include "common.h" #include "soci/soci-platform.h" #include "soci-dtocstr.h" +#include "soci-mktime.h" // std #include #include @@ -207,9 +208,11 @@ void mysql_vector_use_type_backend::pre_use(indicator const *ind) std::size_t const bufSize = 80; buf = new char[bufSize]; - snprintf(buf, bufSize, "\'%d-%02d-%02d %02d:%02d:%02d\'", - v[i].tm_year + 1900, v[i].tm_mon + 1, v[i].tm_mday, - v[i].tm_hour, v[i].tm_min, v[i].tm_sec); + int n = 0; + buf[n++] = '\''; + n += format_std_tm(v[i], buf + n, bufSize - n - 1); + buf[n++] = '\''; + buf[n] = '\0'; } break; diff --git a/src/backends/oracle/standard-use-type.cpp b/src/backends/oracle/standard-use-type.cpp index 910a0b4a1..017a46dbe 100644 --- a/src/backends/oracle/standard-use-type.cpp +++ b/src/backends/oracle/standard-use-type.cpp @@ -29,7 +29,6 @@ #ifdef _MSC_VER #pragma warning(disable:4355) -#define snprintf _snprintf #endif using namespace soci; diff --git a/src/backends/oracle/vector-use-type.cpp b/src/backends/oracle/vector-use-type.cpp index 11fda2e11..dc182382f 100644 --- a/src/backends/oracle/vector-use-type.cpp +++ b/src/backends/oracle/vector-use-type.cpp @@ -20,7 +20,6 @@ #ifdef _MSC_VER #pragma warning(disable:4355) -#define snprintf _snprintf #endif using namespace soci; diff --git a/src/backends/postgresql/standard-use-type.cpp b/src/backends/postgresql/standard-use-type.cpp index e6e7ae83e..9aad9004d 100644 --- a/src/backends/postgresql/standard-use-type.cpp +++ b/src/backends/postgresql/standard-use-type.cpp @@ -13,6 +13,7 @@ #include "soci/soci-platform.h" #include "soci-dtocstr.h" #include "soci-exchange-cast.h" +#include "soci-mktime.h" #include // libpq #include #include @@ -142,10 +143,7 @@ void postgresql_standard_use_type_backend::pre_use(indicator const * ind) std::size_t const bufSize = 80; buf_ = new char[bufSize]; - std::tm const& t = exchange_type_cast(data_); - snprintf(buf_, bufSize, "%d-%02d-%02d %02d:%02d:%02d", - t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, - t.tm_hour, t.tm_min, t.tm_sec); + format_std_tm(exchange_type_cast(data_), buf_, bufSize); } break; case x_rowid: diff --git a/src/backends/postgresql/vector-use-type.cpp b/src/backends/postgresql/vector-use-type.cpp index 332b7ebab..716f6e144 100644 --- a/src/backends/postgresql/vector-use-type.cpp +++ b/src/backends/postgresql/vector-use-type.cpp @@ -9,6 +9,7 @@ #include "soci/soci-platform.h" #include "soci/postgresql/soci-postgresql.h" #include "soci-dtocstr.h" +#include "soci-mktime.h" #include "common.h" #include "soci/type-wrappers.h" #include // libpq @@ -218,9 +219,7 @@ void postgresql_vector_use_type_backend::pre_use(indicator const * ind) std::size_t const bufSize = 80; buf = new char[bufSize]; - snprintf(buf, bufSize, "%d-%02d-%02d %02d:%02d:%02d", - v[i].tm_year + 1900, v[i].tm_mon + 1, v[i].tm_mday, - v[i].tm_hour, v[i].tm_min, v[i].tm_sec); + format_std_tm(v[i], buf, bufSize); } break; case x_xmltype: diff --git a/src/backends/sqlite3/standard-use-type.cpp b/src/backends/sqlite3/standard-use-type.cpp index 7b39f4272..bf301c71d 100644 --- a/src/backends/sqlite3/standard-use-type.cpp +++ b/src/backends/sqlite3/standard-use-type.cpp @@ -12,6 +12,7 @@ #include "soci/blob.h" #include "soci-dtocstr.h" #include "soci-exchange-cast.h" +#include "soci-mktime.h" // std #include #include @@ -176,12 +177,7 @@ void sqlite3_standard_use_type_backend::pre_use(indicator const * ind) std::tm &t = exchange_type_cast(data_); col.buffer_.data_ = new char[bufSize]; - col.buffer_.size_ - = snprintf( - col.buffer_.data_, bufSize, "%d-%02d-%02d %02d:%02d:%02d", - t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, - t.tm_hour, t.tm_min, t.tm_sec - ); + col.buffer_.size_ = format_std_tm(t, col.buffer_.data_, bufSize); break; } diff --git a/src/backends/sqlite3/vector-use-type.cpp b/src/backends/sqlite3/vector-use-type.cpp index 71c2ea624..5a983a254 100644 --- a/src/backends/sqlite3/vector-use-type.cpp +++ b/src/backends/sqlite3/vector-use-type.cpp @@ -10,6 +10,7 @@ #include "soci/soci-platform.h" #include "soci/sqlite3/soci-sqlite3.h" #include "soci-dtocstr.h" +#include "soci-mktime.h" #include "common.h" // std #include @@ -176,11 +177,7 @@ void sqlite3_vector_use_type_backend::pre_use(indicator const * ind) col.type_ = dt_date; col.dataType_ = db_date; col.buffer_.data_ = new char[bufSize]; - col.buffer_.size_ - = snprintf(col.buffer_.data_, bufSize, "%d-%02d-%02d %02d:%02d:%02d", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec - ); + col.buffer_.size_ = format_std_tm(tm, col.buffer_.data_, bufSize); break; } diff --git a/src/core/use-type.cpp b/src/core/use-type.cpp index 26d165dc5..f17fcd91e 100644 --- a/src/core/use-type.cpp +++ b/src/core/use-type.cpp @@ -10,6 +10,7 @@ #include "soci/use-type.h" #include "soci/statement.h" #include "soci-exchange-cast.h" +#include "soci-mktime.h" #include @@ -98,9 +99,7 @@ void standard_use_type::dump_value(std::ostream& os) const std::tm const& t = exchange_type_cast(data_); char buf[80]; - snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d", - t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, - t.tm_hour, t.tm_min, t.tm_sec); + format_std_tm(t, buf, sizeof(buf)); os << buf; }