From db5b700c7c116c73283019b3fbf823a23639a405 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 14 Mar 2015 21:20:30 -0500 Subject: Add DateTime::start_of_day() to use instead of the add_hours(-hours()) trick, which doesn't work on days when DST changes. Implementing this requires DateTime objects to keep their own GTimeZone pointer, since the one inside GDateTime is private and can't be used for DateTime::start_of_day()'s call to g_date_time_new(). As a result the public API of DateTime changes, since we'll need a GTimeZone when constructing or assigning from a GDateTime pointer. --- include/datetime/date-time.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'include/datetime') diff --git a/include/datetime/date-time.h b/include/datetime/date-time.h index 4be35f7..6e73ff3 100644 --- a/include/datetime/date-time.h +++ b/include/datetime/date-time.h @@ -38,13 +38,15 @@ public: static DateTime NowLocal(); static DateTime Local(int years, int months, int days, int hours, int minutes, int seconds); + DateTime(); explicit DateTime(time_t t); - explicit DateTime(GDateTime* in=nullptr); - DateTime& operator=(GDateTime* in); + DateTime(GTimeZone* tz, GDateTime* dt); DateTime& operator=(const DateTime& in); DateTime to_timezone(const std::string& zone) const; + DateTime start_of_day() const; + DateTime start_of_minute() const; + DateTime add_days(int days) const; DateTime add_full(int years, int months, int days, int hours, int minutes, double seconds) const; - void reset(GDateTime* in=nullptr); GDateTime* get() const; GDateTime* operator()() const {return get();} @@ -69,6 +71,8 @@ public: bool is_set() const { return m_dt != nullptr; } private: + void reset(GTimeZone*, GDateTime*); + std::shared_ptr m_tz; std::shared_ptr m_dt; }; -- cgit v1.2.3 From 67510bd5a582762648a96d7b42024c4f181ece8e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 16 Mar 2015 18:22:33 +0100 Subject: in DateTime::is_set(), include timezone test --- include/datetime/date-time.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/datetime') diff --git a/include/datetime/date-time.h b/include/datetime/date-time.h index 6e73ff3..8fa1569 100644 --- a/include/datetime/date-time.h +++ b/include/datetime/date-time.h @@ -68,7 +68,7 @@ public: static bool is_same_day(const DateTime& a, const DateTime& b); static bool is_same_minute(const DateTime& a, const DateTime& b); - bool is_set() const { return m_dt != nullptr; } + bool is_set() const { return m_tz && m_dt; } private: void reset(GTimeZone*, GDateTime*); -- cgit v1.2.3 From 273c3b3829c9a3e853d0b6b0a32ae87cc3c6852b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 16 Mar 2015 21:07:54 +0100 Subject: add DateTime::end_of_month(), DateTime::end_of_day(). Add unit tests for them. --- include/datetime/date-time.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'include/datetime') diff --git a/include/datetime/date-time.h b/include/datetime/date-time.h index 8fa1569..7dfc207 100644 --- a/include/datetime/date-time.h +++ b/include/datetime/date-time.h @@ -36,17 +36,21 @@ class DateTime { public: static DateTime NowLocal(); - static DateTime Local(int years, int months, int days, int hours, int minutes, int seconds); + static DateTime Local(int year, int month, int day, int hour, int minute, double seconds); DateTime(); explicit DateTime(time_t t); DateTime(GTimeZone* tz, GDateTime* dt); + DateTime(GTimeZone* tz, int year, int month, int day, int hour, int minute, double seconds); DateTime& operator=(const DateTime& in); DateTime to_timezone(const std::string& zone) const; + DateTime start_of_month() const; DateTime start_of_day() const; DateTime start_of_minute() const; + DateTime end_of_day() const; + DateTime end_of_month() const; DateTime add_days(int days) const; - DateTime add_full(int years, int months, int days, int hours, int minutes, double seconds) const; + DateTime add_full(int year, int month, int day, int hour, int minute, double seconds) const; GDateTime* get() const; GDateTime* operator()() const {return get();} -- cgit v1.2.3