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