Date.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-04-23
  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->timestamp += (_value * 86400);
  20. return *this;
  21. }
  22. ls_std::Date &ls_std::Date::operator-(int _value)
  23. {
  24. this->timestamp -= (_value * 86400);
  25. return *this;
  26. }
  27. bool ls_std::Date::after(const ls_std::Date &_foreignDate) const
  28. {
  29. return this->timestamp > _foreignDate.getTime();
  30. }
  31. bool ls_std::Date::before(const ls_std::Date &_foreignDate) const
  32. {
  33. return this->timestamp < _foreignDate.getTime();
  34. }
  35. int ls_std::Date::getDay()
  36. {
  37. return this->localTime->tm_mday;
  38. }
  39. int ls_std::Date::getHour()
  40. {
  41. return this->localTime->tm_hour;
  42. }
  43. int ls_std::Date::getMinute()
  44. {
  45. return this->localTime->tm_min;
  46. }
  47. int ls_std::Date::getMonth()
  48. {
  49. return this->localTime->tm_mon + 1;
  50. }
  51. int ls_std::Date::getSecond()
  52. {
  53. return this->localTime->tm_sec;
  54. }
  55. int ls_std::Date::getYear()
  56. {
  57. return this->localTime->tm_year + 1900;
  58. }
  59. time_t ls_std::Date::getTime() const
  60. {
  61. return this->timestamp;
  62. }
  63. void ls_std::Date::setTime(time_t _timestamp)
  64. {
  65. this->timestamp = _timestamp;
  66. this->_init();
  67. }
  68. std::string ls_std::Date::toString()
  69. {
  70. std::stringstream _stream{};
  71. _stream << std::put_time(this->localTime, "%Y-%m-%d %H:%M:%S");
  72. return _stream.str();
  73. }
  74. void ls_std::Date::_init()
  75. {
  76. this->localTime = std::localtime(&this->timestamp);
  77. }