format.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "absl/strings/match.h"
  18. #include "absl/strings/string_view.h"
  19. #include "absl/time/internal/cctz/include/cctz/time_zone.h"
  20. #include "absl/time/time.h"
  21. namespace cctz = absl::time_internal::cctz;
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. ABSL_DLL extern const char RFC3339_full[] = "%Y-%m-%d%ET%H:%M:%E*S%Ez";
  25. ABSL_DLL extern const char RFC3339_sec[] = "%Y-%m-%d%ET%H:%M:%S%Ez";
  26. ABSL_DLL extern const char RFC1123_full[] = "%a, %d %b %E4Y %H:%M:%S %z";
  27. ABSL_DLL extern const char RFC1123_no_wday[] = "%d %b %E4Y %H:%M:%S %z";
  28. namespace {
  29. const char kInfiniteFutureStr[] = "infinite-future";
  30. const char kInfinitePastStr[] = "infinite-past";
  31. struct cctz_parts {
  32. cctz::time_point<cctz::seconds> sec;
  33. cctz::detail::femtoseconds fem;
  34. };
  35. inline cctz::time_point<cctz::seconds> unix_epoch() {
  36. return std::chrono::time_point_cast<cctz::seconds>(
  37. std::chrono::system_clock::from_time_t(0));
  38. }
  39. // Splits a Time into seconds and femtoseconds, which can be used with CCTZ.
  40. // Requires that 't' is finite. See duration.cc for details about rep_hi and
  41. // rep_lo.
  42. cctz_parts Split(absl::Time t) {
  43. const auto d = time_internal::ToUnixDuration(t);
  44. const int64_t rep_hi = time_internal::GetRepHi(d);
  45. const int64_t rep_lo = time_internal::GetRepLo(d);
  46. const auto sec = unix_epoch() + cctz::seconds(rep_hi);
  47. const auto fem = cctz::detail::femtoseconds(rep_lo * (1000 * 1000 / 4));
  48. return {sec, fem};
  49. }
  50. // Joins the given seconds and femtoseconds into a Time. See duration.cc for
  51. // details about rep_hi and rep_lo.
  52. absl::Time Join(const cctz_parts& parts) {
  53. const int64_t rep_hi = (parts.sec - unix_epoch()).count();
  54. const uint32_t rep_lo =
  55. static_cast<uint32_t>(parts.fem.count() / (1000 * 1000 / 4));
  56. const auto d = time_internal::MakeDuration(rep_hi, rep_lo);
  57. return time_internal::FromUnixDuration(d);
  58. }
  59. } // namespace
  60. std::string FormatTime(absl::string_view format, absl::Time t,
  61. absl::TimeZone tz) {
  62. if (t == absl::InfiniteFuture()) return std::string(kInfiniteFutureStr);
  63. if (t == absl::InfinitePast()) return std::string(kInfinitePastStr);
  64. const auto parts = Split(t);
  65. return cctz::detail::format(std::string(format), parts.sec, parts.fem,
  66. cctz::time_zone(tz));
  67. }
  68. std::string FormatTime(absl::Time t, absl::TimeZone tz) {
  69. return FormatTime(RFC3339_full, t, tz);
  70. }
  71. std::string FormatTime(absl::Time t) {
  72. return absl::FormatTime(RFC3339_full, t, absl::LocalTimeZone());
  73. }
  74. bool ParseTime(absl::string_view format, absl::string_view input,
  75. absl::Time* time, std::string* err) {
  76. return absl::ParseTime(format, input, absl::UTCTimeZone(), time, err);
  77. }
  78. // If the input string does not contain an explicit UTC offset, interpret
  79. // the fields with respect to the given TimeZone.
  80. bool ParseTime(absl::string_view format, absl::string_view input,
  81. absl::TimeZone tz, absl::Time* time, std::string* err) {
  82. auto strip_leading_space = [](absl::string_view* sv) {
  83. while (!sv->empty()) {
  84. if (!std::isspace(sv->front())) return;
  85. sv->remove_prefix(1);
  86. }
  87. };
  88. // Portable toolchains means we don't get nice constexpr here.
  89. struct Literal {
  90. const char* name;
  91. size_t size;
  92. absl::Time value;
  93. };
  94. static Literal literals[] = {
  95. {kInfiniteFutureStr, strlen(kInfiniteFutureStr), InfiniteFuture()},
  96. {kInfinitePastStr, strlen(kInfinitePastStr), InfinitePast()},
  97. };
  98. strip_leading_space(&input);
  99. for (const auto& lit : literals) {
  100. if (absl::StartsWith(input, absl::string_view(lit.name, lit.size))) {
  101. absl::string_view tail = input;
  102. tail.remove_prefix(lit.size);
  103. strip_leading_space(&tail);
  104. if (tail.empty()) {
  105. *time = lit.value;
  106. return true;
  107. }
  108. }
  109. }
  110. std::string error;
  111. cctz_parts parts;
  112. const bool b =
  113. cctz::detail::parse(std::string(format), std::string(input),
  114. cctz::time_zone(tz), &parts.sec, &parts.fem, &error);
  115. if (b) {
  116. *time = Join(parts);
  117. } else if (err != nullptr) {
  118. *err = error;
  119. }
  120. return b;
  121. }
  122. // Functions required to support absl::Time flags.
  123. bool AbslParseFlag(absl::string_view text, absl::Time* t, std::string* error) {
  124. return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
  125. }
  126. std::string AbslUnparseFlag(absl::Time t) {
  127. return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
  128. }
  129. bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) {
  130. return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
  131. }
  132. std::string UnparseFlag(absl::Time t) {
  133. return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
  134. }
  135. ABSL_NAMESPACE_END
  136. } // namespace absl