• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 "absl/time/civil_time.h"
16 
17 #include <cstdlib>
18 #include <string>
19 
20 #include "absl/strings/str_cat.h"
21 #include "absl/time/time.h"
22 
23 namespace absl {
24 ABSL_NAMESPACE_BEGIN
25 
26 namespace {
27 
28 // Since a civil time has a larger year range than absl::Time (64-bit years vs
29 // 64-bit seconds, respectively) we normalize years to roughly +/- 400 years
30 // around the year 2400, which will produce an equivalent year in a range that
31 // absl::Time can handle.
NormalizeYear(civil_year_t year)32 inline civil_year_t NormalizeYear(civil_year_t year) {
33   return 2400 + year % 400;
34 }
35 
36 // Formats the given CivilSecond according to the given format.
FormatYearAnd(string_view fmt,CivilSecond cs)37 std::string FormatYearAnd(string_view fmt, CivilSecond cs) {
38   const CivilSecond ncs(NormalizeYear(cs.year()), cs.month(), cs.day(),
39                         cs.hour(), cs.minute(), cs.second());
40   const TimeZone utc = UTCTimeZone();
41   return StrCat(cs.year(), FormatTime(fmt, FromCivil(ncs, utc), utc));
42 }
43 
44 template <typename CivilT>
ParseYearAnd(string_view fmt,string_view s,CivilT * c)45 bool ParseYearAnd(string_view fmt, string_view s, CivilT* c) {
46   // Civil times support a larger year range than absl::Time, so we need to
47   // parse the year separately, normalize it, then use absl::ParseTime on the
48   // normalized string.
49   const std::string ss = std::string(s);  // TODO(absl-team): Avoid conversion.
50   const char* const np = ss.c_str();
51   char* endp;
52   errno = 0;
53   const civil_year_t y =
54       std::strtoll(np, &endp, 10);  // NOLINT(runtime/deprecated_fn)
55   if (endp == np || errno == ERANGE) return false;
56   const std::string norm = StrCat(NormalizeYear(y), endp);
57 
58   const TimeZone utc = UTCTimeZone();
59   Time t;
60   if (ParseTime(StrCat("%Y", fmt), norm, utc, &t, nullptr)) {
61     const auto cs = ToCivilSecond(t, utc);
62     *c = CivilT(y, cs.month(), cs.day(), cs.hour(), cs.minute(), cs.second());
63     return true;
64   }
65 
66   return false;
67 }
68 
69 // Tries to parse the type as a CivilT1, but then assigns the result to the
70 // argument of type CivilT2.
71 template <typename CivilT1, typename CivilT2>
ParseAs(string_view s,CivilT2 * c)72 bool ParseAs(string_view s, CivilT2* c) {
73   CivilT1 t1;
74   if (ParseCivilTime(s, &t1)) {
75     *c = CivilT2(t1);
76     return true;
77   }
78   return false;
79 }
80 
81 template <typename CivilT>
ParseLenient(string_view s,CivilT * c)82 bool ParseLenient(string_view s, CivilT* c) {
83   // A fastpath for when the given string data parses exactly into the given
84   // type T (e.g., s="YYYY-MM-DD" and CivilT=CivilDay).
85   if (ParseCivilTime(s, c)) return true;
86   // Try parsing as each of the 6 types, trying the most common types first
87   // (based on csearch results).
88   if (ParseAs<CivilDay>(s, c)) return true;
89   if (ParseAs<CivilSecond>(s, c)) return true;
90   if (ParseAs<CivilHour>(s, c)) return true;
91   if (ParseAs<CivilMonth>(s, c)) return true;
92   if (ParseAs<CivilMinute>(s, c)) return true;
93   if (ParseAs<CivilYear>(s, c)) return true;
94   return false;
95 }
96 }  // namespace
97 
FormatCivilTime(CivilSecond c)98 std::string FormatCivilTime(CivilSecond c) {
99   return FormatYearAnd("-%m-%d%ET%H:%M:%S", c);
100 }
FormatCivilTime(CivilMinute c)101 std::string FormatCivilTime(CivilMinute c) {
102   return FormatYearAnd("-%m-%d%ET%H:%M", c);
103 }
FormatCivilTime(CivilHour c)104 std::string FormatCivilTime(CivilHour c) {
105   return FormatYearAnd("-%m-%d%ET%H", c);
106 }
FormatCivilTime(CivilDay c)107 std::string FormatCivilTime(CivilDay c) { return FormatYearAnd("-%m-%d", c); }
FormatCivilTime(CivilMonth c)108 std::string FormatCivilTime(CivilMonth c) { return FormatYearAnd("-%m", c); }
FormatCivilTime(CivilYear c)109 std::string FormatCivilTime(CivilYear c) { return FormatYearAnd("", c); }
110 
ParseCivilTime(string_view s,CivilSecond * c)111 bool ParseCivilTime(string_view s, CivilSecond* c) {
112   return ParseYearAnd("-%m-%d%ET%H:%M:%S", s, c);
113 }
ParseCivilTime(string_view s,CivilMinute * c)114 bool ParseCivilTime(string_view s, CivilMinute* c) {
115   return ParseYearAnd("-%m-%d%ET%H:%M", s, c);
116 }
ParseCivilTime(string_view s,CivilHour * c)117 bool ParseCivilTime(string_view s, CivilHour* c) {
118   return ParseYearAnd("-%m-%d%ET%H", s, c);
119 }
ParseCivilTime(string_view s,CivilDay * c)120 bool ParseCivilTime(string_view s, CivilDay* c) {
121   return ParseYearAnd("-%m-%d", s, c);
122 }
ParseCivilTime(string_view s,CivilMonth * c)123 bool ParseCivilTime(string_view s, CivilMonth* c) {
124   return ParseYearAnd("-%m", s, c);
125 }
ParseCivilTime(string_view s,CivilYear * c)126 bool ParseCivilTime(string_view s, CivilYear* c) {
127   return ParseYearAnd("", s, c);
128 }
129 
ParseLenientCivilTime(string_view s,CivilSecond * c)130 bool ParseLenientCivilTime(string_view s, CivilSecond* c) {
131   return ParseLenient(s, c);
132 }
ParseLenientCivilTime(string_view s,CivilMinute * c)133 bool ParseLenientCivilTime(string_view s, CivilMinute* c) {
134   return ParseLenient(s, c);
135 }
ParseLenientCivilTime(string_view s,CivilHour * c)136 bool ParseLenientCivilTime(string_view s, CivilHour* c) {
137   return ParseLenient(s, c);
138 }
ParseLenientCivilTime(string_view s,CivilDay * c)139 bool ParseLenientCivilTime(string_view s, CivilDay* c) {
140   return ParseLenient(s, c);
141 }
ParseLenientCivilTime(string_view s,CivilMonth * c)142 bool ParseLenientCivilTime(string_view s, CivilMonth* c) {
143   return ParseLenient(s, c);
144 }
ParseLenientCivilTime(string_view s,CivilYear * c)145 bool ParseLenientCivilTime(string_view s, CivilYear* c) {
146   return ParseLenient(s, c);
147 }
148 
149 namespace time_internal {
150 
operator <<(std::ostream & os,CivilYear y)151 std::ostream& operator<<(std::ostream& os, CivilYear y) {
152   return os << FormatCivilTime(y);
153 }
operator <<(std::ostream & os,CivilMonth m)154 std::ostream& operator<<(std::ostream& os, CivilMonth m) {
155   return os << FormatCivilTime(m);
156 }
operator <<(std::ostream & os,CivilDay d)157 std::ostream& operator<<(std::ostream& os, CivilDay d) {
158   return os << FormatCivilTime(d);
159 }
operator <<(std::ostream & os,CivilHour h)160 std::ostream& operator<<(std::ostream& os, CivilHour h) {
161   return os << FormatCivilTime(h);
162 }
operator <<(std::ostream & os,CivilMinute m)163 std::ostream& operator<<(std::ostream& os, CivilMinute m) {
164   return os << FormatCivilTime(m);
165 }
operator <<(std::ostream & os,CivilSecond s)166 std::ostream& operator<<(std::ostream& os, CivilSecond s) {
167   return os << FormatCivilTime(s);
168 }
169 
170 }  // namespace time_internal
171 
172 ABSL_NAMESPACE_END
173 }  // namespace absl
174