1 // Copyright 2016 Google Inc. All Rights Reserved.
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 #ifndef ABSL_TIME_INTERNAL_CCTZ_CIVIL_TIME_H_
16 #define ABSL_TIME_INTERNAL_CCTZ_CIVIL_TIME_H_
17 
18 #include "absl/base/config.h"
19 #include "absl/time/internal/cctz/include/cctz/civil_time_detail.h"
20 
21 namespace absl {
22 ABSL_NAMESPACE_BEGIN
23 namespace time_internal {
24 namespace cctz {
25 
26 // The term "civil time" refers to the legally recognized human-scale time
27 // that is represented by the six fields YYYY-MM-DD hh:mm:ss. Modern-day civil
28 // time follows the Gregorian Calendar and is a time-zone-independent concept.
29 // A "date" is perhaps the most common example of a civil time (represented in
30 // this library as cctz::civil_day). This library provides six classes and a
31 // handful of functions that help with rounding, iterating, and arithmetic on
32 // civil times while avoiding complications like daylight-saving time (DST).
33 //
34 // The following six classes form the core of this civil-time library:
35 //
36 //   * civil_second
37 //   * civil_minute
38 //   * civil_hour
39 //   * civil_day
40 //   * civil_month
41 //   * civil_year
42 //
43 // Each class is a simple value type with the same interface for construction
44 // and the same six accessors for each of the civil fields (year, month, day,
45 // hour, minute, and second, aka YMDHMS). These classes differ only in their
46 // alignment, which is indicated by the type name and specifies the field on
47 // which arithmetic operates.
48 //
49 // Each class can be constructed by passing up to six optional integer
50 // arguments representing the YMDHMS fields (in that order) to the
51 // constructor. Omitted fields are assigned their minimum valid value. Hours,
52 // minutes, and seconds will be set to 0, month and day will be set to 1, and
53 // since there is no minimum valid year, it will be set to 1970. So, a
54 // default-constructed civil-time object will have YMDHMS fields representing
55 // "1970-01-01 00:00:00". Fields that are out-of-range are normalized (e.g.,
56 // October 32 -> November 1) so that all civil-time objects represent valid
57 // values.
58 //
59 // Each civil-time class is aligned to the civil-time field indicated in the
60 // class's name after normalization. Alignment is performed by setting all the
61 // inferior fields to their minimum valid value (as described above). The
62 // following are examples of how each of the six types would align the fields
63 // representing November 22, 2015 at 12:34:56 in the afternoon. (Note: the
64 // string format used here is not important; it's just a shorthand way of
65 // showing the six YMDHMS fields.)
66 //
67 //   civil_second  2015-11-22 12:34:56
68 //   civil_minute  2015-11-22 12:34:00
69 //   civil_hour    2015-11-22 12:00:00
70 //   civil_day     2015-11-22 00:00:00
71 //   civil_month   2015-11-01 00:00:00
72 //   civil_year    2015-01-01 00:00:00
73 //
74 // Each civil-time type performs arithmetic on the field to which it is
75 // aligned. This means that adding 1 to a civil_day increments the day field
76 // (normalizing as necessary), and subtracting 7 from a civil_month operates
77 // on the month field (normalizing as necessary). All arithmetic produces a
78 // valid civil time. Difference requires two similarly aligned civil-time
79 // objects and returns the scalar answer in units of the objects' alignment.
80 // For example, the difference between two civil_hour objects will give an
81 // answer in units of civil hours.
82 //
83 // In addition to the six civil-time types just described, there are
84 // a handful of helper functions and algorithms for performing common
85 // calculations. These are described below.
86 //
87 // Note: In C++14 and later, this library is usable in a constexpr context.
88 //
89 // CONSTRUCTION:
90 //
91 // Each of the civil-time types can be constructed in two ways: by directly
92 // passing to the constructor up to six (optional) integers representing the
93 // YMDHMS fields, or by copying the YMDHMS fields from a differently aligned
94 // civil-time type.
95 //
96 //   civil_day default_value;  // 1970-01-01 00:00:00
97 //
98 //   civil_day a(2015, 2, 3);           // 2015-02-03 00:00:00
99 //   civil_day b(2015, 2, 3, 4, 5, 6);  // 2015-02-03 00:00:00
100 //   civil_day c(2015);                 // 2015-01-01 00:00:00
101 //
102 //   civil_second ss(2015, 2, 3, 4, 5, 6);  // 2015-02-03 04:05:06
103 //   civil_minute mm(ss);                   // 2015-02-03 04:05:00
104 //   civil_hour hh(mm);                     // 2015-02-03 04:00:00
105 //   civil_day d(hh);                       // 2015-02-03 00:00:00
106 //   civil_month m(d);                      // 2015-02-01 00:00:00
107 //   civil_year y(m);                       // 2015-01-01 00:00:00
108 //
109 //   m = civil_month(y);     // 2015-01-01 00:00:00
110 //   d = civil_day(m);       // 2015-01-01 00:00:00
111 //   hh = civil_hour(d);     // 2015-01-01 00:00:00
112 //   mm = civil_minute(hh);  // 2015-01-01 00:00:00
113 //   ss = civil_second(mm);  // 2015-01-01 00:00:00
114 //
115 // ALIGNMENT CONVERSION:
116 //
117 // The alignment of a civil-time object cannot change, but the object may be
118 // used to construct a new object with a different alignment. This is referred
119 // to as "realigning". When realigning to a type with the same or more
120 // precision (e.g., civil_day -> civil_second), the conversion may be
121 // performed implicitly since no information is lost. However, if information
122 // could be discarded (e.g., civil_second -> civil_day), the conversion must
123 // be explicit at the call site.
124 //
125 //   void fun(const civil_day& day);
126 //
127 //   civil_second cs;
128 //   fun(cs);  // Won't compile because data may be discarded
129 //   fun(civil_day(cs));  // OK: explicit conversion
130 //
131 //   civil_day cd;
132 //   fun(cd);  // OK: no conversion needed
133 //
134 //   civil_month cm;
135 //   fun(cm);  // OK: implicit conversion to civil_day
136 //
137 // NORMALIZATION:
138 //
139 // Integer arguments passed to the constructor may be out-of-range, in which
140 // case they are normalized to produce a valid civil-time object. This enables
141 // natural arithmetic on constructor arguments without worrying about the
142 // field's range. Normalization guarantees that there are no invalid
143 // civil-time objects.
144 //
145 //   civil_day d(2016, 10, 32);  // Out-of-range day; normalized to 2016-11-01
146 //
147 // Note: If normalization is undesired, you can signal an error by comparing
148 // the constructor arguments to the normalized values returned by the YMDHMS
149 // properties.
150 //
151 // PROPERTIES:
152 //
153 // All civil-time types have accessors for all six of the civil-time fields:
154 // year, month, day, hour, minute, and second. Recall that fields inferior to
155 // the type's alignment will be set to their minimum valid value.
156 //
157 //   civil_day d(2015, 6, 28);
158 //   // d.year() == 2015
159 //   // d.month() == 6
160 //   // d.day() == 28
161 //   // d.hour() == 0
162 //   // d.minute() == 0
163 //   // d.second() == 0
164 //
165 // COMPARISON:
166 //
167 // Comparison always considers all six YMDHMS fields, regardless of the type's
168 // alignment. Comparison between differently aligned civil-time types is
169 // allowed.
170 //
171 //   civil_day feb_3(2015, 2, 3);  // 2015-02-03 00:00:00
172 //   civil_day mar_4(2015, 3, 4);  // 2015-03-04 00:00:00
173 //   // feb_3 < mar_4
174 //   // civil_year(feb_3) == civil_year(mar_4)
175 //
176 //   civil_second feb_3_noon(2015, 2, 3, 12, 0, 0);  // 2015-02-03 12:00:00
177 //   // feb_3 < feb_3_noon
178 //   // feb_3 == civil_day(feb_3_noon)
179 //
180 //   // Iterates all the days of February 2015.
181 //   for (civil_day d(2015, 2, 1); d < civil_month(2015, 3); ++d) {
182 //     // ...
183 //   }
184 //
185 // STREAMING:
186 //
187 // Each civil-time type may be sent to an output stream using operator<<().
188 // The output format follows the pattern "YYYY-MM-DDThh:mm:ss" where fields
189 // inferior to the type's alignment are omitted.
190 //
191 //   civil_second cs(2015, 2, 3, 4, 5, 6);
192 //   std::cout << cs << "\n";  // Outputs: 2015-02-03T04:05:06
193 //
194 //   civil_day cd(cs);
195 //   std::cout << cd << "\n";  // Outputs: 2015-02-03
196 //
197 //   civil_year cy(cs);
198 //   std::cout << cy << "\n";  // Outputs: 2015
199 //
200 // ARITHMETIC:
201 //
202 // Civil-time types support natural arithmetic operators such as addition,
203 // subtraction, and difference. Arithmetic operates on the civil-time field
204 // indicated in the type's name. Difference requires arguments with the same
205 // alignment and returns the answer in units of the alignment.
206 //
207 //   civil_day a(2015, 2, 3);
208 //   ++a;                         // 2015-02-04 00:00:00
209 //   --a;                         // 2015-02-03 00:00:00
210 //   civil_day b = a + 1;         // 2015-02-04 00:00:00
211 //   civil_day c = 1 + b;         // 2015-02-05 00:00:00
212 //   int n = c - a;               // n = 2 (civil days)
213 //   int m = c - civil_month(c);  // Won't compile: different types.
214 //
215 // EXAMPLE: Adding a month to January 31.
216 //
217 // One of the classic questions that arises when considering a civil-time
218 // library (or a date library or a date/time library) is this: "What happens
219 // when you add a month to January 31?" This is an interesting question
220 // because there could be a number of possible answers:
221 //
222 //   1. March 3 (or 2 if a leap year). This may make sense if the operation
223 //      wants the equivalent of February 31.
224 //   2. February 28 (or 29 if a leap year). This may make sense if the operation
225 //      wants the last day of January to go to the last day of February.
226 //   3. Error. The caller may get some error, an exception, an invalid date
227 //      object, or maybe false is returned. This may make sense because there is
228 //      no single unambiguously correct answer to the question.
229 //
230 // Practically speaking, any answer that is not what the programmer intended
231 // is the wrong answer.
232 //
233 // This civil-time library avoids the problem by making it impossible to ask
234 // ambiguous questions. All civil-time objects are aligned to a particular
235 // civil-field boundary (such as aligned to a year, month, day, hour, minute,
236 // or second), and arithmetic operates on the field to which the object is
237 // aligned. This means that in order to "add a month" the object must first be
238 // aligned to a month boundary, which is equivalent to the first day of that
239 // month.
240 //
241 // Of course, there are ways to compute an answer the question at hand using
242 // this civil-time library, but they require the programmer to be explicit
243 // about the answer they expect. To illustrate, let's see how to compute all
244 // three of the above possible answers to the question of "Jan 31 plus 1
245 // month":
246 //
247 //   const civil_day d(2015, 1, 31);
248 //
249 //   // Answer 1:
250 //   // Add 1 to the month field in the constructor, and rely on normalization.
251 //   const auto ans_normalized = civil_day(d.year(), d.month() + 1, d.day());
252 //   // ans_normalized == 2015-03-03 (aka Feb 31)
253 //
254 //   // Answer 2:
255 //   // Add 1 to month field, capping to the end of next month.
256 //   const auto next_month = civil_month(d) + 1;
257 //   const auto last_day_of_next_month = civil_day(next_month + 1) - 1;
258 //   const auto ans_capped = std::min(ans_normalized, last_day_of_next_month);
259 //   // ans_capped == 2015-02-28
260 //
261 //   // Answer 3:
262 //   // Signal an error if the normalized answer is not in next month.
263 //   if (civil_month(ans_normalized) != next_month) {
264 //     // error, month overflow
265 //   }
266 //
267 using civil_year = detail::civil_year;
268 using civil_month = detail::civil_month;
269 using civil_day = detail::civil_day;
270 using civil_hour = detail::civil_hour;
271 using civil_minute = detail::civil_minute;
272 using civil_second = detail::civil_second;
273 
274 // An enum class with members monday, tuesday, wednesday, thursday, friday,
275 // saturday, and sunday. These enum values may be sent to an output stream
276 // using operator<<(). The result is the full weekday name in English with a
277 // leading capital letter.
278 //
279 //   weekday wd = weekday::thursday;
280 //   std::cout << wd << "\n";  // Outputs: Thursday
281 //
282 using detail::weekday;
283 
284 // Returns the weekday for the given civil-time value.
285 //
286 //   civil_day a(2015, 8, 13);
287 //   weekday wd = get_weekday(a);  // wd == weekday::thursday
288 //
289 using detail::get_weekday;
290 
291 // Returns the civil_day that strictly follows or precedes the given
292 // civil_day, and that falls on the given weekday.
293 //
294 // For example, given:
295 //
296 //     August 2015
297 // Su Mo Tu We Th Fr Sa
298 //                    1
299 //  2  3  4  5  6  7  8
300 //  9 10 11 12 13 14 15
301 // 16 17 18 19 20 21 22
302 // 23 24 25 26 27 28 29
303 // 30 31
304 //
305 //   civil_day a(2015, 8, 13);  // get_weekday(a) == weekday::thursday
306 //   civil_day b = next_weekday(a, weekday::thursday);  // b = 2015-08-20
307 //   civil_day c = prev_weekday(a, weekday::thursday);  // c = 2015-08-06
308 //
309 //   civil_day d = ...
310 //   // Gets the following Thursday if d is not already Thursday
311 //   civil_day thurs1 = next_weekday(d - 1, weekday::thursday);
312 //   // Gets the previous Thursday if d is not already Thursday
313 //   civil_day thurs2 = prev_weekday(d + 1, weekday::thursday);
314 //
315 using detail::next_weekday;
316 using detail::prev_weekday;
317 
318 // Returns the day-of-year for the given civil-time value.
319 //
320 //   civil_day a(2015, 1, 1);
321 //   int yd_jan_1 = get_yearday(a);   // yd_jan_1 = 1
322 //   civil_day b(2015, 12, 31);
323 //   int yd_dec_31 = get_yearday(b);  // yd_dec_31 = 365
324 //
325 using detail::get_yearday;
326 
327 }  // namespace cctz
328 }  // namespace time_internal
329 ABSL_NAMESPACE_END
330 }  // namespace absl
331 
332 #endif  // ABSL_TIME_INTERNAL_CCTZ_CIVIL_TIME_H_
333