Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/time/civil_time.h
Line
Count
Source
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
// -----------------------------------------------------------------------------
16
// File: civil_time.h
17
// -----------------------------------------------------------------------------
18
//
19
// This header file defines abstractions for computing with "civil time".
20
// The term "civil time" refers to the legally recognized human-scale time
21
// that is represented by the six fields `YYYY-MM-DD hh:mm:ss`. A "date"
22
// is perhaps the most common example of a civil time (represented here as
23
// an `absl::CivilDay`).
24
//
25
// Modern-day civil time follows the Gregorian Calendar and is a
26
// time-zone-independent concept: a civil time of "2015-06-01 12:00:00", for
27
// example, is not tied to a time zone. Put another way, a civil time does not
28
// map to a unique point in time; a civil time must be mapped to an absolute
29
// time *through* a time zone.
30
//
31
// Because a civil time is what most people think of as "time," it is common to
32
// map absolute times to civil times to present to users.
33
//
34
// Time zones define the relationship between absolute and civil times. Given an
35
// absolute or civil time and a time zone, you can compute the other time:
36
//
37
//   Civil Time = F(Absolute Time, Time Zone)
38
//   Absolute Time = G(Civil Time, Time Zone)
39
//
40
// The Abseil time library allows you to construct such civil times from
41
// absolute times; consult time.h for such functionality.
42
//
43
// This library provides six classes for constructing civil-time objects, and
44
// provides several helper functions for rounding, iterating, and performing
45
// arithmetic on civil-time objects, while avoiding complications like
46
// daylight-saving time (DST):
47
//
48
//   * `absl::CivilSecond`
49
//   * `absl::CivilMinute`
50
//   * `absl::CivilHour`
51
//   * `absl::CivilDay`
52
//   * `absl::CivilMonth`
53
//   * `absl::CivilYear`
54
//
55
// Example:
56
//
57
//   // Construct a civil-time object for a specific day
58
//   const absl::CivilDay cd(1969, 7, 20);
59
//
60
//   // Construct a civil-time object for a specific second
61
//   const absl::CivilSecond cd(2018, 8, 1, 12, 0, 1);
62
//
63
// Note: In C++14 and later, this library is usable in a constexpr context.
64
//
65
// Example:
66
//
67
//   // Valid in C++14
68
//   constexpr absl::CivilDay cd(1969, 7, 20);
69
70
#ifndef ABSL_TIME_CIVIL_TIME_H_
71
#define ABSL_TIME_CIVIL_TIME_H_
72
73
#include <iosfwd>
74
#include <string>
75
76
#include "absl/base/config.h"
77
#include "absl/strings/string_view.h"
78
#include "absl/time/internal/cctz/include/cctz/civil_time.h"
79
#include "absl/time/internal/cctz/include/cctz/civil_time_detail.h"
80
81
namespace absl {
82
ABSL_NAMESPACE_BEGIN
83
84
namespace time_internal {
85
struct second_tag : cctz::detail::second_tag {};
86
struct minute_tag : second_tag, cctz::detail::minute_tag {};
87
struct hour_tag : minute_tag, cctz::detail::hour_tag {};
88
struct day_tag : hour_tag, cctz::detail::day_tag {};
89
struct month_tag : day_tag, cctz::detail::month_tag {};
90
struct year_tag : month_tag, cctz::detail::year_tag {};
91
}  // namespace time_internal
92
93
// -----------------------------------------------------------------------------
94
// CivilSecond, CivilMinute, CivilHour, CivilDay, CivilMonth, CivilYear
95
// -----------------------------------------------------------------------------
96
//
97
// Each of these civil-time types is a simple value type with the same
98
// interface for construction and the same six accessors for each of the civil
99
// time fields (year, month, day, hour, minute, and second, aka YMDHMS). These
100
// classes differ only in their alignment, which is indicated by the type name
101
// and specifies the field on which arithmetic operates.
102
//
103
// CONSTRUCTION
104
//
105
// Each of the civil-time types can be constructed in two ways: by directly
106
// passing to the constructor up to six integers representing the YMDHMS fields,
107
// or by copying the YMDHMS fields from a differently aligned civil-time type.
108
// Omitted fields are assigned their minimum valid value. Hours, minutes, and
109
// seconds will be set to 0, month and day will be set to 1. Since there is no
110
// minimum year, the default is 1970.
111
//
112
// Examples:
113
//
114
//   absl::CivilDay default_value;               // 1970-01-01 00:00:00
115
//
116
//   absl::CivilDay a(2015, 2, 3);               // 2015-02-03 00:00:00
117
//   absl::CivilDay b(2015, 2, 3, 4, 5, 6);      // 2015-02-03 00:00:00
118
//   absl::CivilDay c(2015);                     // 2015-01-01 00:00:00
119
//
120
//   absl::CivilSecond ss(2015, 2, 3, 4, 5, 6);  // 2015-02-03 04:05:06
121
//   absl::CivilMinute mm(ss);                   // 2015-02-03 04:05:00
122
//   absl::CivilHour hh(mm);                     // 2015-02-03 04:00:00
123
//   absl::CivilDay d(hh);                       // 2015-02-03 00:00:00
124
//   absl::CivilMonth m(d);                      // 2015-02-01 00:00:00
125
//   absl::CivilYear y(m);                       // 2015-01-01 00:00:00
126
//
127
//   m = absl::CivilMonth(y);                    // 2015-01-01 00:00:00
128
//   d = absl::CivilDay(m);                      // 2015-01-01 00:00:00
129
//   hh = absl::CivilHour(d);                    // 2015-01-01 00:00:00
130
//   mm = absl::CivilMinute(hh);                 // 2015-01-01 00:00:00
131
//   ss = absl::CivilSecond(mm);                 // 2015-01-01 00:00:00
132
//
133
// Each civil-time class is aligned to the civil-time field indicated in the
134
// class's name after normalization. Alignment is performed by setting all the
135
// inferior fields to their minimum valid value (as described above). The
136
// following are examples of how each of the six types would align the fields
137
// representing November 22, 2015 at 12:34:56 in the afternoon. (Note: the
138
// string format used here is not important; it's just a shorthand way of
139
// showing the six YMDHMS fields.)
140
//
141
//   absl::CivilSecond   : 2015-11-22 12:34:56
142
//   absl::CivilMinute   : 2015-11-22 12:34:00
143
//   absl::CivilHour     : 2015-11-22 12:00:00
144
//   absl::CivilDay      : 2015-11-22 00:00:00
145
//   absl::CivilMonth    : 2015-11-01 00:00:00
146
//   absl::CivilYear     : 2015-01-01 00:00:00
147
//
148
// Each civil-time type performs arithmetic on the field to which it is
149
// aligned. This means that adding 1 to an absl::CivilDay increments the day
150
// field (normalizing as necessary), and subtracting 7 from an absl::CivilMonth
151
// operates on the month field (normalizing as necessary). All arithmetic
152
// produces a valid civil time. Difference requires two similarly aligned
153
// civil-time objects and returns the scalar answer in units of the objects'
154
// alignment. For example, the difference between two absl::CivilHour objects
155
// will give an answer in units of civil hours.
156
//
157
// ALIGNMENT CONVERSION
158
//
159
// The alignment of a civil-time object cannot change, but the object may be
160
// used to construct a new object with a different alignment. This is referred
161
// to as "realigning". When realigning to a type with the same or more
162
// precision (e.g., absl::CivilDay -> absl::CivilSecond), the conversion may be
163
// performed implicitly since no information is lost. However, if information
164
// could be discarded (e.g., CivilSecond -> CivilDay), the conversion must
165
// be explicit at the call site.
166
//
167
// Examples:
168
//
169
//   void UseDay(absl::CivilDay day);
170
//
171
//   absl::CivilSecond cs;
172
//   UseDay(cs);                  // Won't compile because data may be discarded
173
//   UseDay(absl::CivilDay(cs));  // OK: explicit conversion
174
//
175
//   absl::CivilDay cd;
176
//   UseDay(cd);                  // OK: no conversion needed
177
//
178
//   absl::CivilMonth cm;
179
//   UseDay(cm);                  // OK: implicit conversion to absl::CivilDay
180
//
181
// NORMALIZATION
182
//
183
// Normalization takes invalid values and adjusts them to produce valid values.
184
// Within the civil-time library, integer arguments passed to the Civil*
185
// constructors may be out-of-range, in which case they are normalized by
186
// carrying overflow into a field of courser granularity to produce valid
187
// civil-time objects. This normalization enables natural arithmetic on
188
// constructor arguments without worrying about the field's range.
189
//
190
// Examples:
191
//
192
//   // Out-of-range; normalized to 2016-11-01
193
//   absl::CivilDay d(2016, 10, 32);
194
//   // Out-of-range, negative: normalized to 2016-10-30T23
195
//   absl::CivilHour h1(2016, 10, 31, -1);
196
//   // Normalization is cumulative: normalized to 2016-10-30T23
197
//   absl::CivilHour h2(2016, 10, 32, -25);
198
//
199
// Note: If normalization is undesired, you can signal an error by comparing
200
// the constructor arguments to the normalized values returned by the YMDHMS
201
// properties.
202
//
203
// COMPARISON
204
//
205
// Comparison between civil-time objects considers all six YMDHMS fields,
206
// regardless of the type's alignment. Comparison between differently aligned
207
// civil-time types is allowed.
208
//
209
// Examples:
210
//
211
//   absl::CivilDay feb_3(2015, 2, 3);  // 2015-02-03 00:00:00
212
//   absl::CivilDay mar_4(2015, 3, 4);  // 2015-03-04 00:00:00
213
//   // feb_3 < mar_4
214
//   // absl::CivilYear(feb_3) == absl::CivilYear(mar_4)
215
//
216
//   absl::CivilSecond feb_3_noon(2015, 2, 3, 12, 0, 0);  // 2015-02-03 12:00:00
217
//   // feb_3 < feb_3_noon
218
//   // feb_3 == absl::CivilDay(feb_3_noon)
219
//
220
//   // Iterates all the days of February 2015.
221
//   for (absl::CivilDay d(2015, 2, 1); d < absl::CivilMonth(2015, 3); ++d) {
222
//     // ...
223
//   }
224
//
225
// ARITHMETIC
226
//
227
// Civil-time types support natural arithmetic operators such as addition,
228
// subtraction, and difference. Arithmetic operates on the civil-time field
229
// indicated in the type's name. Difference operators require arguments with
230
// the same alignment and return the answer in units of the alignment.
231
//
232
// Example:
233
//
234
//   absl::CivilDay a(2015, 2, 3);
235
//   ++a;                              // 2015-02-04 00:00:00
236
//   --a;                              // 2015-02-03 00:00:00
237
//   absl::CivilDay b = a + 1;         // 2015-02-04 00:00:00
238
//   absl::CivilDay c = 1 + b;         // 2015-02-05 00:00:00
239
//   int n = c - a;                    // n = 2 (civil days)
240
//   int m = c - absl::CivilMonth(c);  // Won't compile: different types.
241
//
242
// ACCESSORS
243
//
244
// Each civil-time type has accessors for all six of the civil-time fields:
245
// year, month, day, hour, minute, and second.
246
//
247
// civil_year_t year()
248
// int          month()
249
// int          day()
250
// int          hour()
251
// int          minute()
252
// int          second()
253
//
254
// Recall that fields inferior to the type's alignment will be set to their
255
// minimum valid value.
256
//
257
// Example:
258
//
259
//   absl::CivilDay d(2015, 6, 28);
260
//   // d.year() == 2015
261
//   // d.month() == 6
262
//   // d.day() == 28
263
//   // d.hour() == 0
264
//   // d.minute() == 0
265
//   // d.second() == 0
266
//
267
// CASE STUDY: Adding a month to January 31.
268
//
269
// One of the classic questions that arises when considering a civil time
270
// library (or a date library or a date/time library) is this:
271
//   "What is the result of adding a month to January 31?"
272
// This is an interesting question because it is unclear what is meant by a
273
// "month", and several different answers are possible, depending on context:
274
//
275
//   1. March 3 (or 2 if a leap year), if "add a month" means to add a month to
276
//      the current month, and adjust the date to overflow the extra days into
277
//      March. In this case the result of "February 31" would be normalized as
278
//      within the civil-time library.
279
//   2. February 28 (or 29 if a leap year), if "add a month" means to add a
280
//      month, and adjust the date while holding the resulting month constant.
281
//      In this case, the result of "February 31" would be truncated to the last
282
//      day in February.
283
//   3. An error. The caller may get some error, an exception, an invalid date
284
//      object, or perhaps return `false`. This may make sense because there is
285
//      no single unambiguously correct answer to the question.
286
//
287
// Practically speaking, any answer that is not what the programmer intended
288
// is the wrong answer.
289
//
290
// The Abseil time library avoids this problem by making it impossible to
291
// ask ambiguous questions. All civil-time objects are aligned to a particular
292
// civil-field boundary (such as aligned to a year, month, day, hour, minute,
293
// or second), and arithmetic operates on the field to which the object is
294
// aligned. This means that in order to "add a month" the object must first be
295
// aligned to a month boundary, which is equivalent to the first day of that
296
// month.
297
//
298
// Of course, there are ways to compute an answer the question at hand using
299
// this Abseil time library, but they require the programmer to be explicit
300
// about the answer they expect. To illustrate, let's see how to compute all
301
// three of the above possible answers to the question of "Jan 31 plus 1
302
// month":
303
//
304
// Example:
305
//
306
//   const absl::CivilDay d(2015, 1, 31);
307
//
308
//   // Answer 1:
309
//   // Add 1 to the month field in the constructor, and rely on normalization.
310
//   const auto normalized = absl::CivilDay(d.year(), d.month() + 1, d.day());
311
//   // normalized == 2015-03-03 (aka Feb 31)
312
//
313
//   // Answer 2:
314
//   // Add 1 to month field, capping to the end of next month.
315
//   const auto next_month = absl::CivilMonth(d) + 1;
316
//   const auto last_day_of_next_month = absl::CivilDay(next_month + 1) - 1;
317
//   const auto capped = std::min(normalized, last_day_of_next_month);
318
//   // capped == 2015-02-28
319
//
320
//   // Answer 3:
321
//   // Signal an error if the normalized answer is not in next month.
322
//   if (absl::CivilMonth(normalized) != next_month) {
323
//     // error, month overflow
324
//   }
325
//
326
using CivilSecond =
327
    time_internal::cctz::detail::civil_time<time_internal::second_tag>;
328
using CivilMinute =
329
    time_internal::cctz::detail::civil_time<time_internal::minute_tag>;
330
using CivilHour =
331
    time_internal::cctz::detail::civil_time<time_internal::hour_tag>;
332
using CivilDay =
333
    time_internal::cctz::detail::civil_time<time_internal::day_tag>;
334
using CivilMonth =
335
    time_internal::cctz::detail::civil_time<time_internal::month_tag>;
336
using CivilYear =
337
    time_internal::cctz::detail::civil_time<time_internal::year_tag>;
338
339
// civil_year_t
340
//
341
// Type alias of a civil-time year value. This type is guaranteed to (at least)
342
// support any year value supported by `time_t`.
343
//
344
// Example:
345
//
346
//   absl::CivilSecond cs = ...;
347
//   absl::civil_year_t y = cs.year();
348
//   cs = absl::CivilSecond(y, 1, 1, 0, 0, 0);  // CivilSecond(CivilYear(cs))
349
//
350
using civil_year_t = time_internal::cctz::year_t;
351
352
// civil_diff_t
353
//
354
// Type alias of the difference between two civil-time values.
355
// This type is used to indicate arguments that are not
356
// normalized (such as parameters to the civil-time constructors), the results
357
// of civil-time subtraction, or the operand to civil-time addition.
358
//
359
// Example:
360
//
361
//   absl::civil_diff_t n_sec = cs1 - cs2;             // cs1 == cs2 + n_sec;
362
//
363
using civil_diff_t = time_internal::cctz::diff_t;
364
365
// Weekday::monday, Weekday::tuesday, Weekday::wednesday, Weekday::thursday,
366
// Weekday::friday, Weekday::saturday, Weekday::sunday
367
//
368
// The Weekday enum class represents the civil-time concept of a "weekday" with
369
// members for all days of the week.
370
//
371
//   absl::Weekday wd = absl::Weekday::thursday;
372
//
373
using Weekday = time_internal::cctz::weekday;
374
375
// GetWeekday()
376
//
377
// Returns the absl::Weekday for the given (realigned) civil-time value.
378
//
379
// Example:
380
//
381
//   absl::CivilDay a(2015, 8, 13);
382
//   absl::Weekday wd = absl::GetWeekday(a);  // wd == absl::Weekday::thursday
383
//
384
0
inline Weekday GetWeekday(CivilSecond cs) {
385
0
  return time_internal::cctz::get_weekday(cs);
386
0
}
387
388
// NextWeekday()
389
// PrevWeekday()
390
//
391
// Returns the absl::CivilDay that strictly follows or precedes a given
392
// absl::CivilDay, and that falls on the given absl::Weekday.
393
//
394
// Example, given the following month:
395
//
396
//       August 2015
397
//   Su Mo Tu We Th Fr Sa
398
//                      1
399
//    2  3  4  5  6  7  8
400
//    9 10 11 12 13 14 15
401
//   16 17 18 19 20 21 22
402
//   23 24 25 26 27 28 29
403
//   30 31
404
//
405
//   absl::CivilDay a(2015, 8, 13);
406
//   // absl::GetWeekday(a) == absl::Weekday::thursday
407
//   absl::CivilDay b = absl::NextWeekday(a, absl::Weekday::thursday);
408
//   // b = 2015-08-20
409
//   absl::CivilDay c = absl::PrevWeekday(a, absl::Weekday::thursday);
410
//   // c = 2015-08-06
411
//
412
//   absl::CivilDay d = ...
413
//   // Gets the following Thursday if d is not already Thursday
414
//   absl::CivilDay thurs1 = absl::NextWeekday(d - 1, absl::Weekday::thursday);
415
//   // Gets the previous Thursday if d is not already Thursday
416
//   absl::CivilDay thurs2 = absl::PrevWeekday(d + 1, absl::Weekday::thursday);
417
//
418
0
inline CivilDay NextWeekday(CivilDay cd, Weekday wd) {
419
0
  return CivilDay(time_internal::cctz::next_weekday(cd, wd));
420
0
}
421
0
inline CivilDay PrevWeekday(CivilDay cd, Weekday wd) {
422
0
  return CivilDay(time_internal::cctz::prev_weekday(cd, wd));
423
0
}
424
425
// GetYearDay()
426
//
427
// Returns the day-of-year for the given (realigned) civil-time value.
428
//
429
// Example:
430
//
431
//   absl::CivilDay a(2015, 1, 1);
432
//   int yd_jan_1 = absl::GetYearDay(a);   // yd_jan_1 = 1
433
//   absl::CivilDay b(2015, 12, 31);
434
//   int yd_dec_31 = absl::GetYearDay(b);  // yd_dec_31 = 365
435
//
436
0
inline int GetYearDay(CivilSecond cs) {
437
0
  return time_internal::cctz::get_yearday(cs);
438
0
}
439
440
// FormatCivilTime()
441
//
442
// Formats the given civil-time value into a string value of the following
443
// format:
444
//
445
//  Type        | Format
446
//  ---------------------------------
447
//  CivilSecond | YYYY-MM-DDTHH:MM:SS
448
//  CivilMinute | YYYY-MM-DDTHH:MM
449
//  CivilHour   | YYYY-MM-DDTHH
450
//  CivilDay    | YYYY-MM-DD
451
//  CivilMonth  | YYYY-MM
452
//  CivilYear   | YYYY
453
//
454
// Example:
455
//
456
//   absl::CivilDay d = absl::CivilDay(1969, 7, 20);
457
//   std::string day_string = absl::FormatCivilTime(d);  // "1969-07-20"
458
//
459
std::string FormatCivilTime(CivilSecond c);
460
std::string FormatCivilTime(CivilMinute c);
461
std::string FormatCivilTime(CivilHour c);
462
std::string FormatCivilTime(CivilDay c);
463
std::string FormatCivilTime(CivilMonth c);
464
std::string FormatCivilTime(CivilYear c);
465
466
// absl::ParseCivilTime()
467
//
468
// Parses a civil-time value from the specified `absl::string_view` into the
469
// passed output parameter. Returns `true` upon successful parsing.
470
//
471
// The expected form of the input string is as follows:
472
//
473
//  Type        | Format
474
//  ---------------------------------
475
//  CivilSecond | YYYY-MM-DDTHH:MM:SS
476
//  CivilMinute | YYYY-MM-DDTHH:MM
477
//  CivilHour   | YYYY-MM-DDTHH
478
//  CivilDay    | YYYY-MM-DD
479
//  CivilMonth  | YYYY-MM
480
//  CivilYear   | YYYY
481
//
482
// Example:
483
//
484
//   absl::CivilDay d;
485
//   bool ok = absl::ParseCivilTime("2018-01-02", &d); // OK
486
//
487
// Parsing tolerates the following variations from the standard format:
488
// * Leading and trailing whitespace is ignored.
489
// * The year component may be negative (prefixed with '-') and may contain
490
//   an arbitrary number of digits.
491
// * Sub-year components (month, day, hour, minute, second) may consist of
492
//   either one or two digits.
493
//
494
// Note that parsing will fail if the string's format does not match the
495
// expected type exactly. `ParseLenientCivilTime()` below is more lenient.
496
//
497
bool ParseCivilTime(absl::string_view s, CivilSecond* c);
498
bool ParseCivilTime(absl::string_view s, CivilMinute* c);
499
bool ParseCivilTime(absl::string_view s, CivilHour* c);
500
bool ParseCivilTime(absl::string_view s, CivilDay* c);
501
bool ParseCivilTime(absl::string_view s, CivilMonth* c);
502
bool ParseCivilTime(absl::string_view s, CivilYear* c);
503
504
// ParseLenientCivilTime()
505
//
506
// Parses any of the formats accepted by `absl::ParseCivilTime()`. Unlike
507
// `ParseCivilTime()`, the input string format does not need to match the
508
// target civil-time type. Discrepancies are resolved as follows:
509
// * Extra components in the input string are ignored.
510
// * Missing components are defaulted to their minimum valid values.
511
// This behavior is consistent with civil-time converting constructors.
512
//
513
// Example:
514
//
515
//   absl::CivilDay d;
516
//   bool ok = absl::ParseLenientCivilTime("1969-07-20", &d); // OK
517
//   ok = absl::ParseLenientCivilTime("1969-07-20T10", &d);   // OK: T10 floored
518
//   ok = absl::ParseLenientCivilTime("1969-07", &d);   // OK: day defaults to 1
519
//
520
bool ParseLenientCivilTime(absl::string_view s, CivilSecond* c);
521
bool ParseLenientCivilTime(absl::string_view s, CivilMinute* c);
522
bool ParseLenientCivilTime(absl::string_view s, CivilHour* c);
523
bool ParseLenientCivilTime(absl::string_view s, CivilDay* c);
524
bool ParseLenientCivilTime(absl::string_view s, CivilMonth* c);
525
bool ParseLenientCivilTime(absl::string_view s, CivilYear* c);
526
527
namespace time_internal {  // For functions found via ADL on civil-time tags.
528
529
// Support for StrFormat(), StrCat(), etc
530
template <typename Sink>
531
void AbslStringify(Sink& sink, CivilSecond c) {
532
  sink.Append(FormatCivilTime(c));
533
}
534
template <typename Sink>
535
void AbslStringify(Sink& sink, CivilMinute c) {
536
  sink.Append(FormatCivilTime(c));
537
}
538
template <typename Sink>
539
void AbslStringify(Sink& sink, CivilHour c) {
540
  sink.Append(FormatCivilTime(c));
541
}
542
template <typename Sink>
543
void AbslStringify(Sink& sink, CivilDay c) {
544
  sink.Append(FormatCivilTime(c));
545
}
546
template <typename Sink>
547
void AbslStringify(Sink& sink, CivilMonth c) {
548
  sink.Append(FormatCivilTime(c));
549
}
550
template <typename Sink>
551
void AbslStringify(Sink& sink, CivilYear c) {
552
  sink.Append(FormatCivilTime(c));
553
}
554
555
// Streaming Operators
556
//
557
// Each civil-time type may be sent to an output stream using operator<<().
558
// The result matches the string produced by `FormatCivilTime()`.
559
//
560
// Example:
561
//
562
//   absl::CivilDay d = absl::CivilDay(1969, 7, 20);
563
//   std::cout << "Date is: " << d << "\n";
564
//
565
std::ostream& operator<<(std::ostream& os, CivilYear y);
566
std::ostream& operator<<(std::ostream& os, CivilMonth m);
567
std::ostream& operator<<(std::ostream& os, CivilDay d);
568
std::ostream& operator<<(std::ostream& os, CivilHour h);
569
std::ostream& operator<<(std::ostream& os, CivilMinute m);
570
std::ostream& operator<<(std::ostream& os, CivilSecond s);
571
572
// AbslParseFlag()
573
//
574
// Parses the command-line flag string representation `s` into a civil-time
575
// value. Flags must be specified in a format that is valid for
576
// `absl::ParseLenientCivilTime()`.
577
bool AbslParseFlag(absl::string_view s, CivilSecond* c, std::string* error);
578
bool AbslParseFlag(absl::string_view s, CivilMinute* c, std::string* error);
579
bool AbslParseFlag(absl::string_view s, CivilHour* c, std::string* error);
580
bool AbslParseFlag(absl::string_view s, CivilDay* c, std::string* error);
581
bool AbslParseFlag(absl::string_view s, CivilMonth* c, std::string* error);
582
bool AbslParseFlag(absl::string_view s, CivilYear* c, std::string* error);
583
584
// AbslUnparseFlag()
585
//
586
// Unparses a civil-time value into a command-line string representation using
587
// the format specified by `absl::ParseCivilTime()`.
588
std::string AbslUnparseFlag(CivilSecond c);
589
std::string AbslUnparseFlag(CivilMinute c);
590
std::string AbslUnparseFlag(CivilHour c);
591
std::string AbslUnparseFlag(CivilDay c);
592
std::string AbslUnparseFlag(CivilMonth c);
593
std::string AbslUnparseFlag(CivilYear c);
594
595
}  // namespace time_internal
596
597
ABSL_NAMESPACE_END
598
}  // namespace absl
599
600
#endif  // ABSL_TIME_CIVIL_TIME_H_