Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00028
00029
00030
00031 inline civil_year_t NormalizeYear(civil_year_t year) {
00032 return 2400 + year % 400;
00033 }
00034
00035
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
00041 return StrCat(cs.year(),
00042 FormatTime(std::string(fmt), FromCivil(ncs, utc), utc));
00043 }
00044
00045 }
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 }
00082
00083 }