civil_time.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/time/civil_time.h"
00016 
00017 #include <cstdlib>
00018 #include <string>
00019 
00020 #include "absl/strings/str_cat.h"
00021 #include "absl/time/time.h"
00022 
00023 namespace absl {
00024 
00025 namespace {
00026 
00027 // Since a civil time has a larger year range than absl::Time (64-bit years vs
00028 // 64-bit seconds, respectively) we normalize years to roughly +/- 400 years
00029 // around the year 2400, which will produce an equivalent year in a range that
00030 // absl::Time can handle.
00031 inline civil_year_t NormalizeYear(civil_year_t year) {
00032   return 2400 + year % 400;
00033 }
00034 
00035 // Formats the given CivilSecond according to the given format.
00036 std::string FormatYearAnd(string_view fmt, CivilSecond cs) {
00037   const CivilSecond ncs(NormalizeYear(cs.year()), cs.month(), cs.day(),
00038                         cs.hour(), cs.minute(), cs.second());
00039   const TimeZone utc = UTCTimeZone();
00040   // TODO(absl-team): Avoid conversion of fmt std::string.
00041   return StrCat(cs.year(),
00042                 FormatTime(std::string(fmt), FromCivil(ncs, utc), utc));
00043 }
00044 
00045 }  // namespace
00046 
00047 std::string FormatCivilTime(CivilSecond c) {
00048   return FormatYearAnd("-%m-%dT%H:%M:%S", c);
00049 }
00050 std::string FormatCivilTime(CivilMinute c) {
00051   return FormatYearAnd("-%m-%dT%H:%M", c);
00052 }
00053 std::string FormatCivilTime(CivilHour c) {
00054   return FormatYearAnd("-%m-%dT%H", c);
00055 }
00056 std::string FormatCivilTime(CivilDay c) { return FormatYearAnd("-%m-%d", c); }
00057 std::string FormatCivilTime(CivilMonth c) { return FormatYearAnd("-%m", c); }
00058 std::string FormatCivilTime(CivilYear c) { return FormatYearAnd("", c); }
00059 
00060 namespace time_internal {
00061 
00062 std::ostream& operator<<(std::ostream& os, CivilYear y) {
00063   return os << FormatCivilTime(y);
00064 }
00065 std::ostream& operator<<(std::ostream& os, CivilMonth m) {
00066   return os << FormatCivilTime(m);
00067 }
00068 std::ostream& operator<<(std::ostream& os, CivilDay d) {
00069   return os << FormatCivilTime(d);
00070 }
00071 std::ostream& operator<<(std::ostream& os, CivilHour h) {
00072   return os << FormatCivilTime(h);
00073 }
00074 std::ostream& operator<<(std::ostream& os, CivilMinute m) {
00075   return os << FormatCivilTime(m);
00076 }
00077 std::ostream& operator<<(std::ostream& os, CivilSecond s) {
00078   return os << FormatCivilTime(s);
00079 }
00080 
00081 }  // namespace time_internal
00082 
00083 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14