Date.cpp 2.2 KB

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