From 5aac5aab2ce8144412f2255d5c0b48cbaa9039d7 Mon Sep 17 00:00:00 2001 From: Chris Frey Date: Sat, 17 Jul 2010 00:08:50 -0400 Subject: [PATCH] lib: added tm_to_iso() to tzwrapper functions --- ChangeLog | 2 ++ src/tzwrapper.cc | 27 +++++++++++++++++++++++++++ src/tzwrapper.h | 9 +++++++++ 3 files changed, 38 insertions(+) diff --git a/ChangeLog b/ChangeLog index 2ba164f3..d24b8d61 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ Release: version 0.17 - 2010/01/?? ------------------------------------------------------------------------------ +2010/07/17 + - lib: added tm_to_iso() to tzwrapper functions 2010/07/16 - lib: added BARRY_GCC_FORMAT_CHECK() for printf() style arg checking - lib: added C-style BarryLogf() function for internal use diff --git a/src/tzwrapper.cc b/src/tzwrapper.cc index b5acef07..18c84c0d 100644 --- a/src/tzwrapper.cc +++ b/src/tzwrapper.cc @@ -79,6 +79,31 @@ struct tm* iso_to_tm(const char *timestamp, return (found >= 6) ? result : 0; } +std::string tm_to_iso(const struct tm *t, bool utc) +{ + char tmp[128]; + + int cc = snprintf(tmp, sizeof(tmp), "%04d%02d%02dT%02d%02d%02d", + t->tm_year + 1900, + t->tm_mon + 1, + t->tm_mday, + t->tm_hour, + t->tm_min, + t->tm_sec + ); + if( cc < 0 || (size_t)cc >= sizeof(tmp) ) + return ""; + + if( utc ) { + if( (size_t)cc >= (sizeof(tmp) - 1) ) + return ""; // not enough room for Z + tmp[cc++] = 'Z'; + tmp[cc] = 0; + } + + return tmp; +} + time_t TzWrapper::iso_mktime(const char *timestamp) { bool utc; @@ -131,6 +156,8 @@ int main() cout << "Fail check: passed" << endl; else cout << "Fail check: ERROR" << endl; + + cout << "t1: " << tm_to_iso(gmtime(&t1), true) << endl; } #endif diff --git a/src/tzwrapper.h b/src/tzwrapper.h index 8a6a5c80..f4825943 100644 --- a/src/tzwrapper.h +++ b/src/tzwrapper.h @@ -29,6 +29,15 @@ BXEXPORT struct tm* iso_to_tm(const char *timestamp, struct tm *result, bool &utc); +/// Turns the struct tm into an ISO timestamp in the format +/// of YYYYMMDDTHHMMSS[Z]. The Z is appended if utc is true. +/// This function assumes that t contains sane values, and will +/// create the target string directly from its content. +/// Returns the ISO timestamp, or empty string on error. +/// If t contains sane values, this function should never fail. +/// Thread-safe. +BXEXPORT std::string tm_to_iso(const struct tm *t, bool utc); + /// utc_mktime() converts a struct tm that contains /// broken down time in utc to a time_t. This function uses /// a brute-force method of conversion that does not require -- 2.11.4.GIT