format.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = parts.fem.count() / (1000 * 1000 / 4);
  55. const auto d = time_internal::MakeDuration(rep_hi, rep_lo);
  56. return time_internal::FromUnixDuration(d);
  57. }
  58. } // namespace
  59. std::string FormatTime(absl::string_view format, absl::Time t,
  60. absl::TimeZone tz) {
  61. if (t == absl::InfiniteFuture()) return std::string(kInfiniteFutureStr);
  62. if (t == absl::InfinitePast()) return std::string(kInfinitePastStr);
  63. const auto parts = Split(t);
  64. return cctz::detail::format(std::string(format), parts.sec, parts.fem,
  65. cctz::time_zone(tz));
  66. }
  67. std::string FormatTime(absl::Time t, absl::TimeZone tz) {
  68. return FormatTime(RFC3339_full, t, tz);
  69. }
  70. std::string FormatTime(absl::Time t) {
  71. return absl::FormatTime(RFC3339_full, t, absl::LocalTimeZone());
  72. }
  73. bool ParseTime(absl::string_view format, absl::string_view input,
  74. absl::Time* time, std::string* err) {
  75. return absl::ParseTime(format, input, absl::UTCTimeZone(), time, err);
  76. }
  77. // If the input string does not contain an explicit UTC offset, interpret
  78. // the fields with respect to the given TimeZone.
  79. bool ParseTime(absl::string_view format, absl::string_view input,
  80. absl::TimeZone tz, absl::Time* time, std::string* err) {
  81. auto strip_leading_space = [](absl::string_view* sv) {
  82. while (!sv->empty()) {
  83. if (!std::isspace(sv->front())) return;
  84. sv->remove_prefix(1);
  85. }
  86. };
  87. // Portable toolchains means we don't get nice constexpr here.
  88. struct Literal {
  89. const char* name;
  90. size_t size;
  91. absl::Time value;
  92. };
  93. static Literal literals[] = {
  94. {kInfiniteFutureStr, strlen(kInfiniteFutureStr), InfiniteFuture()},
  95. {kInfinitePastStr, strlen(kInfinitePastStr), InfinitePast()},
  96. };
  97. strip_leading_space(&input);
  98. for (const auto& lit : literals) {
  99. if (absl::StartsWith(input, absl::string_view(lit.name, lit.size))) {
  100. absl::string_view tail = input;
  101. tail.remove_prefix(lit.size);
  102. strip_leading_space(&tail);
  103. if (tail.empty()) {
  104. *time = lit.value;
  105. return true;
  106. }
  107. }
  108. }
  109. std::string error;
  110. cctz_parts parts;
  111. const bool b =
  112. cctz::detail::parse(std::string(format), std::string(input),
  113. cctz::time_zone(tz), &parts.sec, &parts.fem, &error);
  114. if (b) {
  115. *time = Join(parts);
  116. } else if (err != nullptr) {
  117. *err = error;
  118. }
  119. return b;
  120. }
  121. // Functions required to support absl::Time flags.
  122. bool AbslParseFlag(absl::string_view text, absl::Time* t, std::string* error) {
  123. return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
  124. }
  125. std::string AbslUnparseFlag(absl::Time t) {
  126. return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
  127. }
  128. bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) {
  129. return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
  130. }
  131. std::string UnparseFlag(absl::Time t) {
  132. return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
  133. }
  134. ABSL_NAMESPACE_END
  135. } // namespace absl