Make to_stream and from_stream return a stream reference

This commit is contained in:
Howard Hinnant 2017-06-17 13:11:39 -04:00
parent edcd9bd9c1
commit 012f2c6135
2 changed files with 81 additions and 71 deletions

113
date.h
View File

@ -3398,13 +3398,13 @@ template <class Duration>
struct fields;
template <class CharT, class Traits, class Duration>
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const fields<Duration>& fds, const std::string* abbrev = nullptr,
const std::chrono::seconds* offset_sec = nullptr);
template <class CharT, class Traits, class Duration, class Alloc>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
fields<Duration>& fds, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr);
@ -3950,14 +3950,14 @@ public:
template <class CharT, class Traits, class Duration>
friend
void
std::basic_ostream<CharT, Traits>&
date::to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const fields<Duration>& fds, const std::string* abbrev,
const std::chrono::seconds* offset_sec);
template <class CharT, class Traits, class Duration, class Alloc>
friend
void
std::basic_istream<CharT, Traits>&
date::from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
fields<Duration>& fds,
std::basic_string<CharT, Traits, Alloc>* abbrev, std::chrono::minutes* offset);
@ -4060,14 +4060,14 @@ public:
template <class CharT, class Traits, class Duration>
friend
void
std::basic_ostream<CharT, Traits>&
date::to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const fields<Duration>& fds, const std::string* abbrev,
const std::chrono::seconds* offset_sec);
template <class CharT, class Traits, class Duration, class Alloc>
friend
void
std::basic_istream<CharT, Traits>&
date::from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
fields<Duration>& fds,
std::basic_string<CharT, Traits, Alloc>* abbrev, std::chrono::minutes* offset);
@ -4242,7 +4242,7 @@ extract_weekday(std::basic_ostream<CharT, Traits>& os, const fields<Duration>& f
} // namespace detail
template <class CharT, class Traits, class Duration>
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const fields<Duration>& fds, const std::string* abbrev,
const std::chrono::seconds* offset_sec)
@ -4265,7 +4265,7 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
{
tm.tm_wday = static_cast<int>(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<CharT, Traits>& os, const CharT* fmt,
tm.tm_year = static_cast<int>(ymd.year()) - 1900;
tm.tm_wday = static_cast<int>(detail::extract_weekday(os, fds));
if (os.fail())
return;
return os;
tm.tm_yday = static_cast<int>((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<CharT, Traits>& 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<CharT, Traits>& os, const CharT* fmt,
tm.tm_year = static_cast<int>(ymd.year()) - 1900;
tm.tm_wday = static_cast<int>(detail::extract_weekday(os, fds));
if (os.fail())
return;
return os;
tm.tm_yday = static_cast<int>((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<CharT, Traits>& os, const CharT* fmt,
tm.tm_year = static_cast<int>(ymd.year()) - 1900;
tm.tm_wday = static_cast<int>(detail::extract_weekday(os, fds));
if (os.fail())
return;
return os;
tm.tm_yday = static_cast<int>((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<CharT, Traits>& 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<CharT, Traits>& os, const CharT* fmt,
tm.tm_year = static_cast<int>(ymd.year()) - 1900;
tm.tm_wday = static_cast<int>(detail::extract_weekday(os, fds));
if (os.fail())
return;
return os;
tm.tm_yday = static_cast<int>((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<CharT, Traits>& os, const CharT* fmt,
{
// Can not format %z with unknown offset
os.setstate(ios::failbit);
return;
return os;
}
auto m = duration_cast<minutes>(*offset_sec);
auto neg = m < minutes{0};
@ -5010,7 +5010,7 @@ to_stream(std::basic_ostream<CharT, Traits>& 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<CharT, Traits>& os, const CharT* fmt,
os << CharT{'%'};
if (modified != CharT{})
os << modified;
return os;
}
template <class CharT, class Traits>
inline
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, const year& y)
{
using CT = std::chrono::seconds;
fields<CT> fds{y/0/0};
to_stream(os, fmt, fds);
return to_stream(os, fmt, fds);
}
template <class CharT, class Traits>
inline
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, const month& m)
{
using CT = std::chrono::seconds;
fields<CT> fds{m/0/0};
to_stream(os, fmt, fds);
return to_stream(os, fmt, fds);
}
template <class CharT, class Traits>
inline
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, const day& d)
{
using CT = std::chrono::seconds;
fields<CT> fds{d/0/0};
to_stream(os, fmt, fds);
return to_stream(os, fmt, fds);
}
template <class CharT, class Traits>
inline
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, const weekday& wd)
{
using CT = std::chrono::seconds;
fields<CT> fds{wd};
to_stream(os, fmt, fds);
return to_stream(os, fmt, fds);
}
template <class CharT, class Traits>
inline
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, const year_month& ym)
{
using CT = std::chrono::seconds;
fields<CT> fds{ym/0};
to_stream(os, fmt, fds);
return to_stream(os, fmt, fds);
}
template <class CharT, class Traits>
inline
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, const month_day& md)
{
using CT = std::chrono::seconds;
fields<CT> fds{md/0};
to_stream(os, fmt, fds);
return to_stream(os, fmt, fds);
}
template <class CharT, class Traits>
inline
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const year_month_day& ymd)
{
using CT = std::chrono::seconds;
fields<CT> fds{ymd};
to_stream(os, fmt, fds);
return to_stream(os, fmt, fds);
}
template <class CharT, class Traits, class Rep, class Period>
inline
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const std::chrono::duration<Rep, Period>& d)
{
using Duration = std::chrono::duration<Rep, Period>;
using CT = typename std::common_type<Duration, std::chrono::seconds>::type;
fields<CT> fds{time_of_day<CT>{d}};
to_stream(os, fmt, fds);
return to_stream(os, fmt, fds);
}
template <class CharT, class Traits, class Duration>
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const local_time<Duration>& tp, const std::string* abbrev = nullptr,
const std::chrono::seconds* offset_sec = nullptr)
@ -5174,11 +5175,11 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
using CT = typename std::common_type<Duration, std::chrono::seconds>::type;
auto ld = floor<days>(tp);
fields<CT> fds{year_month_day{ld}, time_of_day<CT>{tp-ld}};
to_stream(os, fmt, fds, abbrev, offset_sec);
return to_stream(os, fmt, fds, abbrev, offset_sec);
}
template <class CharT, class Traits, class Duration>
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const sys_time<Duration>& tp)
{
@ -5187,7 +5188,7 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
CONSTDATA std::chrono::seconds offset{0};
auto sd = floor<days>(tp);
fields<CT> fds{year_month_day{sd}, time_of_day<CT>{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<CharT, Traits>& is, rld a0, Args&& ...args)
} // namespace detail;
template <class CharT, class Traits, class Duration, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
fields<Duration>& fds, std::basic_string<CharT, Traits, Alloc>* abbrev,
std::chrono::minutes* offset)
@ -6393,14 +6394,15 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
if (offset != nullptr)
*offset = temp_offset;
}
return;
return is;
}
broken:
is.setstate(ios_base::failbit);
return is;
}
template <class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, year& y,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6414,10 +6416,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, year& y,
is.setstate(ios::failbit);
if (!is.fail())
y = fds.ymd.year();
return is;
}
template <class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, month& m,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6431,10 +6434,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, month& m,
is.setstate(ios::failbit);
if (!is.fail())
m = fds.ymd.month();
return is;
}
template <class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, day& d,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6448,10 +6452,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, day& d,
is.setstate(ios::failbit);
if (!is.fail())
d = fds.ymd.day();
return is;
}
template <class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, weekday& wd,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6465,10 +6470,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, weekday& wd
is.setstate(ios::failbit);
if (!is.fail())
wd = fds.wd;
return is;
}
template <class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, year_month& ym,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6482,10 +6488,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, year_month&
is.setstate(ios::failbit);
if (!is.fail())
ym = fds.ymd.year()/fds.ymd.month();
return is;
}
template <class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, month_day& md,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6499,10 +6506,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, month_day&
is.setstate(ios::failbit);
if (!is.fail())
md = fds.ymd.month()/fds.ymd.day();
return is;
}
template <class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
year_month_day& ymd, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6516,10 +6524,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
is.setstate(ios::failbit);
if (!is.fail())
ymd = fds.ymd;
return is;
}
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
sys_time<Duration>& tp, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6535,10 +6544,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
is.setstate(ios::failbit);
if (!is.fail())
tp = sys_days(fds.ymd) + duration_cast<Duration>(fds.tod.to_duration() - *offptr);
return is;
}
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
local_time<Duration>& tp, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -6552,10 +6562,11 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
is.setstate(ios::failbit);
if (!is.fail())
tp = local_days(fds.ymd) + duration_cast<Duration>(fds.tod.to_duration());
return is;
}
template <class Rep, class Period, class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
std::chrono::duration<Rep, Period>& d,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
@ -6569,6 +6580,7 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
from_stream(is, fmt, fds, abbrev, offset);
if (!is.fail())
d = duration_cast<Duration>(fds.tod.to_duration());
return is;
}
template <class Parsable, class CharT, class Traits = std::char_traits<CharT>,
@ -6597,8 +6609,7 @@ std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is,
const parse_manip<Parsable, CharT, Traits, Alloc>& 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 <class Parsable, class CharT, class Traits, class Alloc>
@ -7344,7 +7355,7 @@ struct make_string<wchar_t, Traits>
{
template <class Rep>
static
std::wstring
std::basic_string<wchar_t, Traits>
from(Rep n)
{
auto s = std::to_wstring(n);

39
tz.h
View File

@ -1109,12 +1109,12 @@ make_zoned(const std::string& name, const sys_time<Duration>& st)
}
template <class CharT, class Traits, class Duration>
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const zoned_time<Duration>& 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 <class CharT, class Traits, class Duration>
@ -1122,8 +1122,7 @@ inline
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const zoned_time<Duration>& 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 <class CharT, class Traits, class Duration>
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const utc_time<Duration>& t)
{
@ -1225,19 +1224,18 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
auto time = make_time(tp - sd);
time.seconds() += seconds{ls.first};
fields<CT> fds{ymd, time};
to_stream(os, fmt, fds, &abbrev, &offset);
return to_stream(os, fmt, fds, &abbrev, &offset);
}
template <class CharT, class Traits, class Duration>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const utc_time<Duration>& t)
{
to_stream(os, "%F %T", t);
return os;
return to_stream(os, "%F %T", t);
}
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
utc_time<Duration>& tp, std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
@ -1262,10 +1260,11 @@ from_stream(std::basic_istream<CharT, Traits>& 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<Duration>(tmp);
}
return is;
}
// tai_clock
@ -1326,7 +1325,7 @@ tai_clock::now() NOEXCEPT
}
template <class CharT, class Traits, class Duration>
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const tai_time<Duration>& t)
{
@ -1341,19 +1340,18 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
year_month_day ymd = sd;
auto time = make_time(tp - sd);
fields<CT> fds{ymd, time};
to_stream(os, fmt, fds, &abbrev, &offset);
return to_stream(os, fmt, fds, &abbrev, &offset);
}
template <class CharT, class Traits, class Duration>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const tai_time<Duration>& t)
{
to_stream(os, "%F %T", t);
return os;
return to_stream(os, "%F %T", t);
}
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
tai_time<Duration>& tp,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
@ -1372,6 +1370,7 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
tp = tai_time<Duration>{duration_cast<Duration>(
(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 <class CharT, class Traits, class Duration>
void
std::basic_ostream<CharT, Traits>&
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const gps_time<Duration>& t)
{
@ -1447,19 +1446,18 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
year_month_day ymd = sd;
auto time = make_time(tp - sd);
fields<CT> fds{ymd, time};
to_stream(os, fmt, fds, &abbrev, &offset);
return to_stream(os, fmt, fds, &abbrev, &offset);
}
template <class CharT, class Traits, class Duration>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const gps_time<Duration>& t)
{
to_stream(os, "%F %T", t);
return os;
return to_stream(os, detail::make_string<CharT, Traits>::from("%F %T").c_str(), t);
}
template <class Duration, class CharT, class Traits, class Alloc = std::allocator<CharT>>
void
std::basic_istream<CharT, Traits>&
from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
gps_time<Duration>& tp,
std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr,
@ -1479,6 +1477,7 @@ from_stream(std::basic_istream<CharT, Traits>& 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 <class Duration>