format.cc 5.3 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 "y_absl/strings/match.h"
  18. #include "y_absl/strings/string_view.h"
  19. #include "y_absl/time/internal/cctz/include/cctz/time_zone.h"
  20. #include "y_absl/time/time.h"
  21. namespace cctz = y_absl::time_internal::cctz;
  22. namespace y_absl {
  23. Y_ABSL_NAMESPACE_BEGIN
  24. Y_ABSL_DLL extern const char RFC3339_full[] = "%Y-%m-%d%ET%H:%M:%E*S%Ez";
  25. Y_ABSL_DLL extern const char RFC3339_sec[] = "%Y-%m-%d%ET%H:%M:%S%Ez";
  26. Y_ABSL_DLL extern const char RFC1123_full[] = "%a, %d %b %E4Y %H:%M:%S %z";
  27. Y_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(y_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. y_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. TString FormatTime(y_absl::string_view format, y_absl::Time t,
  61. y_absl::TimeZone tz) {
  62. if (t == y_absl::InfiniteFuture()) return TString(kInfiniteFutureStr);
  63. if (t == y_absl::InfinitePast()) return TString(kInfinitePastStr);
  64. const auto parts = Split(t);
  65. return cctz::detail::format(TString(format), parts.sec, parts.fem,
  66. cctz::time_zone(tz));
  67. }
  68. TString FormatTime(y_absl::Time t, y_absl::TimeZone tz) {
  69. return FormatTime(RFC3339_full, t, tz);
  70. }
  71. TString FormatTime(y_absl::Time t) {
  72. return y_absl::FormatTime(RFC3339_full, t, y_absl::LocalTimeZone());
  73. }
  74. bool ParseTime(y_absl::string_view format, y_absl::string_view input,
  75. y_absl::Time* time, TString* err) {
  76. return y_absl::ParseTime(format, input, y_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(y_absl::string_view format, y_absl::string_view input,
  81. y_absl::TimeZone tz, y_absl::Time* time, TString* err) {
  82. auto strip_leading_space = [](y_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. y_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 (y_absl::StartsWith(input, y_absl::string_view(lit.name, lit.size))) {
  101. y_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. TString error;
  111. cctz_parts parts;
  112. const bool b =
  113. cctz::detail::parse(TString(format), TString(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 y_absl::Time flags.
  123. bool AbslParseFlag(y_absl::string_view text, y_absl::Time* t, TString* error) {
  124. return y_absl::ParseTime(RFC3339_full, text, y_absl::UTCTimeZone(), t, error);
  125. }
  126. TString AbslUnparseFlag(y_absl::Time t) {
  127. return y_absl::FormatTime(RFC3339_full, t, y_absl::UTCTimeZone());
  128. }
  129. bool ParseFlag(const TString& text, y_absl::Time* t, TString* error) {
  130. return y_absl::ParseTime(RFC3339_full, text, y_absl::UTCTimeZone(), t, error);
  131. }
  132. TString UnparseFlag(y_absl::Time t) {
  133. return y_absl::FormatTime(RFC3339_full, t, y_absl::UTCTimeZone());
  134. }
  135. Y_ABSL_NAMESPACE_END
  136. } // namespace y_absl