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 | #include "absl/time/internal/cctz/include/cctz/civil_time_detail.h" |
16 | |
17 | #include <iomanip> |
18 | #include <ostream> |
19 | #include <sstream> |
20 | |
21 | namespace absl { |
22 | namespace time_internal { |
23 | namespace cctz { |
24 | namespace detail { |
25 | |
26 | // Output stream operators output a format matching YYYY-MM-DDThh:mm:ss, |
27 | // while omitting fields inferior to the type's alignment. For example, |
28 | // civil_day is formatted only as YYYY-MM-DD. |
29 | std::ostream& operator<<(std::ostream& os, const civil_year& y) { |
30 | std::stringstream ss; |
31 | ss << y.year(); // No padding. |
32 | return os << ss.str(); |
33 | } |
34 | std::ostream& operator<<(std::ostream& os, const civil_month& m) { |
35 | std::stringstream ss; |
36 | ss << civil_year(m) << '-'; |
37 | ss << std::setfill('0') << std::setw(2) << m.month(); |
38 | return os << ss.str(); |
39 | } |
40 | std::ostream& operator<<(std::ostream& os, const civil_day& d) { |
41 | std::stringstream ss; |
42 | ss << civil_month(d) << '-'; |
43 | ss << std::setfill('0') << std::setw(2) << d.day(); |
44 | return os << ss.str(); |
45 | } |
46 | std::ostream& operator<<(std::ostream& os, const civil_hour& h) { |
47 | std::stringstream ss; |
48 | ss << civil_day(h) << 'T'; |
49 | ss << std::setfill('0') << std::setw(2) << h.hour(); |
50 | return os << ss.str(); |
51 | } |
52 | std::ostream& operator<<(std::ostream& os, const civil_minute& m) { |
53 | std::stringstream ss; |
54 | ss << civil_hour(m) << ':'; |
55 | ss << std::setfill('0') << std::setw(2) << m.minute(); |
56 | return os << ss.str(); |
57 | } |
58 | std::ostream& operator<<(std::ostream& os, const civil_second& s) { |
59 | std::stringstream ss; |
60 | ss << civil_minute(s) << ':'; |
61 | ss << std::setfill('0') << std::setw(2) << s.second(); |
62 | return os << ss.str(); |
63 | } |
64 | |
65 | //////////////////////////////////////////////////////////////////////// |
66 | |
67 | std::ostream& operator<<(std::ostream& os, weekday wd) { |
68 | switch (wd) { |
69 | case weekday::monday: |
70 | return os << "Monday" ; |
71 | case weekday::tuesday: |
72 | return os << "Tuesday" ; |
73 | case weekday::wednesday: |
74 | return os << "Wednesday" ; |
75 | case weekday::thursday: |
76 | return os << "Thursday" ; |
77 | case weekday::friday: |
78 | return os << "Friday" ; |
79 | case weekday::saturday: |
80 | return os << "Saturday" ; |
81 | case weekday::sunday: |
82 | return os << "Sunday" ; |
83 | } |
84 | return os; // Should never get here, but -Wreturn-type may warn without this. |
85 | } |
86 | |
87 | } // namespace detail |
88 | } // namespace cctz |
89 | } // namespace time_internal |
90 | } // namespace absl |
91 | |