format.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include <cctype>
  16. #include <cstdint>
  17. #include <utility>
  18. #include "absl/strings/match.h"
  19. #include "absl/strings/string_view.h"
  20. #include "absl/time/internal/cctz/include/cctz/time_zone.h"
  21. #include "absl/time/time.h"
  22. namespace cctz = absl::time_internal::cctz;
  23. namespace absl {
  24. ABSL_NAMESPACE_BEGIN
  25. ABSL_DLL extern const char RFC3339_full[] = "%Y-%m-%d%ET%H:%M:%E*S%Ez";
  26. ABSL_DLL extern const char RFC3339_sec[] = "%Y-%m-%d%ET%H:%M:%S%Ez";
  27. ABSL_DLL extern const char RFC1123_full[] = "%a, %d %b %E4Y %H:%M:%S %z";
  28. ABSL_DLL extern const char RFC1123_no_wday[] = "%d %b %E4Y %H:%M:%S %z";
  29. namespace {
  30. const char kInfiniteFutureStr[] = "infinite-future";
  31. const char kInfinitePastStr[] = "infinite-past";
  32. struct cctz_parts {
  33. cctz::time_point<cctz::seconds> sec;
  34. cctz::detail::femtoseconds fem;
  35. };
  36. inline cctz::time_point<cctz::seconds> unix_epoch() {
  37. return std::chrono::time_point_cast<cctz::seconds>(
  38. std::chrono::system_clock::from_time_t(0));
  39. }
  40. // Splits a Time into seconds and femtoseconds, which can be used with CCTZ.
  41. // Requires that 't' is finite. See duration.cc for details about rep_hi and
  42. // rep_lo.
  43. cctz_parts Split(absl::Time t) {
  44. const auto d = time_internal::ToUnixDuration(t);
  45. const int64_t rep_hi = time_internal::GetRepHi(d);
  46. const int64_t rep_lo = time_internal::GetRepLo(d);
  47. const auto sec = unix_epoch() + cctz::seconds(rep_hi);
  48. const auto fem = cctz::detail::femtoseconds(rep_lo * (1000 * 1000 / 4));
  49. return {sec, fem};
  50. }
  51. // Joins the given seconds and femtoseconds into a Time. See duration.cc for
  52. // details about rep_hi and rep_lo.
  53. absl::Time Join(const cctz_parts& parts) {
  54. const int64_t rep_hi = (parts.sec - unix_epoch()).count();
  55. const uint32_t rep_lo =
  56. static_cast<uint32_t>(parts.fem.count() / (1000 * 1000 / 4));
  57. const auto d = time_internal::MakeDuration(rep_hi, rep_lo);
  58. return time_internal::FromUnixDuration(d);
  59. }
  60. } // namespace
  61. std::string FormatTime(absl::string_view format, absl::Time t,
  62. absl::TimeZone tz) {
  63. if (t == absl::InfiniteFuture()) return std::string(kInfiniteFutureStr);
  64. if (t == absl::InfinitePast()) return std::string(kInfinitePastStr);
  65. const auto parts = Split(t);
  66. return cctz::detail::format(std::string(format), parts.sec, parts.fem,
  67. cctz::time_zone(tz));
  68. }
  69. std::string FormatTime(absl::Time t, absl::TimeZone tz) {
  70. return FormatTime(RFC3339_full, t, tz);
  71. }
  72. std::string FormatTime(absl::Time t) {
  73. return absl::FormatTime(RFC3339_full, t, absl::LocalTimeZone());
  74. }
  75. bool ParseTime(absl::string_view format, absl::string_view input,
  76. absl::Time* time, std::string* err) {
  77. return absl::ParseTime(format, input, absl::UTCTimeZone(), time, err);
  78. }
  79. // If the input string does not contain an explicit UTC offset, interpret
  80. // the fields with respect to the given TimeZone.
  81. bool ParseTime(absl::string_view format, absl::string_view input,
  82. absl::TimeZone tz, absl::Time* time, std::string* err) {
  83. auto strip_leading_space = [](absl::string_view* sv) {
  84. while (!sv->empty()) {
  85. if (!std::isspace(sv->front())) return;
  86. sv->remove_prefix(1);
  87. }
  88. };
  89. // Portable toolchains means we don't get nice constexpr here.
  90. struct Literal {
  91. const char* name;
  92. size_t size;
  93. absl::Time value;
  94. };
  95. static Literal literals[] = {
  96. {kInfiniteFutureStr, strlen(kInfiniteFutureStr), InfiniteFuture()},
  97. {kInfinitePastStr, strlen(kInfinitePastStr), InfinitePast()},
  98. };
  99. strip_leading_space(&input);
  100. for (const auto& lit : literals) {
  101. if (absl::StartsWith(input, absl::string_view(lit.name, lit.size))) {
  102. absl::string_view tail = input;
  103. tail.remove_prefix(lit.size);
  104. strip_leading_space(&tail);
  105. if (tail.empty()) {
  106. *time = lit.value;
  107. return true;
  108. }
  109. }
  110. }
  111. std::string error;
  112. cctz_parts parts;
  113. const bool b =
  114. cctz::detail::parse(std::string(format), std::string(input),
  115. cctz::time_zone(tz), &parts.sec, &parts.fem, &error);
  116. if (b) {
  117. *time = Join(parts);
  118. } else if (err != nullptr) {
  119. *err = std::move(error);
  120. }
  121. return b;
  122. }
  123. // Functions required to support absl::Time flags.
  124. bool AbslParseFlag(absl::string_view text, absl::Time* t, std::string* error) {
  125. return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
  126. }
  127. std::string AbslUnparseFlag(absl::Time t) {
  128. return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
  129. }
  130. bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) {
  131. return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
  132. }
  133. std::string UnparseFlag(absl::Time t) {
  134. return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
  135. }
  136. ABSL_NAMESPACE_END
  137. } // namespace absl