date.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include <util/stream/output.h>
  3. #include <util/stream/input.h>
  4. #include <util/generic/string.h>
  5. #include <util/datetime/constants.h>
  6. #include <ctime>
  7. // XXX: uses system calls for trivial things. may be very slow therefore.
  8. time_t GetDateStart(time_t ts);
  9. // Local date (without time zone)
  10. class TDate {
  11. // XXX: wrong: must store number of days since epoch
  12. time_t Timestamp;
  13. public:
  14. TDate()
  15. : Timestamp(0)
  16. {
  17. }
  18. // XXX: wrong. Should be replace with two methods: TodayGmt() and TodayLocal()
  19. static TDate Today() {
  20. return TDate(time(nullptr));
  21. }
  22. TDate(const char* yyyymmdd);
  23. TDate(const TString& yyyymmdd);
  24. TDate(unsigned year, unsigned month, unsigned monthDay); // month from 01, monthDay from 01
  25. TDate(const TString& date, const TString& format);
  26. explicit TDate(time_t t);
  27. time_t GetStart() const {
  28. return Timestamp;
  29. }
  30. time_t GetStartUTC() const;
  31. TString ToStroka(const char* format = "%Y%m%d") const;
  32. TDate& operator++() {
  33. Timestamp = GetDateStart(Timestamp + 3 * (SECONDS_IN_DAY / 2));
  34. return *this;
  35. }
  36. TDate& operator--() {
  37. Timestamp = GetDateStart(Timestamp - SECONDS_IN_DAY / 2);
  38. return *this;
  39. }
  40. TDate& operator+=(unsigned days) {
  41. Timestamp = GetDateStart(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2);
  42. return *this;
  43. }
  44. TDate& operator-=(unsigned days) {
  45. Timestamp = GetDateStart(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2);
  46. return *this;
  47. }
  48. TDate operator+(unsigned days) const {
  49. return TDate(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2);
  50. }
  51. TDate operator-(unsigned days) const {
  52. return TDate(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2);
  53. }
  54. unsigned GetWeekDay() const; // days since Sunday
  55. unsigned GetYear() const;
  56. unsigned GetMonth() const; // from 01
  57. unsigned GetMonthDay() const; // from 01
  58. friend bool operator<(const TDate& left, const TDate& right);
  59. friend bool operator>(const TDate& left, const TDate& right);
  60. friend bool operator<=(const TDate& left, const TDate& right);
  61. friend bool operator>=(const TDate& left, const TDate& right);
  62. friend bool operator==(const TDate& left, const TDate& right);
  63. friend int operator-(const TDate& left, const TDate& right);
  64. friend IInputStream& operator>>(IInputStream& left, TDate& right);
  65. friend IOutputStream& operator<<(IOutputStream& left, const TDate& right);
  66. };
  67. Y_DECLARE_PODTYPE(TDate);
  68. inline bool operator<(const TDate& left, const TDate& right) {
  69. return left.Timestamp < right.Timestamp;
  70. }
  71. inline bool operator>(const TDate& left, const TDate& right) {
  72. return left.Timestamp > right.Timestamp;
  73. }
  74. inline bool operator<=(const TDate& left, const TDate& right) {
  75. return left.Timestamp <= right.Timestamp;
  76. }
  77. inline bool operator>=(const TDate& left, const TDate& right) {
  78. return left.Timestamp >= right.Timestamp;
  79. }
  80. inline bool operator==(const TDate& left, const TDate& right) {
  81. return left.Timestamp == right.Timestamp;
  82. }
  83. inline int operator-(const TDate& left, const TDate& right) {
  84. if (left < right) {
  85. return -(right - left);
  86. }
  87. return static_cast<int>((left.Timestamp + SECONDS_IN_DAY / 2 - right.Timestamp) / SECONDS_IN_DAY);
  88. }
  89. inline IInputStream& operator>>(IInputStream& left, TDate& right) {
  90. TString stroka;
  91. left >> stroka;
  92. TDate date(stroka.c_str());
  93. right = date;
  94. return left;
  95. }
  96. inline IOutputStream& operator<<(IOutputStream& left, const TDate& right) {
  97. return left << right.ToStroka();
  98. }