Date.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-05-22
  7. *
  8. * */
  9. #include <iomanip>
  10. #include <sstream>
  11. #include <ls_std/time/Date.hpp>
  12. ls_std::Date::Date() : ls_std::Class("Date")
  13. {
  14. this->timestamp = std::time(nullptr);
  15. this->_init();
  16. }
  17. ls_std::Date &ls_std::Date::operator+(int _value)
  18. {
  19. this->_incrementByDays(_value);
  20. return *this;
  21. }
  22. ls_std::Date &ls_std::Date::operator-(int _value)
  23. {
  24. this->_decrementByDays(_value);
  25. return *this;
  26. }
  27. ls_std::Date &ls_std::Date::operator+=(int _value)
  28. {
  29. this->_incrementByDays(_value);
  30. return *this;
  31. }
  32. ls_std::Date &ls_std::Date::operator-=(int _value)
  33. {
  34. this->_decrementByDays(_value);
  35. return *this;
  36. }
  37. bool ls_std::Date::after(const ls_std::Date &_foreignDate) const
  38. {
  39. return this->timestamp > _foreignDate.getTime();
  40. }
  41. bool ls_std::Date::before(const ls_std::Date &_foreignDate) const
  42. {
  43. return this->timestamp < _foreignDate.getTime();
  44. }
  45. int ls_std::Date::getDay()
  46. {
  47. return this->localTime->tm_mday;
  48. }
  49. int ls_std::Date::getHour()
  50. {
  51. return this->localTime->tm_hour;
  52. }
  53. int ls_std::Date::getMinute()
  54. {
  55. return this->localTime->tm_min;
  56. }
  57. int ls_std::Date::getMonth()
  58. {
  59. return this->localTime->tm_mon + 1;
  60. }
  61. int ls_std::Date::getSecond()
  62. {
  63. return this->localTime->tm_sec;
  64. }
  65. int ls_std::Date::getYear()
  66. {
  67. return this->localTime->tm_year + 1900;
  68. }
  69. time_t ls_std::Date::getTime() const
  70. {
  71. return this->timestamp;
  72. }
  73. void ls_std::Date::setTime(time_t _timestamp)
  74. {
  75. this->timestamp = _timestamp;
  76. this->_init();
  77. }
  78. std::string ls_std::Date::toString()
  79. {
  80. std::stringstream _stream{};
  81. _stream << std::put_time(this->localTime, "%Y-%m-%d %H:%M:%S");
  82. return _stream.str();
  83. }
  84. void ls_std::Date::_decrementByDays(int _value)
  85. {
  86. this->timestamp -= (_value * 86400);
  87. }
  88. void ls_std::Date::_incrementByDays(int _value)
  89. {
  90. this->timestamp += (_value * 86400);
  91. }
  92. void ls_std::Date::_init()
  93. {
  94. this->localTime = std::localtime(&this->timestamp);
  95. }