Implement GncDate::format()

Analog of GncDateTime::format().
This commit is contained in:
John Ralls 2015-05-04 15:03:54 -07:00
parent 2b84dfae8d
commit 269bb510fb
2 changed files with 38 additions and 6 deletions

View File

@ -65,6 +65,7 @@ public:
void today() { m_greg = boost::gregorian::day_clock::local_day(); }
ymd year_month_day() const;
std::string format(const char* format) const;
private:
Date m_greg;
};
@ -76,6 +77,18 @@ GncDateImpl::year_month_day() const
return {boost_ymd.year, boost_ymd.month.as_number(), boost_ymd.day};
}
std::string
GncDateImpl::format(const char* format) const
{
using Facet = boost::gregorian::date_facet;
std::stringstream ss;
//The stream destructor frees the facet, so it must be heap-allocated.
auto output_facet(new Facet(format));
ss.imbue(std::locale(std::locale(), output_facet));
ss << m_greg;
return ss.str();
}
/** Private implementation of GncDateTime. See the documentation for that class.
*/
static LDT
@ -211,6 +224,7 @@ GncDateTimeImpl::format(const char* format) const
}
/* =================== Presentation-class Implementations ====================*/
/* GncDate */
GncDate::GncDate() : m_impl{new GncDateImpl} {}
GncDate::GncDate(int year, int month, int day) :
m_impl(new GncDateImpl(year, month, day)) {}
@ -228,12 +242,20 @@ GncDate::today()
m_impl->today();
}
std::string
GncDate::format(const char* format)
{
return m_impl->format(format);
}
ymd
GncDate::year_month_day() const
{
return m_impl->year_month_day();
}
/* GncDateTime */
GncDateTime::GncDateTime() : m_impl(new GncDateTimeImpl) {}
GncDateTime::GncDateTime(const time64 time) :
m_impl(new GncDateTimeImpl(time)) {}

View File

@ -69,6 +69,15 @@ public:/** Construct a GncDate representing the current day.
@return ymd struct
*/
ymd year_month_day() const;
/** Format the GncDate into a std::string
* @param format: A cstr describing the way the date and time are
* presented. Code letters preceded with % stand in for arguments;
* most are the same as described in strftime(3), but there are a few
* differences. Consult the boost::date_time documentation.
* @return a std::string containing a representation of the date
* according to the format.
*/
std::string format(const char* format);
/** Test that the Date has an implementation. */
bool isnull (void) { return m_impl == nullptr; }
@ -83,7 +92,7 @@ private:
* between 1400 and 9999 CE.
*
* Be careful when using times: A particular time is represented
* differently depending on the timezone, which can shif the displayed
* differently depending on the timezone, which can shift the displayed
* date. Accounting is generally not sensitive to the time of day, but
* is sensitive to the recorded day. Since GncDates are not timezone
* dependent they should be preferred for accounting entries.
@ -142,11 +151,12 @@ public:
/** Test if the GncDateTime has a member pointer. Testing only. */
bool isnull (void) { return m_impl == nullptr; }
/** Format the GncDateTime into a std::string
* @param format: A cstr describing the way the date and time are
* presented. Code letters preceded with % stand in for arguments;
* most are the same as described in strftime(3), but there are a few
* differences. Consult the boost::date_time documentation.
* @return a std::string containing a representation of the date
* according to the format. Consult the boost::date_time
* documentation for format characters; while they mostly compy with
* POSIX there are a few differences.
* according to the format.
*/
std::string format(const char* format) const;