format.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include <string.h>
00016 #include <cctype>
00017 #include <cstdint>
00018 
00019 #include "absl/time/internal/cctz/include/cctz/time_zone.h"
00020 #include "absl/time/time.h"
00021 
00022 namespace cctz = absl::time_internal::cctz;
00023 
00024 namespace absl {
00025 
00026 extern const char RFC3339_full[] = "%Y-%m-%dT%H:%M:%E*S%Ez";
00027 extern const char RFC3339_sec[] =  "%Y-%m-%dT%H:%M:%S%Ez";
00028 
00029 extern const char RFC1123_full[] = "%a, %d %b %E4Y %H:%M:%S %z";
00030 extern const char RFC1123_no_wday[] =  "%d %b %E4Y %H:%M:%S %z";
00031 
00032 namespace {
00033 
00034 const char kInfiniteFutureStr[] = "infinite-future";
00035 const char kInfinitePastStr[] = "infinite-past";
00036 
00037 struct cctz_parts {
00038   cctz::time_point<cctz::seconds> sec;
00039   cctz::detail::femtoseconds fem;
00040 };
00041 
00042 inline cctz::time_point<cctz::seconds> unix_epoch() {
00043   return std::chrono::time_point_cast<cctz::seconds>(
00044       std::chrono::system_clock::from_time_t(0));
00045 }
00046 
00047 // Splits a Time into seconds and femtoseconds, which can be used with CCTZ.
00048 // Requires that 't' is finite. See duration.cc for details about rep_hi and
00049 // rep_lo.
00050 cctz_parts Split(absl::Time t) {
00051   const auto d = time_internal::ToUnixDuration(t);
00052   const int64_t rep_hi = time_internal::GetRepHi(d);
00053   const int64_t rep_lo = time_internal::GetRepLo(d);
00054   const auto sec = unix_epoch() + cctz::seconds(rep_hi);
00055   const auto fem = cctz::detail::femtoseconds(rep_lo * (1000 * 1000 / 4));
00056   return {sec, fem};
00057 }
00058 
00059 // Joins the given seconds and femtoseconds into a Time. See duration.cc for
00060 // details about rep_hi and rep_lo.
00061 absl::Time Join(const cctz_parts& parts) {
00062   const int64_t rep_hi = (parts.sec - unix_epoch()).count();
00063   const uint32_t rep_lo = parts.fem.count() / (1000 * 1000 / 4);
00064   const auto d = time_internal::MakeDuration(rep_hi, rep_lo);
00065   return time_internal::FromUnixDuration(d);
00066 }
00067 
00068 }  // namespace
00069 
00070 std::string FormatTime(const std::string& format, absl::Time t,
00071                        absl::TimeZone tz) {
00072   if (t == absl::InfiniteFuture()) return kInfiniteFutureStr;
00073   if (t == absl::InfinitePast()) return kInfinitePastStr;
00074   const auto parts = Split(t);
00075   return cctz::detail::format(format, parts.sec, parts.fem,
00076                               cctz::time_zone(tz));
00077 }
00078 
00079 std::string FormatTime(absl::Time t, absl::TimeZone tz) {
00080   return FormatTime(RFC3339_full, t, tz);
00081 }
00082 
00083 std::string FormatTime(absl::Time t) {
00084   return absl::FormatTime(RFC3339_full, t, absl::LocalTimeZone());
00085 }
00086 
00087 bool ParseTime(const std::string& format, const std::string& input,
00088                absl::Time* time, std::string* err) {
00089   return absl::ParseTime(format, input, absl::UTCTimeZone(), time, err);
00090 }
00091 
00092 // If the input string does not contain an explicit UTC offset, interpret
00093 // the fields with respect to the given TimeZone.
00094 bool ParseTime(const std::string& format, const std::string& input,
00095                absl::TimeZone tz, absl::Time* time, std::string* err) {
00096   const char* data = input.c_str();
00097   while (std::isspace(*data)) ++data;
00098 
00099   size_t inf_size = strlen(kInfiniteFutureStr);
00100   if (strncmp(data, kInfiniteFutureStr, inf_size) == 0) {
00101     const char* new_data = data + inf_size;
00102     while (std::isspace(*new_data)) ++new_data;
00103     if (*new_data == '\0') {
00104       *time = InfiniteFuture();
00105       return true;
00106     }
00107   }
00108 
00109   inf_size = strlen(kInfinitePastStr);
00110   if (strncmp(data, kInfinitePastStr, inf_size) == 0) {
00111     const char* new_data = data + inf_size;
00112     while (std::isspace(*new_data)) ++new_data;
00113     if (*new_data == '\0') {
00114       *time = InfinitePast();
00115       return true;
00116     }
00117   }
00118 
00119   std::string error;
00120   cctz_parts parts;
00121   const bool b = cctz::detail::parse(format, input, cctz::time_zone(tz),
00122                                      &parts.sec, &parts.fem, &error);
00123   if (b) {
00124     *time = Join(parts);
00125   } else if (err != nullptr) {
00126     *err = error;
00127   }
00128   return b;
00129 }
00130 
00131 // Functions required to support absl::Time flags.
00132 bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) {
00133   return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
00134 }
00135 
00136 std::string UnparseFlag(absl::Time t) {
00137   return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
00138 }
00139 
00140 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14