Date.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2020-08-21
  7. *
  8. * */
  9. #include <iomanip>
  10. #include <sstream>
  11. #include "Date.hpp"
  12. ls_std::Date::Date() : Class("Date")
  13. {
  14. this->timestamp = std::time(nullptr);
  15. this->_init();
  16. }
  17. ls_std::Date & ls_std::Date::operator+(int _value) {
  18. this->timestamp += (_value * 86400);
  19. return *this;
  20. }
  21. ls_std::Date & ls_std::Date::operator-(int _value) {
  22. this->timestamp -= (_value * 86400);
  23. return *this;
  24. }
  25. bool ls_std::Date::after(const Date& _foreignDate) const {
  26. return this->timestamp > _foreignDate.getTime();
  27. }
  28. bool ls_std::Date::before(const Date& _foreignDate) const {
  29. return this->timestamp < _foreignDate.getTime();
  30. }
  31. int ls_std::Date::getDay() {
  32. return this->localTime->tm_mday;
  33. }
  34. int ls_std::Date::getHour() {
  35. return this->localTime->tm_hour;
  36. }
  37. int ls_std::Date::getMinute() {
  38. return this->localTime->tm_min;
  39. }
  40. int ls_std::Date::getMonth() {
  41. return this->localTime->tm_mon + 1;
  42. }
  43. int ls_std::Date::getSecond() {
  44. return this->localTime->tm_sec;
  45. }
  46. int ls_std::Date::getYear() {
  47. return this->localTime->tm_year + 1900;
  48. }
  49. time_t ls_std::Date::getTime() const {
  50. return this->timestamp;
  51. }
  52. void ls_std::Date::setTime(time_t _timestamp) {
  53. this->timestamp = _timestamp;
  54. this->_init();
  55. }
  56. std::string ls_std::Date::toString() {
  57. std::stringstream _stream {};
  58. _stream << std::put_time(this->localTime, "%Y-%m-%d %H:%M:%S");
  59. return _stream.str();
  60. }
  61. void ls_std::Date::_init() {
  62. this->localTime = std::localtime(&this->timestamp);
  63. }