Date.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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-16
  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. bool ls_std::Date::after(const Date& _foreignDate) const {
  22. return this->timestamp > _foreignDate.getTime();
  23. }
  24. bool ls_std::Date::before(const Date& _foreignDate) const {
  25. return this->timestamp < _foreignDate.getTime();
  26. }
  27. int ls_std::Date::getDay() {
  28. return this->localTime->tm_mday;
  29. }
  30. int ls_std::Date::getHour() {
  31. return this->localTime->tm_hour;
  32. }
  33. int ls_std::Date::getMinute() {
  34. return this->localTime->tm_min;
  35. }
  36. int ls_std::Date::getMonth() {
  37. return this->localTime->tm_mon;
  38. }
  39. int ls_std::Date::getSecond() {
  40. return this->localTime->tm_sec;
  41. }
  42. int ls_std::Date::getYear() {
  43. return this->localTime->tm_year;
  44. }
  45. time_t ls_std::Date::getTime() const {
  46. return this->timestamp;
  47. }
  48. void ls_std::Date::setTime(time_t _timestamp) {
  49. this->timestamp = _timestamp;
  50. this->_init();
  51. }
  52. std::string ls_std::Date::toString() {
  53. std::stringstream _stream {};
  54. _stream << std::put_time(this->localTime, "%Y-%m-%d %H:%M:%S");
  55. return _stream.str();
  56. }
  57. void ls_std::Date::_init() {
  58. this->localTime = std::localtime(&this->timestamp);
  59. this->localTime->tm_mon += 1;
  60. this->localTime->tm_year += 1900;
  61. }