From f161aea8ce96c20f888cd65a78e178e5f5552dc2 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sun, 11 Oct 2009 05:19:01 -0400 Subject: Removed reliance on strptime/strftime The code now uses Boost's input and output facets for times and dates. This ensures completely consistency regarding timezones and times, and fixes the regression test that was broken while I was away coding in London (where it was GMT-0 and I didn't notice the difference between local and GMT). --- src/times.h | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) (limited to 'src/times.h') diff --git a/src/times.h b/src/times.h index 1ff98325..247c9393 100644 --- a/src/times.h +++ b/src/times.h @@ -81,39 +81,34 @@ string_to_day_of_week(const std::string& str); optional string_to_month_of_year(const std::string& str); -datetime_t parse_datetime(const char * str, int current_year = -1); +datetime_t parse_datetime(const char * str, + optional current_year = none); inline datetime_t parse_datetime(const std::string& str, - int current_year = -1) { + optional current_year = none) { return parse_datetime(str.c_str(), current_year); } -date_t parse_date(const char * str, int current_year = -1); +date_t parse_date(const char * str, + optional current_year = none); -inline date_t parse_date(const std::string& str, int current_year = -1) { +inline date_t parse_date(const std::string& str, + optional current_year = none) { return parse_date(str.c_str(), current_year); } -inline std::time_t to_time_t(const ptime& t) -{ - if( t == posix_time::neg_infin ) - return 0; - else if( t == posix_time::pos_infin ) - return LONG_MAX; - ptime start(date(1970,1,1)); - return (t-start).total_seconds(); -} - extern std::string output_datetime_format; inline std::string format_datetime(const datetime_t& when, const optional& format = none) { - char buf[256]; - std::time_t moment = to_time_t(when); - std::strftime(buf, 255, format ? format->c_str() : - output_datetime_format.c_str(), std::localtime(&moment)); - return buf; + posix_time::time_facet * facet + (new posix_time::time_facet(format ? format->c_str() : + output_datetime_format.c_str())); + std::ostringstream buf; + buf.imbue(std::locale(std::locale::classic(), facet)); + buf << when; + return buf.str(); } extern std::string output_date_format; @@ -121,11 +116,13 @@ extern std::string output_date_format; inline std::string format_date(const date_t& when, const optional& format = none) { - char buf[256]; - std::tm moment = gregorian::to_tm(when); - std::strftime(buf, 255, format ? format->c_str() : - output_date_format.c_str(), &moment); - return buf; + gregorian::date_facet * facet + (new gregorian::date_facet(format ? format->c_str() : + output_date_format.c_str())); + std::ostringstream buf; + buf.imbue(std::locale(std::locale::classic(), facet)); + buf << when; + return buf.str(); } class date_interval_t : public equality_comparable -- cgit v1.2.3