date.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "date.h"
  2. #include <util/string/cast.h>
  3. #include <util/generic/yexception.h>
  4. #include <util/datetime/base.h>
  5. time_t GetDateStart(time_t ts) {
  6. tm dateTm;
  7. memset(&dateTm, 0, sizeof(tm));
  8. localtime_r(&ts, &dateTm);
  9. dateTm.tm_isdst = -1;
  10. dateTm.tm_sec = 0;
  11. dateTm.tm_min = 0;
  12. dateTm.tm_hour = 0;
  13. return mktime(&dateTm);
  14. }
  15. static time_t ParseDate(const char* date, const char* format) {
  16. tm dateTm;
  17. memset(&dateTm, 0, sizeof(tm));
  18. if (!strptime(date, format, &dateTm)) {
  19. ythrow yexception() << "Invalid date string and format: " << date << ", " << format;
  20. }
  21. return mktime(&dateTm);
  22. }
  23. static time_t ParseDate(const char* dateStr) {
  24. if (strlen(dateStr) != 8) {
  25. ythrow yexception() << "Invalid date string: " << dateStr;
  26. }
  27. return ParseDate(dateStr, "%Y%m%d");
  28. }
  29. template <>
  30. TDate FromStringImpl<TDate>(const char* data, size_t len) {
  31. return TDate(ParseDate(TString(data, len).data()));
  32. }
  33. TDate::TDate(const char* yyyymmdd)
  34. : Timestamp(GetDateStart(ParseDate(yyyymmdd)))
  35. {
  36. }
  37. TDate::TDate(const TString& yyyymmdd)
  38. : Timestamp(GetDateStart(ParseDate(yyyymmdd.c_str())))
  39. {
  40. }
  41. TDate::TDate(time_t ts)
  42. : Timestamp(GetDateStart(ts))
  43. {
  44. }
  45. TDate::TDate(const TString& date, const TString& format)
  46. : Timestamp(GetDateStart(ParseDate(date.data(), format.data())))
  47. {
  48. }
  49. TDate::TDate(unsigned year, unsigned month, unsigned monthDay) {
  50. tm dateTm;
  51. Zero(dateTm);
  52. dateTm.tm_year = year - 1900;
  53. dateTm.tm_mon = month - 1;
  54. dateTm.tm_mday = monthDay;
  55. dateTm.tm_isdst = -1;
  56. Timestamp = mktime(&dateTm);
  57. if (Timestamp == (time_t)-1) {
  58. ythrow yexception() << "Invalid TDate args:(" << year << ',' << month << ',' << monthDay << ')';
  59. }
  60. }
  61. time_t TDate::GetStartUTC() const {
  62. tm dateTm;
  63. localtime_r(&Timestamp, &dateTm);
  64. dateTm.tm_isdst = -1;
  65. dateTm.tm_sec = 0;
  66. dateTm.tm_min = 0;
  67. dateTm.tm_hour = 0;
  68. return TimeGM(&dateTm);
  69. }
  70. TString TDate::ToStroka(const char* format) const {
  71. tm dateTm;
  72. localtime_r(&Timestamp, &dateTm);
  73. return Strftime(format, &dateTm);
  74. }
  75. unsigned TDate::GetWeekDay() const {
  76. tm dateTm;
  77. localtime_r(&Timestamp, &dateTm);
  78. return (unsigned)dateTm.tm_wday;
  79. }
  80. unsigned TDate::GetYear() const {
  81. tm dateTm;
  82. localtime_r(&Timestamp, &dateTm);
  83. return ((unsigned)dateTm.tm_year) + 1900;
  84. }
  85. unsigned TDate::GetMonth() const {
  86. tm dateTm;
  87. localtime_r(&Timestamp, &dateTm);
  88. return ((unsigned)dateTm.tm_mon) + 1;
  89. }
  90. unsigned TDate::GetMonthDay() const {
  91. tm dateTm;
  92. localtime_r(&Timestamp, &dateTm);
  93. return (unsigned)dateTm.tm_mday;
  94. }