From 9143d8b8e7b43540b63ef6259a9fb1ed555a4b32 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 1 Nov 2015 17:20:48 -0500 Subject: [PATCH] ISO8601: Covnerted from sprintf to std::stringstream --- src/ISO8601.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/ISO8601.cpp b/src/ISO8601.cpp index 5da6ed678..b97261bcf 100644 --- a/src/ISO8601.cpp +++ b/src/ISO8601.cpp @@ -1900,19 +1900,18 @@ const std::string ISO8601p::format () const //////////////////////////////////////////////////////////////////////////////// const std::string ISO8601p::formatVague () const { - char formatted[24]; float days = (float) _period / 86400.0; - if (_period >= 86400 * 365) sprintf (formatted, "%.1fy", (days / 365.0)); - else if (_period >= 86400 * 84) sprintf (formatted, "%1dmo", (int) (days / 30)); - else if (_period >= 86400 * 13) sprintf (formatted, "%dw", (int) (float) (days / 7.0)); - else if (_period >= 86400) sprintf (formatted, "%dd", (int) days); - else if (_period >= 3600) sprintf (formatted, "%dh", (int) (_period / 3600)); - else if (_period >= 60) sprintf (formatted, "%dmin", (int) (_period / 60)); - else if (_period >= 1) sprintf (formatted, "%ds", (int) _period); - else formatted[0] = '\0'; + std::stringstream formatted; + if (_period >= 86400 * 365) formatted << std::fixed << std::setprecision (1) << (days / 365) << "y"; + else if (_period >= 86400 * 84) formatted << static_cast (days / 30) << "mo"; + else if (_period >= 86400 * 13) formatted << static_cast (days / 7) << "w"; + else if (_period >= 86400) formatted << static_cast (days) << "d"; + else if (_period >= 3600) formatted << static_cast (_period / 3600) << "h"; + else if (_period >= 60) formatted << static_cast (_period / 60) << "min"; + else if (_period >= 1) formatted << static_cast (_period) << "s"; - return std::string (formatted); + return formatted.str (); } ////////////////////////////////////////////////////////////////////////////////