Date.cpp 2.3 KB

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