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