/src/abseil-cpp/absl/time/format.cc
Line | Count | Source |
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 | | |
25 | | namespace cctz = absl::time_internal::cctz; |
26 | | |
27 | | namespace absl { |
28 | | ABSL_NAMESPACE_BEGIN |
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 { |
42 | | cctz::time_point<cctz::seconds> sec; |
43 | | cctz::detail::femtoseconds fem; |
44 | | }; |
45 | | |
46 | 56 | inline cctz::time_point<cctz::seconds> unix_epoch() { |
47 | 56 | return std::chrono::time_point_cast<cctz::seconds>( |
48 | 56 | std::chrono::system_clock::from_time_t(0)); |
49 | 56 | } |
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 | 56 | cctz_parts Split(absl::Time t) { |
55 | 56 | const auto d = time_internal::ToUnixDuration(t); |
56 | 56 | const int64_t rep_hi = time_internal::GetRepHi(d); |
57 | 56 | const int64_t rep_lo = time_internal::GetRepLo(d); |
58 | 56 | const auto sec = unix_epoch() + cctz::seconds(rep_hi); |
59 | 56 | const auto fem = cctz::detail::femtoseconds(rep_lo * (1000 * 1000 / 4)); |
60 | 56 | return {sec, fem}; |
61 | 56 | } |
62 | | |
63 | | // Joins the given seconds and femtoseconds into a Time. See duration.cc for |
64 | | // details about rep_hi and rep_lo. |
65 | 0 | absl::Time Join(const cctz_parts& parts) { |
66 | 0 | const int64_t rep_hi = (parts.sec - unix_epoch()).count(); |
67 | 0 | const uint32_t rep_lo = |
68 | 0 | static_cast<uint32_t>(parts.fem.count() / (1000 * 1000 / 4)); |
69 | 0 | const auto d = time_internal::MakeDuration(rep_hi, rep_lo); |
70 | 0 | return time_internal::FromUnixDuration(d); |
71 | 0 | } |
72 | | |
73 | | } // namespace |
74 | | |
75 | | std::string FormatTime(absl::string_view format, absl::Time t, |
76 | 56 | absl::TimeZone tz) { |
77 | 56 | if (t == absl::InfiniteFuture()) return std::string(kInfiniteFutureStr); |
78 | 56 | if (t == absl::InfinitePast()) return std::string(kInfinitePastStr); |
79 | 56 | const auto parts = Split(t); |
80 | 56 | return cctz::detail::format(std::string(format), parts.sec, parts.fem, |
81 | 56 | cctz::time_zone(tz)); |
82 | 56 | } |
83 | | |
84 | 0 | std::string FormatTime(absl::Time t, absl::TimeZone tz) { |
85 | 0 | return FormatTime(RFC3339_full, t, tz); |
86 | 0 | } |
87 | | |
88 | 56 | std::string FormatTime(absl::Time t) { |
89 | 56 | return absl::FormatTime(RFC3339_full, t, absl::LocalTimeZone()); |
90 | 56 | } |
91 | | |
92 | | bool ParseTime(absl::string_view format, absl::string_view input, |
93 | 0 | absl::Time* time, std::string* err) { |
94 | 0 | return absl::ParseTime(format, input, absl::UTCTimeZone(), time, err); |
95 | 0 | } |
96 | | |
97 | | // If the input string does not contain an explicit UTC offset, interpret |
98 | | // the fields with respect to the given TimeZone. |
99 | | bool ParseTime(absl::string_view format, absl::string_view input, |
100 | 0 | absl::TimeZone tz, absl::Time* time, std::string* err) { |
101 | 0 | auto strip_leading_space = [](absl::string_view* sv) { |
102 | 0 | while (!sv->empty()) { |
103 | 0 | if (!std::isspace(sv->front())) return; |
104 | 0 | sv->remove_prefix(1); |
105 | 0 | } |
106 | 0 | }; |
107 | | |
108 | | // Portable toolchains means we don't get nice constexpr here. |
109 | 0 | struct Literal { |
110 | 0 | const char* name; |
111 | 0 | size_t size; |
112 | 0 | absl::Time value; |
113 | 0 | }; |
114 | 0 | static Literal literals[] = { |
115 | 0 | {kInfiniteFutureStr, strlen(kInfiniteFutureStr), InfiniteFuture()}, |
116 | 0 | {kInfinitePastStr, strlen(kInfinitePastStr), InfinitePast()}, |
117 | 0 | }; |
118 | 0 | strip_leading_space(&input); |
119 | 0 | for (const auto& lit : literals) { |
120 | 0 | if (absl::StartsWith(input, absl::string_view(lit.name, lit.size))) { |
121 | 0 | absl::string_view tail = input; |
122 | 0 | tail.remove_prefix(lit.size); |
123 | 0 | strip_leading_space(&tail); |
124 | 0 | if (tail.empty()) { |
125 | 0 | *time = lit.value; |
126 | 0 | return true; |
127 | 0 | } |
128 | 0 | } |
129 | 0 | } |
130 | | |
131 | 0 | std::string error; |
132 | 0 | cctz_parts parts; |
133 | 0 | const bool b = |
134 | 0 | cctz::detail::parse(std::string(format), std::string(input), |
135 | 0 | cctz::time_zone(tz), &parts.sec, &parts.fem, &error); |
136 | 0 | if (b) { |
137 | 0 | *time = Join(parts); |
138 | 0 | } else if (err != nullptr) { |
139 | 0 | *err = error; |
140 | 0 | } |
141 | 0 | return b; |
142 | 0 | } |
143 | | |
144 | | // Functions required to support absl::Time flags. |
145 | 0 | bool AbslParseFlag(absl::string_view text, absl::Time* t, std::string* error) { |
146 | 0 | return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error); |
147 | 0 | } |
148 | | |
149 | 0 | std::string AbslUnparseFlag(absl::Time t) { |
150 | 0 | return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone()); |
151 | 0 | } |
152 | 0 | bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) { |
153 | 0 | return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error); |
154 | 0 | } |
155 | | |
156 | 0 | std::string UnparseFlag(absl::Time t) { |
157 | 0 | return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone()); |
158 | 0 | } |
159 | | |
160 | | ABSL_NAMESPACE_END |
161 | | } // namespace absl |