Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 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 | #include <cctype> |
| 17 | #include <cstdint> |
| 18 | |
| 19 | #include "absl/time/internal/cctz/include/cctz/time_zone.h" |
| 20 | #include "absl/time/time.h" |
| 21 | |
| 22 | namespace cctz = absl::time_internal::cctz; |
| 23 | |
| 24 | namespace absl { |
| 25 | |
| 26 | extern const char RFC3339_full[] = "%Y-%m-%dT%H:%M:%E*S%Ez"; |
| 27 | extern const char RFC3339_sec[] = "%Y-%m-%dT%H:%M:%S%Ez"; |
| 28 | |
| 29 | extern const char RFC1123_full[] = "%a, %d %b %E4Y %H:%M:%S %z"; |
| 30 | extern const char RFC1123_no_wday[] = "%d %b %E4Y %H:%M:%S %z"; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | const char kInfiniteFutureStr[] = "infinite-future"; |
| 35 | const char kInfinitePastStr[] = "infinite-past"; |
| 36 | |
| 37 | struct cctz_parts { |
| 38 | cctz::time_point<cctz::seconds> sec; |
| 39 | cctz::detail::femtoseconds fem; |
| 40 | }; |
| 41 | |
| 42 | inline cctz::time_point<cctz::seconds> unix_epoch() { |
| 43 | return std::chrono::time_point_cast<cctz::seconds>( |
| 44 | std::chrono::system_clock::from_time_t(0)); |
| 45 | } |
| 46 | |
| 47 | // Splits a Time into seconds and femtoseconds, which can be used with CCTZ. |
| 48 | // Requires that 't' is finite. See duration.cc for details about rep_hi and |
| 49 | // rep_lo. |
| 50 | cctz_parts Split(absl::Time t) { |
| 51 | const auto d = time_internal::ToUnixDuration(t); |
| 52 | const int64_t rep_hi = time_internal::GetRepHi(d); |
| 53 | const int64_t rep_lo = time_internal::GetRepLo(d); |
| 54 | const auto sec = unix_epoch() + cctz::seconds(rep_hi); |
| 55 | const auto fem = cctz::detail::femtoseconds(rep_lo * (1000 * 1000 / 4)); |
| 56 | return {sec, fem}; |
| 57 | } |
| 58 | |
| 59 | // Joins the given seconds and femtoseconds into a Time. See duration.cc for |
| 60 | // details about rep_hi and rep_lo. |
| 61 | absl::Time Join(const cctz_parts& parts) { |
| 62 | const int64_t rep_hi = (parts.sec - unix_epoch()).count(); |
| 63 | const uint32_t rep_lo = parts.fem.count() / (1000 * 1000 / 4); |
| 64 | const auto d = time_internal::MakeDuration(rep_hi, rep_lo); |
| 65 | return time_internal::FromUnixDuration(d); |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | std::string FormatTime(const std::string& format, absl::Time t, |
| 71 | absl::TimeZone tz) { |
| 72 | if (t == absl::InfiniteFuture()) return kInfiniteFutureStr; |
| 73 | if (t == absl::InfinitePast()) return kInfinitePastStr; |
| 74 | const auto parts = Split(t); |
| 75 | return cctz::detail::format(format, parts.sec, parts.fem, |
| 76 | cctz::time_zone(tz)); |
| 77 | } |
| 78 | |
| 79 | std::string FormatTime(absl::Time t, absl::TimeZone tz) { |
| 80 | return FormatTime(RFC3339_full, t, tz); |
| 81 | } |
| 82 | |
| 83 | std::string FormatTime(absl::Time t) { |
| 84 | return absl::FormatTime(RFC3339_full, t, absl::LocalTimeZone()); |
| 85 | } |
| 86 | |
| 87 | bool ParseTime(const std::string& format, const std::string& input, |
| 88 | absl::Time* time, std::string* err) { |
| 89 | return absl::ParseTime(format, input, absl::UTCTimeZone(), time, err); |
| 90 | } |
| 91 | |
| 92 | // If the input string does not contain an explicit UTC offset, interpret |
| 93 | // the fields with respect to the given TimeZone. |
| 94 | bool ParseTime(const std::string& format, const std::string& input, |
| 95 | absl::TimeZone tz, absl::Time* time, std::string* err) { |
| 96 | const char* data = input.c_str(); |
| 97 | while (std::isspace(*data)) ++data; |
| 98 | |
| 99 | size_t inf_size = strlen(kInfiniteFutureStr); |
| 100 | if (strncmp(data, kInfiniteFutureStr, inf_size) == 0) { |
| 101 | const char* new_data = data + inf_size; |
| 102 | while (std::isspace(*new_data)) ++new_data; |
| 103 | if (*new_data == '\0') { |
| 104 | *time = InfiniteFuture(); |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | inf_size = strlen(kInfinitePastStr); |
| 110 | if (strncmp(data, kInfinitePastStr, inf_size) == 0) { |
| 111 | const char* new_data = data + inf_size; |
| 112 | while (std::isspace(*new_data)) ++new_data; |
| 113 | if (*new_data == '\0') { |
| 114 | *time = InfinitePast(); |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | std::string error; |
| 120 | cctz_parts parts; |
| 121 | const bool b = cctz::detail::parse(format, input, cctz::time_zone(tz), |
| 122 | &parts.sec, &parts.fem, &error); |
| 123 | if (b) { |
| 124 | *time = Join(parts); |
| 125 | } else if (err != nullptr) { |
| 126 | *err = error; |
| 127 | } |
| 128 | return b; |
| 129 | } |
| 130 | |
| 131 | // Functions required to support absl::Time flags. |
| 132 | bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) { |
| 133 | return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error); |
| 134 | } |
| 135 | |
| 136 | std::string UnparseFlag(absl::Time t) { |
| 137 | return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone()); |
| 138 | } |
| 139 | |
| 140 | } // namespace absl |