Default constructors return the epoch.

GncDate::today() and GncDateTime::now() get the current date and time.

Suggested in a C++Now talk by Jeff Garland, the author of boost::date_time.
Reasoning is that getting time from the system clock is expensive and so
shouldn't be done unless needed.
This commit is contained in:
John Ralls
2015-04-07 15:36:40 -07:00
parent 01f5a9c04c
commit 10daa27abc
2 changed files with 41 additions and 14 deletions

View File

@@ -45,30 +45,30 @@ static const TimeZoneProvider tzp;
static const PTime unix_epoch (Date(1970, boost::gregorian::Jan, 1),
boost::posix_time::seconds(0));
/* To ensure things aren't overly screwed up by setting the nanosecond clock for boost::date_time. Don't do it, though, it doesn't get us anything and slows down the date/time library. */
#ifndef BOOST_DATE_TIME_HAS_NANOSECONDS
static constexpr auto ticks_per_second = INT64_C(1000000);
#else
static constexpr auto ticks_per_second = INT64_C(1000000000);
#endif
/** Private implementation of GncDate. See the documentation for that class.
*/
class GncDateImpl
{
public:
GncDateImpl(): m_greg(boost::gregorian::day_clock::local_day()) {}
GncDateImpl(): m_greg(unix_epoch.date()) {}
GncDateImpl(const int year, const int month, const int day) :
m_greg(year, static_cast<Month>(month), day) {}
GncDateImpl(Date d) : m_greg(d) {}
void today() { m_greg = boost::gregorian::day_clock::local_day(); }
private:
Date m_greg;
};
/** Private implementation of GncDateTime. See the documentation for that class.
*/
class GncDateTimeImpl
{
public:
GncDateTimeImpl() : m_time(boost::local_time::local_sec_clock::local_time(tzp.get(boost::gregorian::day_clock::local_day().year()))) {}
GncDateTimeImpl(const time64 time);
private:
LDT m_time;
};
static LDT
LDT_from_unix_local(const time64 time)
{
@@ -79,14 +79,39 @@ LDT_from_unix_local(const time64 time)
return LDT(temp, tz);
}
GncDateTimeImpl::GncDateTimeImpl(const time64 time) :
m_time(LDT_from_unix_local(time)) {}
class GncDateTimeImpl
{
public:
GncDateTimeImpl() : m_time(unix_epoch, tzp.get(unix_epoch.date().year())) {}
GncDateTimeImpl(const time64 time) : m_time(LDT_from_unix_local(time)) {}
GncDateTimeImpl(PTime&& pt) : m_time(pt, tzp.get(pt.date().year())) {}
GncDateTimeImpl(LDT&& ldt) : m_time(ldt) {}
void now() { m_time = boost::local_time::local_sec_clock::local_time(tzp.get(boost::gregorian::day_clock::local_day().year())); }
private:
LDT m_time;
};
}
/* =================== Presentation-class Implementations ====================*/
GncDate::GncDate() : m_impl{new GncDateImpl} {}
GncDate::GncDate(int year, int month, int day) :
m_impl(new GncDateImpl(year, month, day)) {}
GncDate::~GncDate() = default;
void
GncDate::today()
{
m_impl->today();
}
GncDateTime::GncDateTime() : m_impl(new GncDateTimeImpl) {}
GncDateTime::GncDateTime(time64 time) : m_impl(new GncDateTimeImpl(time)) {}
GncDateTime::~GncDateTime() = default;
void
GncDateTime::now()
{
m_impl->now();
}
}

View File

@@ -53,7 +53,9 @@ public:
*/
GncDate(int year, int month, int day);
~GncDate();
/** Set the date object to the computer clock's current day. */
void today();
/** Test that the Date has an implementation. */
bool isnull (void) { return m_impl == nullptr; }
private:
@@ -87,7 +89,7 @@ public:
*/
GncDateTime(time64 time);
~GncDateTime();
void now();
bool isnull (void) { return m_impl == nullptr; }
private: