diff options
author | John Wiegley <johnw@newartisans.com> | 2009-10-11 05:19:01 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-10-11 05:19:01 -0400 |
commit | f161aea8ce96c20f888cd65a78e178e5f5552dc2 (patch) | |
tree | b8f9279c611fe08f1b10bb885144f7c275c7561d /src/times.h | |
parent | 1a8e835bfe2cb18cd10691d7a4ab0fc478c4aced (diff) | |
download | fork-ledger-f161aea8ce96c20f888cd65a78e178e5f5552dc2.tar.gz fork-ledger-f161aea8ce96c20f888cd65a78e178e5f5552dc2.tar.bz2 fork-ledger-f161aea8ce96c20f888cd65a78e178e5f5552dc2.zip |
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).
Diffstat (limited to 'src/times.h')
-rw-r--r-- | src/times.h | 45 |
1 files changed, 21 insertions, 24 deletions
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<date_time::months_of_year> 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<date_t::year_type> current_year = none); inline datetime_t parse_datetime(const std::string& str, - int current_year = -1) { + optional<date_t::year_type> 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<date_t::year_type> 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<date_t::year_type> 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<std::string>& 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<std::string>& 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<date_interval_t> |