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