| 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 | #include "absl/time/civil_time.h" |
| 16 | |
| 17 | #include <cstdlib> |
| 18 | #include <string> |
| 19 | |
| 20 | #include "absl/strings/str_cat.h" |
| 21 | #include "absl/time/time.h" |
| 22 | |
| 23 | namespace absl { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // Since a civil time has a larger year range than absl::Time (64-bit years vs |
| 28 | // 64-bit seconds, respectively) we normalize years to roughly +/- 400 years |
| 29 | // around the year 2400, which will produce an equivalent year in a range that |
| 30 | // absl::Time can handle. |
| 31 | inline civil_year_t NormalizeYear(civil_year_t year) { |
| 32 | return 2400 + year % 400; |
| 33 | } |
| 34 | |
| 35 | // Formats the given CivilSecond according to the given format. |
| 36 | std::string FormatYearAnd(string_view fmt, CivilSecond cs) { |
| 37 | const CivilSecond ncs(NormalizeYear(cs.year()), cs.month(), cs.day(), |
| 38 | cs.hour(), cs.minute(), cs.second()); |
| 39 | const TimeZone utc = UTCTimeZone(); |
| 40 | // TODO(absl-team): Avoid conversion of fmt std::string. |
| 41 | return StrCat(cs.year(), |
| 42 | FormatTime(std::string(fmt), FromCivil(ncs, utc), utc)); |
| 43 | } |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | std::string FormatCivilTime(CivilSecond c) { |
| 48 | return FormatYearAnd("-%m-%dT%H:%M:%S" , c); |
| 49 | } |
| 50 | std::string FormatCivilTime(CivilMinute c) { |
| 51 | return FormatYearAnd("-%m-%dT%H:%M" , c); |
| 52 | } |
| 53 | std::string FormatCivilTime(CivilHour c) { |
| 54 | return FormatYearAnd("-%m-%dT%H" , c); |
| 55 | } |
| 56 | std::string FormatCivilTime(CivilDay c) { return FormatYearAnd("-%m-%d" , c); } |
| 57 | std::string FormatCivilTime(CivilMonth c) { return FormatYearAnd("-%m" , c); } |
| 58 | std::string FormatCivilTime(CivilYear c) { return FormatYearAnd("" , c); } |
| 59 | |
| 60 | namespace time_internal { |
| 61 | |
| 62 | std::ostream& operator<<(std::ostream& os, CivilYear y) { |
| 63 | return os << FormatCivilTime(y); |
| 64 | } |
| 65 | std::ostream& operator<<(std::ostream& os, CivilMonth m) { |
| 66 | return os << FormatCivilTime(m); |
| 67 | } |
| 68 | std::ostream& operator<<(std::ostream& os, CivilDay d) { |
| 69 | return os << FormatCivilTime(d); |
| 70 | } |
| 71 | std::ostream& operator<<(std::ostream& os, CivilHour h) { |
| 72 | return os << FormatCivilTime(h); |
| 73 | } |
| 74 | std::ostream& operator<<(std::ostream& os, CivilMinute m) { |
| 75 | return os << FormatCivilTime(m); |
| 76 | } |
| 77 | std::ostream& operator<<(std::ostream& os, CivilSecond s) { |
| 78 | return os << FormatCivilTime(s); |
| 79 | } |
| 80 | |
| 81 | } // namespace time_internal |
| 82 | |
| 83 | } // namespace absl |
| 84 | |