Browse Source

Add plus equals and minus equals operator to Date class

- add operators to Date class
- add tests for Date class
Patrick-Christopher Mattulat 3 years ago
parent
commit
c9715aa3d9
3 changed files with 70 additions and 4 deletions
  1. 5 1
      include/ls_std/time/Date.hpp
  2. 25 3
      source/ls_std/time/Date.cpp
  3. 40 0
      test/cases/time/DateTest.cpp

+ 5 - 1
include/ls_std/time/Date.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2021-05-01
+ * Changed:         2021-05-22
  *
  * */
 
@@ -26,6 +26,8 @@ namespace ls_std
 
       ls_std::Date &operator+(int _value);
       ls_std::Date &operator-(int _value);
+      ls_std::Date &operator+=(int _value);
+      ls_std::Date &operator-=(int _value);
 
       // additional functionality
 
@@ -46,6 +48,8 @@ namespace ls_std
       time_t timestamp{};
       tm *localTime{};
 
+      void _decrementByDays(int _value);
+      void _incrementByDays(int _value);
       void _init();
   };
 }

+ 25 - 3
source/ls_std/time/Date.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2021-04-23
+ * Changed:         2021-05-22
  *
  * */
 
@@ -19,13 +19,25 @@ ls_std::Date::Date() : ls_std::Class("Date")
 
 ls_std::Date &ls_std::Date::operator+(int _value)
 {
-  this->timestamp += (_value * 86400);
+  this->_incrementByDays(_value);
   return *this;
 }
 
 ls_std::Date &ls_std::Date::operator-(int _value)
 {
-  this->timestamp -= (_value * 86400);
+  this->_decrementByDays(_value);
+  return *this;
+}
+
+ls_std::Date &ls_std::Date::operator+=(int _value)
+{
+  this->_incrementByDays(_value);
+  return *this;
+}
+
+ls_std::Date &ls_std::Date::operator-=(int _value)
+{
+  this->_decrementByDays(_value);
   return *this;
 }
 
@@ -88,6 +100,16 @@ std::string ls_std::Date::toString()
   return _stream.str();
 }
 
+void ls_std::Date::_decrementByDays(int _value)
+{
+  this->timestamp -= (_value * 86400);
+}
+
+void ls_std::Date::_incrementByDays(int _value)
+{
+  this->timestamp += (_value * 86400);
+}
+
 void ls_std::Date::_init()
 {
   this->localTime = std::localtime(&this->timestamp);

+ 40 - 0
test/cases/time/DateTest.cpp

@@ -65,6 +65,46 @@ namespace
     ASSERT_EQ(timestamp + 86400, date.getTime());
   }
 
+  TEST_F(DateTest, operator_plus_equals)
+  {
+    ls_std::Date date{};
+    time_t timestamp = date.getTime();
+
+    date += 2;
+    time_t expectedTimestamp = timestamp + 86400 * 2;
+    ASSERT_EQ(expectedTimestamp, date.getTime());
+  }
+
+  TEST_F(DateTest, operator_plus_equals_with_negative_value)
+  {
+    ls_std::Date date{};
+    time_t timestamp = date.getTime();
+
+    date += -2;
+    time_t expectedTimestamp = timestamp - 86400 * 2;
+    ASSERT_EQ(expectedTimestamp, date.getTime());
+  }
+
+  TEST_F(DateTest, operator_minus_equals)
+  {
+    ls_std::Date date{};
+    time_t timestamp = date.getTime();
+
+    date -= 2;
+    time_t expectedTimestamp = timestamp - 86400 * 2;
+    ASSERT_EQ(expectedTimestamp, date.getTime());
+  }
+
+  TEST_F(DateTest, operator_minus_equals_with_negative_value)
+  {
+    ls_std::Date date{};
+    time_t timestamp = date.getTime();
+
+    date -= -2;
+    time_t expectedTimestamp = timestamp + 86400 * 2;
+    ASSERT_EQ(expectedTimestamp, date.getTime());
+  }
+
   // additional functionality
 
   TEST_F(DateTest, after)