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