DateTime.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/crt/DateTime.h>
  6. #include <chrono>
  7. namespace Aws
  8. {
  9. namespace Crt
  10. {
  11. DateTime::DateTime() noexcept : m_good(true)
  12. {
  13. std::chrono::system_clock::time_point time;
  14. aws_date_time_init_epoch_millis(
  15. &m_date_time,
  16. static_cast<uint64_t>(
  17. std::chrono::duration_cast<std::chrono::milliseconds>(time.time_since_epoch()).count()));
  18. }
  19. DateTime::DateTime(const std::chrono::system_clock::time_point &timepointToAssign) noexcept : m_good(true)
  20. {
  21. aws_date_time_init_epoch_millis(
  22. &m_date_time,
  23. static_cast<uint64_t>(
  24. std::chrono::duration_cast<std::chrono::milliseconds>(timepointToAssign.time_since_epoch())
  25. .count()));
  26. }
  27. DateTime::DateTime(uint64_t millisSinceEpoch) noexcept : m_good(true)
  28. {
  29. aws_date_time_init_epoch_millis(&m_date_time, millisSinceEpoch);
  30. }
  31. DateTime::DateTime(double epoch_millis) noexcept : m_good(true)
  32. {
  33. aws_date_time_init_epoch_secs(&m_date_time, epoch_millis);
  34. }
  35. DateTime::DateTime(const char *timestamp, DateFormat format) noexcept
  36. {
  37. ByteBuf timeStampBuf = ByteBufFromCString(timestamp);
  38. m_good =
  39. (aws_date_time_init_from_str(&m_date_time, &timeStampBuf, static_cast<aws_date_format>(format)) ==
  40. AWS_ERROR_SUCCESS);
  41. }
  42. bool DateTime::operator==(const DateTime &other) const noexcept
  43. {
  44. return aws_date_time_diff(&m_date_time, &other.m_date_time) == 0;
  45. }
  46. bool DateTime::operator<(const DateTime &other) const noexcept
  47. {
  48. return aws_date_time_diff(&m_date_time, &other.m_date_time) < 0;
  49. }
  50. bool DateTime::operator>(const DateTime &other) const noexcept
  51. {
  52. return aws_date_time_diff(&m_date_time, &other.m_date_time) > 0;
  53. }
  54. bool DateTime::operator!=(const DateTime &other) const noexcept { return !(*this == other); }
  55. bool DateTime::operator<=(const DateTime &other) const noexcept
  56. {
  57. return aws_date_time_diff(&m_date_time, &other.m_date_time) <= 0;
  58. }
  59. bool DateTime::operator>=(const DateTime &other) const noexcept
  60. {
  61. return aws_date_time_diff(&m_date_time, &other.m_date_time) >= 0;
  62. }
  63. DateTime DateTime::operator+(const std::chrono::milliseconds &a) const noexcept
  64. {
  65. auto currentTime = aws_date_time_as_millis(&m_date_time);
  66. currentTime += a.count();
  67. return {currentTime};
  68. }
  69. DateTime DateTime::operator-(const std::chrono::milliseconds &a) const noexcept
  70. {
  71. auto currentTime = aws_date_time_as_millis(&m_date_time);
  72. currentTime -= a.count();
  73. return {currentTime};
  74. }
  75. DateTime &DateTime::operator=(double secondsSinceEpoch) noexcept
  76. {
  77. aws_date_time_init_epoch_secs(&m_date_time, secondsSinceEpoch);
  78. m_good = true;
  79. return *this;
  80. }
  81. DateTime &DateTime::operator=(uint64_t millisSinceEpoch) noexcept
  82. {
  83. aws_date_time_init_epoch_millis(&m_date_time, millisSinceEpoch);
  84. m_good = true;
  85. return *this;
  86. }
  87. DateTime &DateTime::operator=(const std::chrono::system_clock::time_point &timepointToAssign) noexcept
  88. {
  89. aws_date_time_init_epoch_millis(
  90. &m_date_time,
  91. static_cast<uint64_t>(
  92. std::chrono::duration_cast<std::chrono::milliseconds>(timepointToAssign.time_since_epoch())
  93. .count()));
  94. m_good = true;
  95. return *this;
  96. }
  97. DateTime &DateTime::operator=(const char *timestamp) noexcept
  98. {
  99. ByteBuf timeStampBuf = aws_byte_buf_from_c_str(timestamp);
  100. m_good = aws_date_time_init_from_str(
  101. &m_date_time, &timeStampBuf, static_cast<aws_date_format>(DateFormat::AutoDetect)) ==
  102. AWS_ERROR_SUCCESS;
  103. return *this;
  104. }
  105. DateTime::operator bool() const noexcept { return m_good; }
  106. int DateTime::GetLastError() const noexcept { return aws_last_error(); }
  107. bool DateTime::ToLocalTimeString(DateFormat format, ByteBuf &outputBuf) const noexcept
  108. {
  109. return (
  110. aws_date_time_to_local_time_str(&m_date_time, static_cast<aws_date_format>(format), &outputBuf) ==
  111. AWS_ERROR_SUCCESS);
  112. }
  113. bool DateTime::ToGmtString(DateFormat format, ByteBuf &outputBuf) const noexcept
  114. {
  115. return (
  116. aws_date_time_to_utc_time_str(&m_date_time, static_cast<aws_date_format>(format), &outputBuf) ==
  117. AWS_ERROR_SUCCESS);
  118. }
  119. double DateTime::SecondsWithMSPrecision() const noexcept { return aws_date_time_as_epoch_secs(&m_date_time); }
  120. uint64_t DateTime::Millis() const noexcept { return aws_date_time_as_millis(&m_date_time); }
  121. std::chrono::system_clock::time_point DateTime::UnderlyingTimestamp() const noexcept
  122. {
  123. return std::chrono::system_clock::from_time_t(m_date_time.timestamp);
  124. }
  125. uint16_t DateTime::GetYear(bool localTime) const noexcept
  126. {
  127. return aws_date_time_year(&m_date_time, localTime);
  128. }
  129. Month DateTime::GetMonth(bool localTime) const noexcept
  130. {
  131. return static_cast<Month>(aws_date_time_month(&m_date_time, localTime));
  132. }
  133. uint8_t DateTime::GetDay(bool localTime) const noexcept
  134. {
  135. return aws_date_time_month_day(&m_date_time, localTime);
  136. }
  137. DayOfWeek DateTime::GetDayOfWeek(bool localTime) const noexcept
  138. {
  139. return static_cast<DayOfWeek>(aws_date_time_day_of_week(&m_date_time, localTime));
  140. }
  141. uint8_t DateTime::GetHour(bool localTime) const noexcept { return aws_date_time_hour(&m_date_time, localTime); }
  142. uint8_t DateTime::GetMinute(bool localTime) const noexcept
  143. {
  144. return aws_date_time_minute(&m_date_time, localTime);
  145. }
  146. uint8_t DateTime::GetSecond(bool localTime) const noexcept
  147. {
  148. return aws_date_time_second(&m_date_time, localTime);
  149. }
  150. bool DateTime::IsDST(bool localTime) const noexcept { return aws_date_time_dst(&m_date_time, localTime); }
  151. DateTime DateTime::Now() noexcept
  152. {
  153. DateTime dateTime;
  154. aws_date_time_init_now(&dateTime.m_date_time);
  155. return dateTime;
  156. }
  157. std::chrono::milliseconds DateTime::operator-(const DateTime &other) const noexcept
  158. {
  159. auto diff = aws_date_time_diff(&m_date_time, &other.m_date_time);
  160. return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::seconds(diff));
  161. }
  162. } // namespace Crt
  163. } // namespace Aws