From 64e72c622be986c7272e938c2d3235c61abef402 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 23 Dec 2019 01:05:49 +0100 Subject: Add struct tm constructor for LocalTime, use it to tidy up day_{greater,less}_than_or_equal and add add_months() method. --- src/local_time.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/local_time.cc') diff --git a/src/local_time.cc b/src/local_time.cc index 5030500e..2aa55183 100644 --- a/src/local_time.cc +++ b/src/local_time.cc @@ -52,7 +52,17 @@ LocalTime::LocalTime () { time_t now = time (0); struct tm* tm = localtime (&now); + set (tm); +} + +LocalTime::LocalTime (struct tm t) +{ + set (&t); +} +void +LocalTime::set (struct tm const * tm) +{ _year = tm->tm_year + 1900; _month = tm->tm_mon + 1; _day = tm->tm_mday; @@ -208,6 +218,22 @@ LocalTime::time_of_day (bool with_second, bool with_millisecond) const return buffer; } +void +LocalTime::add_months (int m) +{ + _month += m; + + while (_month < 0) { + _month += 12; + _year--; + } + + while (_month > 11) { + _month -= 12; + _year++; + } +} + bool LocalTime::operator== (LocalTime const & other) const { -- cgit v1.2.3