Date.cpp 2.2 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: 2022-05-09
  7. *
  8. * */
  9. #include <iomanip>
  10. #include <sstream>
  11. #include <ls_std/time/Date.hpp>
  12. ls::std::time::Date::Date() : ls::std::core::Class("Date")
  13. {
  14. this->timestamp = ::std::time(nullptr);
  15. this->_init();
  16. }
  17. ls::std::time::Date &ls::std::time::Date::operator+(int _value)
  18. {
  19. this->_incrementByDays(_value);
  20. return *this;
  21. }
  22. ls::std::time::Date &ls::std::time::Date::operator-(int _value)
  23. {
  24. this->_decrementByDays(_value);
  25. return *this;
  26. }
  27. ls::std::time::Date &ls::std::time::Date::operator+=(int _value)
  28. {
  29. this->_incrementByDays(_value);
  30. return *this;
  31. }
  32. ls::std::time::Date &ls::std::time::Date::operator-=(int _value)
  33. {
  34. this->_decrementByDays(_value);
  35. return *this;
  36. }
  37. bool ls::std::time::Date::after(const ls::std::time::Date &_foreignDate) const
  38. {
  39. return this->timestamp > _foreignDate.getTime();
  40. }
  41. bool ls::std::time::Date::before(const ls::std::time::Date &_foreignDate) const
  42. {
  43. return this->timestamp < _foreignDate.getTime();
  44. }
  45. int ls::std::time::Date::getDay()
  46. {
  47. return this->localTime->tm_mday;
  48. }
  49. int ls::std::time::Date::getHour()
  50. {
  51. return this->localTime->tm_hour;
  52. }
  53. int ls::std::time::Date::getMinute()
  54. {
  55. return this->localTime->tm_min;
  56. }
  57. int ls::std::time::Date::getMonth()
  58. {
  59. return this->localTime->tm_mon + 1;
  60. }
  61. int ls::std::time::Date::getSecond()
  62. {
  63. return this->localTime->tm_sec;
  64. }
  65. int ls::std::time::Date::getYear()
  66. {
  67. return this->localTime->tm_year + 1900;
  68. }
  69. time_t ls::std::time::Date::getTime() const
  70. {
  71. return this->timestamp;
  72. }
  73. void ls::std::time::Date::setTime(time_t _timestamp)
  74. {
  75. this->timestamp = _timestamp;
  76. this->_init();
  77. }
  78. std::string ls::std::time::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::time::Date::_decrementByDays(int _value)
  85. {
  86. this->timestamp -= (_value * 86400);
  87. }
  88. void ls::std::time::Date::_incrementByDays(int _value)
  89. {
  90. this->timestamp += (_value * 86400);
  91. }
  92. void ls::std::time::Date::_init()
  93. {
  94. this->localTime = ::std::localtime(&this->timestamp);
  95. }