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 
unix_epoch()46 inline cctz::time_point<cctz::seconds> unix_epoch() {
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.
Split(absl::Time t)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.
Join(const cctz_parts & parts)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);
69   return time_internal::FromUnixDuration(d);
70 }
71 
72 }  // namespace
73 
FormatTime(absl::string_view format,absl::Time t,absl::TimeZone tz)74 std::string FormatTime(absl::string_view format, absl::Time t,
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 
FormatTime(absl::Time t,absl::TimeZone tz)83 std::string FormatTime(absl::Time t, absl::TimeZone tz) {
84   return FormatTime(RFC3339_full, t, tz);
85 }
86 
FormatTime(absl::Time t)87 std::string FormatTime(absl::Time t) {
88   return absl::FormatTime(RFC3339_full, t, absl::LocalTimeZone());
89 }
90 
ParseTime(absl::string_view format,absl::string_view input,absl::Time * time,std::string * err)91 bool ParseTime(absl::string_view format, absl::string_view input,
92                absl::Time* time, std::string* err) {
93   return absl::ParseTime(format, input, absl::UTCTimeZone(), time, 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.
ParseTime(absl::string_view format,absl::string_view input,absl::TimeZone tz,absl::Time * time,std::string * err)98 bool ParseTime(absl::string_view format, absl::string_view input,
99                absl::TimeZone tz, absl::Time* time, std::string* err) {
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;
111     absl::Time value;
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 
130   std::string error;
131   cctz_parts parts;
132   const bool b =
133       cctz::detail::parse(std::string(format), std::string(input),
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.
AbslParseFlag(absl::string_view text,absl::Time * t,std::string * error)144 bool AbslParseFlag(absl::string_view text, absl::Time* t, std::string* error) {
145   return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
146 }
147 
AbslUnparseFlag(absl::Time t)148 std::string AbslUnparseFlag(absl::Time t) {
149   return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
150 }
ParseFlag(const std::string & text,absl::Time * t,std::string * error)151 bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) {
152   return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
153 }
154 
UnparseFlag(absl::Time t)155 std::string UnparseFlag(absl::Time t) {
156   return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
157 }
158 
159 ABSL_NAMESPACE_END
160 }  // namespace absl
161