abseil-cpp/absl/time/format.cc
Go to the documentation of this file.
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 
20 #include "absl/strings/match.h"
21 #include "absl/strings/string_view.h"
22 #include "absl/time/internal/cctz/include/cctz/time_zone.h"
23 #include "absl/time/time.h"
24 
26 
27 namespace absl {
29 
30 ABSL_DLL extern const char RFC3339_full[] = "%Y-%m-%d%ET%H:%M:%E*S%Ez";
31 ABSL_DLL extern const char RFC3339_sec[] = "%Y-%m-%d%ET%H:%M:%S%Ez";
32 
33 ABSL_DLL extern const char RFC1123_full[] = "%a, %d %b %E4Y %H:%M:%S %z";
34 ABSL_DLL extern const char RFC1123_no_wday[] = "%d %b %E4Y %H:%M:%S %z";
35 
36 namespace {
37 
38 const char kInfiniteFutureStr[] = "infinite-future";
39 const char kInfinitePastStr[] = "infinite-past";
40 
41 struct cctz_parts {
44 };
45 
47  return std::chrono::time_point_cast<cctz::seconds>(
48  std::chrono::system_clock::from_time_t(0));
49 }
50 
51 // Splits a Time into seconds and femtoseconds, which can be used with CCTZ.
52 // Requires that 't' is finite. See duration.cc for details about rep_hi and
53 // rep_lo.
54 cctz_parts Split(absl::Time t) {
55  const auto d = time_internal::ToUnixDuration(t);
56  const int64_t rep_hi = time_internal::GetRepHi(d);
57  const int64_t rep_lo = time_internal::GetRepLo(d);
58  const auto sec = unix_epoch() + cctz::seconds(rep_hi);
59  const auto fem = cctz::detail::femtoseconds(rep_lo * (1000 * 1000 / 4));
60  return {sec, fem};
61 }
62 
63 // Joins the given seconds and femtoseconds into a Time. See duration.cc for
64 // details about rep_hi and rep_lo.
65 absl::Time Join(const cctz_parts& parts) {
66  const int64_t rep_hi = (parts.sec - unix_epoch()).count();
67  const uint32_t rep_lo = parts.fem.count() / (1000 * 1000 / 4);
68  const auto d = time_internal::MakeDuration(rep_hi, rep_lo);
70 }
71 
72 } // namespace
73 
75  absl::TimeZone tz) {
76  if (t == absl::InfiniteFuture()) return std::string(kInfiniteFutureStr);
77  if (t == absl::InfinitePast()) return std::string(kInfinitePastStr);
78  const auto parts = Split(t);
79  return cctz::detail::format(std::string(format), parts.sec, parts.fem,
80  cctz::time_zone(tz));
81 }
82 
84  return FormatTime(RFC3339_full, t, tz);
85 }
86 
89 }
90 
92  absl::Time* time, std::string* err) {
94 }
95 
96 // If the input string does not contain an explicit UTC offset, interpret
97 // the fields with respect to the given TimeZone.
100  auto strip_leading_space = [](absl::string_view* sv) {
101  while (!sv->empty()) {
102  if (!std::isspace(sv->front())) return;
103  sv->remove_prefix(1);
104  }
105  };
106 
107  // Portable toolchains means we don't get nice constexpr here.
108  struct Literal {
109  const char* name;
110  size_t size;
112  };
113  static Literal literals[] = {
114  {kInfiniteFutureStr, strlen(kInfiniteFutureStr), InfiniteFuture()},
115  {kInfinitePastStr, strlen(kInfinitePastStr), InfinitePast()},
116  };
117  strip_leading_space(&input);
118  for (const auto& lit : literals) {
119  if (absl::StartsWith(input, absl::string_view(lit.name, lit.size))) {
120  absl::string_view tail = input;
121  tail.remove_prefix(lit.size);
122  strip_leading_space(&tail);
123  if (tail.empty()) {
124  *time = lit.value;
125  return true;
126  }
127  }
128  }
129 
131  cctz_parts parts;
132  const bool b =
134  cctz::time_zone(tz), &parts.sec, &parts.fem, &error);
135  if (b) {
136  *time = Join(parts);
137  } else if (err != nullptr) {
138  *err = error;
139  }
140  return b;
141 }
142 
143 // Functions required to support absl::Time flags.
146 }
147 
150 }
153 }
154 
157 }
158 
160 } // namespace absl
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
absl::ParseFlag
ABSL_NAMESPACE_BEGIN bool ParseFlag(absl::string_view input, T *dst, std::string *error)
Definition: abseil-cpp/absl/flags/marshalling.h:328
http2_test_server.format
format
Definition: http2_test_server.py:118
absl::Time
Definition: third_party/abseil-cpp/absl/time/time.h:642
absl::time_internal::MakeDuration
constexpr Duration MakeDuration(int64_t hi, uint32_t lo)
Definition: third_party/abseil-cpp/absl/time/time.h:1392
absl::time_internal::cctz::time_point
std::chrono::time_point< std::chrono::system_clock, D > time_point
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:39
string.h
absl::StartsWith
bool StartsWith(absl::string_view text, absl::string_view prefix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:58
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
error_ref_leak.err
err
Definition: error_ref_leak.py:35
absl::LocalTimeZone
TimeZone LocalTimeZone()
Definition: third_party/abseil-cpp/absl/time/time.h:1109
setup.name
name
Definition: setup.py:542
absl::UnparseFlag
std::string UnparseFlag(const T &v)
Definition: abseil-cpp/absl/flags/marshalling.h:342
absl::UTCTimeZone
TimeZone UTCTimeZone()
Definition: third_party/abseil-cpp/absl/time/time.h:1099
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::time_internal::cctz::detail::femtoseconds
std::chrono::duration< std::int_fast64_t, std::femto > femtoseconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:267
absl::FormatTime
std::string FormatTime(absl::string_view format, absl::Time t, absl::TimeZone tz)
Definition: abseil-cpp/absl/time/format.cc:74
absl::AbslParseFlag
bool AbslParseFlag(absl::string_view text, absl::LogSeverity *dst, std::string *err)
Definition: abseil-cpp/absl/flags/marshalling.cc:202
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
absl::ParseTime
bool ParseTime(absl::string_view format, absl::string_view input, absl::Time *time, std::string *err)
Definition: abseil-cpp/absl/time/format.cc:91
grpc_generator::Split
void Split(const std::string &s, char, std::vector< std::string > *append_to)
Definition: generator_helpers.h:169
absl::RFC3339_full
ABSL_NAMESPACE_BEGIN const ABSL_DLL char RFC3339_full[]
Definition: third_party/abseil-cpp/absl/time/time.h:1261
grpc_core::promise_detail::Join
BasicJoin< JoinTraits, Promises... > Join
Definition: join.h:42
absl::AbslUnparseFlag
std::string AbslUnparseFlag(absl::LogSeverity v)
Definition: abseil-cpp/absl/flags/marshalling.cc:235
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
ABSL_DLL
#define ABSL_DLL
Definition: third_party/abseil-cpp/absl/base/config.h:746
absl::RFC1123_full
const ABSL_DLL char RFC1123_full[]
Definition: third_party/abseil-cpp/absl/time/time.h:1268
value
const char * value
Definition: hpack_parser_table.cc:165
absl::time_internal::ToUnixDuration
constexpr Duration ToUnixDuration(Time t)
Definition: third_party/abseil-cpp/absl/time/time.h:1451
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
absl::string_view::remove_prefix
ABSL_INTERNAL_STRING_VIEW_CXX14_CONSTEXPR void remove_prefix(size_type n)
Definition: abseil-cpp/absl/strings/string_view.h:344
absl::RFC3339_sec
const ABSL_DLL char RFC3339_sec[]
Definition: third_party/abseil-cpp/absl/time/time.h:1262
parse
static void parse(const char *s)
Definition: debug/trace.cc:121
absl::time_internal::cctz::time_zone
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:68
absl::InfinitePast
constexpr Time InfinitePast()
Definition: third_party/abseil-cpp/absl/time/time.h:768
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
absl::time_internal::GetRepHi
constexpr int64_t GetRepHi(Duration d)
Definition: third_party/abseil-cpp/absl/time/time.h:1422
absl::string_view::empty
constexpr bool empty() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:292
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::RFC1123_no_wday
const ABSL_DLL char RFC1123_no_wday[]
Definition: third_party/abseil-cpp/absl/time/time.h:1269
absl::time_internal::GetRepLo
constexpr uint32_t GetRepLo(Duration d)
Definition: third_party/abseil-cpp/absl/time/time.h:1423
make_curve25519_tables.d
int d
Definition: make_curve25519_tables.py:53
absl::ABSL_NAMESPACE_BEGIN::unix_epoch
cctz::time_point< cctz::seconds > unix_epoch()
Definition: third_party/abseil-cpp/absl/time/time.cc:54
absl::time_internal::FromUnixDuration
constexpr Time FromUnixDuration(Duration d)
Definition: third_party/abseil-cpp/absl/time/time.h:1450
absl::time_internal::cctz
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/civil_time.h:24
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
absl::TimeZone
Definition: third_party/abseil-cpp/absl/time/time.h:912
absl::InfiniteFuture
constexpr Time InfiniteFuture()
Definition: third_party/abseil-cpp/absl/time/time.h:760


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:24