From 012f2c613582b97aef01478737055f037239fb9e Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Sat, 17 Jun 2017 13:11:39 -0400 Subject: [PATCH] Make to_stream and from_stream return a stream reference --- date.h | 113 +++++++++++++++++++++++++++++++-------------------------- tz.h | 39 ++++++++++---------- 2 files changed, 81 insertions(+), 71 deletions(-) diff --git a/date.h b/date.h index 073cf0f..e0f5cef 100644 --- a/date.h +++ b/date.h @@ -3398,13 +3398,13 @@ template struct fields; template -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const fields& fds, const std::string* abbrev = nullptr, const std::chrono::seconds* offset_sec = nullptr); template -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, fields& fds, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr); @@ -3950,14 +3950,14 @@ public: template friend - void + std::basic_ostream& date::to_stream(std::basic_ostream& os, const CharT* fmt, const fields& fds, const std::string* abbrev, const std::chrono::seconds* offset_sec); template friend - void + std::basic_istream& date::from_stream(std::basic_istream& is, const CharT* fmt, fields& fds, std::basic_string* abbrev, std::chrono::minutes* offset); @@ -4060,14 +4060,14 @@ public: template friend - void + std::basic_ostream& date::to_stream(std::basic_ostream& os, const CharT* fmt, const fields& fds, const std::string* abbrev, const std::chrono::seconds* offset_sec); template friend - void + std::basic_istream& date::from_stream(std::basic_istream& is, const CharT* fmt, fields& fds, std::basic_string* abbrev, std::chrono::minutes* offset); @@ -4242,7 +4242,7 @@ extract_weekday(std::basic_ostream& os, const fields& f } // namespace detail template -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const fields& fds, const std::string* abbrev, const std::chrono::seconds* offset_sec) @@ -4265,7 +4265,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, { tm.tm_wday = static_cast(detail::extract_weekday(os, fds)); if (os.fail()) - return; + return os; const CharT f[] = {'%', *fmt}; facet.put(os, os, os.fill(), &tm, begin(f), end(f)); } @@ -4319,7 +4319,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, tm.tm_year = static_cast(ymd.year()) - 1900; tm.tm_wday = static_cast(detail::extract_weekday(os, fds)); if (os.fail()) - return; + return os; tm.tm_yday = static_cast((ld - local_days(ymd.year()/1/1)).count()); CharT f[3] = {'%'}; auto fe = begin(f) + 1; @@ -4730,7 +4730,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, { auto wd = detail::extract_weekday(os, fds); if (os.fail()) - return; + return os; if (modified == CharT{'O'}) { const CharT f[] = {'%', modified, *fmt}; @@ -4763,7 +4763,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, tm.tm_year = static_cast(ymd.year()) - 1900; tm.tm_wday = static_cast(detail::extract_weekday(os, fds)); if (os.fail()) - return; + return os; tm.tm_yday = static_cast((ld - local_days(ymd.year()/1/1)).count()); facet.put(os, os, os.fill(), &tm, begin(f), end(f)); modified = CharT{}; @@ -4802,7 +4802,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, tm.tm_year = static_cast(ymd.year()) - 1900; tm.tm_wday = static_cast(detail::extract_weekday(os, fds)); if (os.fail()) - return; + return os; tm.tm_yday = static_cast((ld - local_days(ymd.year()/1/1)).count()); facet.put(os, os, os.fill(), &tm, begin(f), end(f)); modified = CharT{}; @@ -4836,7 +4836,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, { auto wd = detail::extract_weekday(os, fds); if (os.fail()) - return; + return os; if (modified == CharT{'O'}) { const CharT f[] = {'%', modified, *fmt}; @@ -4869,7 +4869,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, tm.tm_year = static_cast(ymd.year()) - 1900; tm.tm_wday = static_cast(detail::extract_weekday(os, fds)); if (os.fail()) - return; + return os; tm.tm_yday = static_cast((ld - local_days(ymd.year()/1/1)).count()); facet.put(os, os, os.fill(), &tm, begin(f), end(f)); modified = CharT{}; @@ -4976,7 +4976,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, { // Can not format %z with unknown offset os.setstate(ios::failbit); - return; + return os; } auto m = duration_cast(*offset_sec); auto neg = m < minutes{0}; @@ -5010,7 +5010,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, { // Can not format %Z with unknown time_zone os.setstate(ios::failbit); - return; + return os; } for (auto c : *abbrev) os << CharT(c); @@ -5080,93 +5080,94 @@ to_stream(std::basic_ostream& os, const CharT* fmt, os << CharT{'%'}; if (modified != CharT{}) os << modified; + return os; } template inline -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const year& y) { using CT = std::chrono::seconds; fields fds{y/0/0}; - to_stream(os, fmt, fds); + return to_stream(os, fmt, fds); } template inline -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const month& m) { using CT = std::chrono::seconds; fields fds{m/0/0}; - to_stream(os, fmt, fds); + return to_stream(os, fmt, fds); } template inline -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const day& d) { using CT = std::chrono::seconds; fields fds{d/0/0}; - to_stream(os, fmt, fds); + return to_stream(os, fmt, fds); } template inline -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const weekday& wd) { using CT = std::chrono::seconds; fields fds{wd}; - to_stream(os, fmt, fds); + return to_stream(os, fmt, fds); } template inline -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const year_month& ym) { using CT = std::chrono::seconds; fields fds{ym/0}; - to_stream(os, fmt, fds); + return to_stream(os, fmt, fds); } template inline -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const month_day& md) { using CT = std::chrono::seconds; fields fds{md/0}; - to_stream(os, fmt, fds); + return to_stream(os, fmt, fds); } template inline -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const year_month_day& ymd) { using CT = std::chrono::seconds; fields fds{ymd}; - to_stream(os, fmt, fds); + return to_stream(os, fmt, fds); } template inline -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const std::chrono::duration& d) { using Duration = std::chrono::duration; using CT = typename std::common_type::type; fields fds{time_of_day{d}}; - to_stream(os, fmt, fds); + return to_stream(os, fmt, fds); } template -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const local_time& tp, const std::string* abbrev = nullptr, const std::chrono::seconds* offset_sec = nullptr) @@ -5174,11 +5175,11 @@ to_stream(std::basic_ostream& os, const CharT* fmt, using CT = typename std::common_type::type; auto ld = floor(tp); fields fds{year_month_day{ld}, time_of_day{tp-ld}}; - to_stream(os, fmt, fds, abbrev, offset_sec); + return to_stream(os, fmt, fds, abbrev, offset_sec); } template -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const sys_time& tp) { @@ -5187,7 +5188,7 @@ to_stream(std::basic_ostream& os, const CharT* fmt, CONSTDATA std::chrono::seconds offset{0}; auto sd = floor(tp); fields fds{year_month_day{sd}, time_of_day{tp-sd}}; - to_stream(os, fmt, fds, &abbrev, &offset); + return to_stream(os, fmt, fds, &abbrev, &offset); } // format @@ -5474,7 +5475,7 @@ read(std::basic_istream& is, rld a0, Args&& ...args) } // namespace detail; template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, fields& fds, std::basic_string* abbrev, std::chrono::minutes* offset) @@ -6393,14 +6394,15 @@ from_stream(std::basic_istream& is, const CharT* fmt, if (offset != nullptr) *offset = temp_offset; } - return; + return is; } broken: is.setstate(ios_base::failbit); + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, year& y, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6414,10 +6416,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, year& y, is.setstate(ios::failbit); if (!is.fail()) y = fds.ymd.year(); + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, month& m, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6431,10 +6434,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, month& m, is.setstate(ios::failbit); if (!is.fail()) m = fds.ymd.month(); + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, day& d, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6448,10 +6452,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, day& d, is.setstate(ios::failbit); if (!is.fail()) d = fds.ymd.day(); + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, weekday& wd, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6465,10 +6470,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, weekday& wd is.setstate(ios::failbit); if (!is.fail()) wd = fds.wd; + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, year_month& ym, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6482,10 +6488,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, year_month& is.setstate(ios::failbit); if (!is.fail()) ym = fds.ymd.year()/fds.ymd.month(); + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, month_day& md, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6499,10 +6506,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, month_day& is.setstate(ios::failbit); if (!is.fail()) md = fds.ymd.month()/fds.ymd.day(); + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, year_month_day& ymd, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6516,10 +6524,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, is.setstate(ios::failbit); if (!is.fail()) ymd = fds.ymd; + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, sys_time& tp, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6535,10 +6544,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, is.setstate(ios::failbit); if (!is.fail()) tp = sys_days(fds.ymd) + duration_cast(fds.tod.to_duration() - *offptr); + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, local_time& tp, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -6552,10 +6562,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, is.setstate(ios::failbit); if (!is.fail()) tp = local_days(fds.ymd) + duration_cast(fds.tod.to_duration()); + return is; } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, std::chrono::duration& d, std::basic_string* abbrev = nullptr, @@ -6569,6 +6580,7 @@ from_stream(std::basic_istream& is, const CharT* fmt, from_stream(is, fmt, fds, abbrev, offset); if (!is.fail()) d = duration_cast(fds.tod.to_duration()); + return is; } template , @@ -6597,8 +6609,7 @@ std::basic_istream& operator>>(std::basic_istream& is, const parse_manip& x) { - from_stream(is, x.format_.c_str(), x.tp_, x.abbrev_, x.offset_); - return is; + return from_stream(is, x.format_.c_str(), x.tp_, x.abbrev_, x.offset_); } template @@ -7344,7 +7355,7 @@ struct make_string { template static - std::wstring + std::basic_string from(Rep n) { auto s = std::to_wstring(n); diff --git a/tz.h b/tz.h index 4d0789f..c1c261f 100644 --- a/tz.h +++ b/tz.h @@ -1109,12 +1109,12 @@ make_zoned(const std::string& name, const sys_time& st) } template -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const zoned_time& tp) { auto const info = tp.get_info(); - to_stream(os, fmt, tp.get_local_time(), &info.abbrev, &info.offset); + return to_stream(os, fmt, tp.get_local_time(), &info.abbrev, &info.offset); } template @@ -1122,8 +1122,7 @@ inline std::basic_ostream& operator<<(std::basic_ostream& os, const zoned_time& t) { - to_stream(os, "%F %T %Z", t); - return os; + return to_stream(os, "%F %T %Z", t); } #if !MISSING_LEAP_SECONDS @@ -1209,7 +1208,7 @@ utc_clock::now() } template -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const utc_time& t) { @@ -1225,19 +1224,18 @@ to_stream(std::basic_ostream& os, const CharT* fmt, auto time = make_time(tp - sd); time.seconds() += seconds{ls.first}; fields fds{ymd, time}; - to_stream(os, fmt, fds, &abbrev, &offset); + return to_stream(os, fmt, fds, &abbrev, &offset); } template std::basic_ostream& operator<<(std::basic_ostream& os, const utc_time& t) { - to_stream(os, "%F %T", t); - return os; + return to_stream(os, "%F %T", t); } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, utc_time& tp, std::basic_string* abbrev = nullptr, std::chrono::minutes* offset = nullptr) @@ -1262,10 +1260,11 @@ from_stream(std::basic_istream& is, const CharT* fmt, if (is_60_sec != is_leap_second(tmp).first || !fds.tod.in_conventional_range()) { is.setstate(ios::failbit); - return; + return is; } tp = time_point_cast(tmp); } + return is; } // tai_clock @@ -1326,7 +1325,7 @@ tai_clock::now() NOEXCEPT } template -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const tai_time& t) { @@ -1341,19 +1340,18 @@ to_stream(std::basic_ostream& os, const CharT* fmt, year_month_day ymd = sd; auto time = make_time(tp - sd); fields fds{ymd, time}; - to_stream(os, fmt, fds, &abbrev, &offset); + return to_stream(os, fmt, fds, &abbrev, &offset); } template std::basic_ostream& operator<<(std::basic_ostream& os, const tai_time& t) { - to_stream(os, "%F %T", t); - return os; + return to_stream(os, "%F %T", t); } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, tai_time& tp, std::basic_string* abbrev = nullptr, @@ -1372,6 +1370,7 @@ from_stream(std::basic_istream& is, const CharT* fmt, tp = tai_time{duration_cast( (sys_days(fds.ymd) + fds.tod.to_duration() + (sys_days(year{1970}/jan/1) - sys_days(year{1958}/jan/1)) - *offptr).time_since_epoch())}; + return is; } // gps_clock @@ -1432,7 +1431,7 @@ gps_clock::now() NOEXCEPT } template -void +std::basic_ostream& to_stream(std::basic_ostream& os, const CharT* fmt, const gps_time& t) { @@ -1447,19 +1446,18 @@ to_stream(std::basic_ostream& os, const CharT* fmt, year_month_day ymd = sd; auto time = make_time(tp - sd); fields fds{ymd, time}; - to_stream(os, fmt, fds, &abbrev, &offset); + return to_stream(os, fmt, fds, &abbrev, &offset); } template std::basic_ostream& operator<<(std::basic_ostream& os, const gps_time& t) { - to_stream(os, "%F %T", t); - return os; + return to_stream(os, detail::make_string::from("%F %T").c_str(), t); } template > -void +std::basic_istream& from_stream(std::basic_istream& is, const CharT* fmt, gps_time& tp, std::basic_string* abbrev = nullptr, @@ -1479,6 +1477,7 @@ from_stream(std::basic_istream& is, const CharT* fmt, (sys_days(fds.ymd) + fds.tod.to_duration() - (sys_days(year{1980}/jan/sun[1]) - sys_days(year{1970}/jan/1)) - *offptr).time_since_epoch())}; + return is; } template