Date.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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-03
  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 &ls::std::time::Date::operator+(int _value)
  20. {
  21. this->_incrementByDays(_value);
  22. return *this;
  23. }
  24. ls::std::time::Date &ls::std::time::Date::operator-(int _value)
  25. {
  26. this->_decrementByDays(_value);
  27. return *this;
  28. }
  29. ls::std::time::Date &ls::std::time::Date::operator+=(int _value)
  30. {
  31. this->_incrementByDays(_value);
  32. return *this;
  33. }
  34. ls::std::time::Date &ls::std::time::Date::operator-=(int _value)
  35. {
  36. this->_decrementByDays(_value);
  37. return *this;
  38. }
  39. bool ls::std::time::Date::after(const ls::std::time::Date &_foreignDate) const
  40. {
  41. return this->timestamp > _foreignDate.getTime();
  42. }
  43. bool ls::std::time::Date::before(const ls::std::time::Date &_foreignDate) const
  44. {
  45. return this->timestamp < _foreignDate.getTime();
  46. }
  47. int ls::std::time::Date::getDay()
  48. {
  49. return this->localTime->tm_mday;
  50. }
  51. int ls::std::time::Date::getHour()
  52. {
  53. return this->localTime->tm_hour;
  54. }
  55. int ls::std::time::Date::getMinute()
  56. {
  57. return this->localTime->tm_min;
  58. }
  59. int ls::std::time::Date::getMonth()
  60. {
  61. return this->localTime->tm_mon + 1;
  62. }
  63. int ls::std::time::Date::getSecond()
  64. {
  65. return this->localTime->tm_sec;
  66. }
  67. int ls::std::time::Date::getYear()
  68. {
  69. return this->localTime->tm_year + 1900;
  70. }
  71. time_t ls::std::time::Date::getTime() const
  72. {
  73. return this->timestamp;
  74. }
  75. void ls::std::time::Date::setTime(time_t _timestamp)
  76. {
  77. this->timestamp = _timestamp;
  78. this->_init();
  79. }
  80. ::std::string ls::std::time::Date::toString()
  81. {
  82. ::std::stringstream _stream{};
  83. _stream << ::std::put_time(this->localTime, "%Y-%m-%d %H:%M:%S");
  84. return _stream.str();
  85. }
  86. void ls::std::time::Date::_decrementByDays(int _value)
  87. {
  88. this->timestamp -= (_value * 86400);
  89. }
  90. void ls::std::time::Date::_incrementByDays(int _value)
  91. {
  92. this->timestamp += (_value * 86400);
  93. }
  94. void ls::std::time::Date::_init()
  95. {
  96. this->localTime = ::std::localtime(&this->timestamp);
  97. }