Support wide streams

This commit is contained in:
Howard Hinnant 2016-05-29 00:15:33 -04:00
parent f60f61201c
commit 76bef1fe01
4 changed files with 419 additions and 184 deletions

193
date.h
View File

@ -247,7 +247,9 @@ CONSTCD11 day operator+(const days& x, const day& y) NOEXCEPT;
CONSTCD11 day operator-(const day& x, const days& y) NOEXCEPT; CONSTCD11 day operator-(const day& x, const days& y) NOEXCEPT;
CONSTCD11 days operator-(const day& x, const day& y) NOEXCEPT; CONSTCD11 days operator-(const day& x, const day& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const day& d); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const day& d);
// month // month
@ -282,7 +284,9 @@ CONSTCD14 month operator+(const months& x, const month& y) NOEXCEPT;
CONSTCD14 month operator-(const month& x, const months& y) NOEXCEPT; CONSTCD14 month operator-(const month& x, const months& y) NOEXCEPT;
CONSTCD14 months operator-(const month& x, const month& y) NOEXCEPT; CONSTCD14 months operator-(const month& x, const month& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const month& m); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const month& m);
// year // year
@ -322,7 +326,9 @@ CONSTCD11 year operator+(const years& x, const year& y) NOEXCEPT;
CONSTCD11 year operator-(const year& x, const years& y) NOEXCEPT; CONSTCD11 year operator-(const year& x, const years& y) NOEXCEPT;
CONSTCD11 years operator-(const year& x, const year& y) NOEXCEPT; CONSTCD11 years operator-(const year& x, const year& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year& y); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year& y);
// weekday // weekday
@ -361,7 +367,9 @@ CONSTCD14 weekday operator+(const days& x, const weekday& y) NOEXCEPT;
CONSTCD14 weekday operator-(const weekday& x, const days& y) NOEXCEPT; CONSTCD14 weekday operator-(const weekday& x, const days& y) NOEXCEPT;
CONSTCD14 days operator-(const weekday& x, const weekday& y) NOEXCEPT; CONSTCD14 days operator-(const weekday& x, const weekday& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const weekday& wd); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const weekday& wd);
// weekday_indexed // weekday_indexed
@ -381,7 +389,9 @@ public:
CONSTCD11 bool operator==(const weekday_indexed& x, const weekday_indexed& y) NOEXCEPT; CONSTCD11 bool operator==(const weekday_indexed& x, const weekday_indexed& y) NOEXCEPT;
CONSTCD11 bool operator!=(const weekday_indexed& x, const weekday_indexed& y) NOEXCEPT; CONSTCD11 bool operator!=(const weekday_indexed& x, const weekday_indexed& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const weekday_indexed& wdi); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const weekday_indexed& wdi);
// weekday_last // weekday_last
@ -399,7 +409,9 @@ public:
CONSTCD11 bool operator==(const weekday_last& x, const weekday_last& y) NOEXCEPT; CONSTCD11 bool operator==(const weekday_last& x, const weekday_last& y) NOEXCEPT;
CONSTCD11 bool operator!=(const weekday_last& x, const weekday_last& y) NOEXCEPT; CONSTCD11 bool operator!=(const weekday_last& x, const weekday_last& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const weekday_last& wdl); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const weekday_last& wdl);
// year_month // year_month
@ -438,7 +450,9 @@ CONSTCD11 year_month operator+(const year_month& ym, const years& dy) NOEXCEPT;
CONSTCD11 year_month operator+(const years& dy, const year_month& ym) NOEXCEPT; CONSTCD11 year_month operator+(const years& dy, const year_month& ym) NOEXCEPT;
CONSTCD11 year_month operator-(const year_month& ym, const years& dy) NOEXCEPT; CONSTCD11 year_month operator-(const year_month& ym, const years& dy) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_month& ym); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_month& ym);
// month_day // month_day
@ -463,7 +477,9 @@ CONSTCD11 bool operator> (const month_day& x, const month_day& y) NOEXCEPT;
CONSTCD11 bool operator<=(const month_day& x, const month_day& y) NOEXCEPT; CONSTCD11 bool operator<=(const month_day& x, const month_day& y) NOEXCEPT;
CONSTCD11 bool operator>=(const month_day& x, const month_day& y) NOEXCEPT; CONSTCD11 bool operator>=(const month_day& x, const month_day& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const month_day& md); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const month_day& md);
// month_day_last // month_day_last
@ -485,7 +501,9 @@ CONSTCD11 bool operator> (const month_day_last& x, const month_day_last& y) NOEX
CONSTCD11 bool operator<=(const month_day_last& x, const month_day_last& y) NOEXCEPT; CONSTCD11 bool operator<=(const month_day_last& x, const month_day_last& y) NOEXCEPT;
CONSTCD11 bool operator>=(const month_day_last& x, const month_day_last& y) NOEXCEPT; CONSTCD11 bool operator>=(const month_day_last& x, const month_day_last& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const month_day_last& mdl); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const month_day_last& mdl);
// month_weekday // month_weekday
@ -506,7 +524,9 @@ public:
CONSTCD11 bool operator==(const month_weekday& x, const month_weekday& y) NOEXCEPT; CONSTCD11 bool operator==(const month_weekday& x, const month_weekday& y) NOEXCEPT;
CONSTCD11 bool operator!=(const month_weekday& x, const month_weekday& y) NOEXCEPT; CONSTCD11 bool operator!=(const month_weekday& x, const month_weekday& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const month_weekday& mwd); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const month_weekday& mwd);
// month_weekday_last // month_weekday_last
@ -530,7 +550,9 @@ CONSTCD11
CONSTCD11 CONSTCD11
bool operator!=(const month_weekday_last& x, const month_weekday_last& y) NOEXCEPT; bool operator!=(const month_weekday_last& x, const month_weekday_last& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const month_weekday_last& mwdl); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const month_weekday_last& mwdl);
// class year_month_day // class year_month_day
@ -580,7 +602,9 @@ CONSTCD11 year_month_day operator+(const year_month_day& ymd, const years& dy)
CONSTCD11 year_month_day operator+(const years& dy, const year_month_day& ymd) NOEXCEPT; CONSTCD11 year_month_day operator+(const years& dy, const year_month_day& ymd) NOEXCEPT;
CONSTCD11 year_month_day operator-(const year_month_day& ymd, const years& dy) NOEXCEPT; CONSTCD11 year_month_day operator-(const year_month_day& ymd, const years& dy) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_month_day& ymd); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_month_day& ymd);
// year_month_day_last // year_month_day_last
@ -645,7 +669,9 @@ CONSTCD11
year_month_day_last year_month_day_last
operator-(const year_month_day_last& ymdl, const years& dy) NOEXCEPT; operator-(const year_month_day_last& ymdl, const years& dy) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_month_day_last& ymdl); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_month_day_last& ymdl);
// year_month_weekday // year_month_weekday
@ -710,7 +736,9 @@ CONSTCD11
year_month_weekday year_month_weekday
operator-(const year_month_weekday& ymwd, const years& dy) NOEXCEPT; operator-(const year_month_weekday& ymwd, const years& dy) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_month_weekday& ymwdi); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_month_weekday& ymwdi);
// year_month_weekday_last // year_month_weekday_last
@ -774,7 +802,9 @@ CONSTCD11
year_month_weekday_last year_month_weekday_last
operator-(const year_month_weekday_last& ymwdl, const years& dy) NOEXCEPT; operator-(const year_month_weekday_last& ymwdl, const years& dy) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_month_weekday_last& ymwdl); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_month_weekday_last& ymwdl);
#if !defined(_MSC_VER) || (_MSC_VER >= 1900) #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
inline namespace literals inline namespace literals
@ -814,10 +844,11 @@ CONSTCD11 date::year operator "" _y(unsigned long long y) NOEXCEPT;
// utilities // utilities
namespace detail { namespace detail {
template<class CharT, class Traits = std::char_traits<CharT>>
class save_stream class save_stream
{ {
std::ostream& os_; std::basic_ostream<CharT, Traits>& os_;
char fill_; CharT fill_;
std::ios::fmtflags flags_; std::ios::fmtflags flags_;
std::locale loc_; std::locale loc_;
@ -832,7 +863,7 @@ public:
save_stream(const save_stream&) = delete; save_stream(const save_stream&) = delete;
save_stream& operator=(const save_stream&) = delete; save_stream& operator=(const save_stream&) = delete;
explicit save_stream(std::ostream& os) explicit save_stream(std::basic_ostream<CharT, Traits>& os)
: os_(os) : os_(os)
, fill_(os.fill()) , fill_(os.fill())
, flags_(os.flags()) , flags_(os.flags())
@ -1133,11 +1164,12 @@ operator-(const day& x, const days& y) NOEXCEPT
return x + -y; return x + -y;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const day& d) operator<<(std::basic_ostream<CharT, Traits>& os, const day& d)
{ {
detail::save_stream _(os); detail::save_stream<CharT, Traits> _(os);
os.fill('0'); os.fill('0');
os.flags(std::ios::dec | std::ios::right); os.flags(std::ios::dec | std::ios::right);
os.width(2); os.width(2);
@ -1255,9 +1287,10 @@ operator-(const month& x, const months& y) NOEXCEPT
return x + -y; return x + -y;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const month& m) operator<<(std::basic_ostream<CharT, Traits>& os, const month& m)
{ {
switch (static_cast<unsigned>(m)) switch (static_cast<unsigned>(m))
{ {
@ -1421,11 +1454,12 @@ operator-(const year& x, const years& y) NOEXCEPT
return year{static_cast<int>(x) - y.count()}; return year{static_cast<int>(x) - y.count()};
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year& y) operator<<(std::basic_ostream<CharT, Traits>& os, const year& y)
{ {
detail::save_stream _(os); detail::save_stream<CharT, Traits> _(os);
os.fill('0'); os.fill('0');
os.flags(std::ios::dec | std::ios::internal); os.flags(std::ios::dec | std::ios::internal);
os.width(4 + (y < year{0})); os.width(4 + (y < year{0}));
@ -1543,9 +1577,10 @@ operator-(const weekday& x, const days& y) NOEXCEPT
return x + -y; return x + -y;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const weekday& wd) operator<<(std::basic_ostream<CharT, Traits>& os, const weekday& wd)
{ {
switch (static_cast<unsigned>(wd)) switch (static_cast<unsigned>(wd))
{ {
@ -1652,9 +1687,10 @@ weekday_indexed::weekday_indexed(const date::weekday& wd, unsigned index) NOEXCE
, index_(static_cast<decltype(index_)>(index)) , index_(static_cast<decltype(index_)>(index))
{} {}
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const weekday_indexed& wdi) operator<<(std::basic_ostream<CharT, Traits>& os, const weekday_indexed& wdi)
{ {
return os << wdi.weekday() << '[' << wdi.index() << ']'; return os << wdi.weekday() << '[' << wdi.index() << ']';
} }
@ -1705,9 +1741,10 @@ operator!=(const weekday_last& x, const weekday_last& y) NOEXCEPT
return !(x == y); return !(x == y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const weekday_last& wdl) operator<<(std::basic_ostream<CharT, Traits>& os, const weekday_last& wdl)
{ {
return os << wdl.weekday() << "[last]"; return os << wdl.weekday() << "[last]";
} }
@ -1875,9 +1912,10 @@ operator-(const year_month& ym, const years& dy) NOEXCEPT
return ym + -dy; return ym + -dy;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_month& ym) operator<<(std::basic_ostream<CharT, Traits>& os, const year_month& ym)
{ {
return os << ym.year() << '/' << ym.month(); return os << ym.year() << '/' << ym.month();
} }
@ -1959,9 +1997,10 @@ operator>=(const month_day& x, const month_day& y) NOEXCEPT
return !(x < y); return !(x < y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const month_day& md) operator<<(std::basic_ostream<CharT, Traits>& os, const month_day& md)
{ {
return os << md.month() << '/' << md.day(); return os << md.month() << '/' << md.day();
} }
@ -2020,9 +2059,10 @@ operator>=(const month_day_last& x, const month_day_last& y) NOEXCEPT
return !(x < y); return !(x < y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const month_day_last& mdl) operator<<(std::basic_ostream<CharT, Traits>& os, const month_day_last& mdl)
{ {
return os << mdl.month() << "/last"; return os << mdl.month() << "/last";
} }
@ -2071,9 +2111,10 @@ operator!=(const month_weekday& x, const month_weekday& y) NOEXCEPT
return !(x == y); return !(x == y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const month_weekday& mwd) operator<<(std::basic_ostream<CharT, Traits>& os, const month_weekday& mwd)
{ {
return os << mwd.month() << '/' << mwd.weekday_indexed(); return os << mwd.month() << '/' << mwd.weekday_indexed();
} }
@ -2122,9 +2163,10 @@ operator!=(const month_weekday_last& x, const month_weekday_last& y) NOEXCEPT
return !(x == y); return !(x == y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const month_weekday_last& mwdl) operator<<(std::basic_ostream<CharT, Traits>& os, const month_weekday_last& mwdl)
{ {
return os << mwdl.month() << '/' << mwdl.weekday_last(); return os << mwdl.month() << '/' << mwdl.weekday_last();
} }
@ -2270,9 +2312,10 @@ operator>=(const year_month_day_last& x, const year_month_day_last& y) NOEXCEPT
return !(x < y); return !(x < y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_month_day_last& ymdl) operator<<(std::basic_ostream<CharT, Traits>& os, const year_month_day_last& ymdl)
{ {
return os << ymdl.year() << '/' << ymdl.month_day_last(); return os << ymdl.year() << '/' << ymdl.month_day_last();
} }
@ -2487,11 +2530,12 @@ operator>=(const year_month_day& x, const year_month_day& y) NOEXCEPT
return !(x < y); return !(x < y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_month_day& ymd) operator<<(std::basic_ostream<CharT, Traits>& os, const year_month_day& ymd)
{ {
detail::save_stream _(os); detail::save_stream<CharT, Traits> _(os);
os.fill('0'); os.fill('0');
os.flags(std::ios::dec | std::ios::right); os.flags(std::ios::dec | std::ios::right);
os << ymd.year() << '-'; os << ymd.year() << '-';
@ -2718,9 +2762,10 @@ operator!=(const year_month_weekday& x, const year_month_weekday& y) NOEXCEPT
return !(x == y); return !(x == y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_month_weekday& ymwdi) operator<<(std::basic_ostream<CharT, Traits>& os, const year_month_weekday& ymwdi)
{ {
return os << ymwdi.year() << '/' << ymwdi.month() return os << ymwdi.year() << '/' << ymwdi.month()
<< '/' << ymwdi.weekday_indexed(); << '/' << ymwdi.weekday_indexed();
@ -2885,9 +2930,10 @@ operator!=(const year_month_weekday_last& x, const year_month_weekday_last& y) N
return !(x == y); return !(x == y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_month_weekday_last& ymwdl) operator<<(std::basic_ostream<CharT, Traits>& os, const year_month_weekday_last& ymwdl)
{ {
return os << ymwdl.year() << '/' << ymwdl.month() << '/' << ymwdl.weekday_last(); return os << ymwdl.year() << '/' << ymwdl.month() << '/' << ymwdl.weekday_last();
} }
@ -3435,12 +3481,13 @@ public:
CONSTCD14 time_of_day_storage& make24() NOEXCEPT {base::make24(); return *this;} CONSTCD14 time_of_day_storage& make24() NOEXCEPT {base::make24(); return *this;}
CONSTCD14 time_of_day_storage& make12() NOEXCEPT {base::make12(); return *this;} CONSTCD14 time_of_day_storage& make12() NOEXCEPT {base::make12(); return *this;}
template<class CharT, class Traits>
friend friend
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const time_of_day_storage& t) operator<<(std::basic_ostream<CharT, Traits>& os, const time_of_day_storage& t)
{ {
using namespace std; using namespace std;
detail::save_stream _(os); detail::save_stream<CharT, Traits> _(os);
os.fill('0'); os.fill('0');
os.flags(std::ios::dec | std::ios::right); os.flags(std::ios::dec | std::ios::right);
if (t.mode_ != am && t.mode_ != pm) if (t.mode_ != am && t.mode_ != pm)
@ -3501,12 +3548,13 @@ public:
CONSTCD14 time_of_day_storage& make24() NOEXCEPT {base::make24(); return *this;} CONSTCD14 time_of_day_storage& make24() NOEXCEPT {base::make24(); return *this;}
CONSTCD14 time_of_day_storage& make12() NOEXCEPT {base::make12(); return *this;} CONSTCD14 time_of_day_storage& make12() NOEXCEPT {base::make12(); return *this;}
template<class CharT, class Traits>
friend friend
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const time_of_day_storage& t) operator<<(std::basic_ostream<CharT, Traits>& os, const time_of_day_storage& t)
{ {
using namespace std; using namespace std;
detail::save_stream _(os); detail::save_stream<CharT, Traits> _(os);
if (static_cast<precision>(t) < std::chrono::hours{0}) if (static_cast<precision>(t) < std::chrono::hours{0})
os << '-'; os << '-';
os.fill('0'); os.fill('0');
@ -3572,12 +3620,13 @@ public:
CONSTCD14 time_of_day_storage& make24() NOEXCEPT {base::make24(); return *this;} CONSTCD14 time_of_day_storage& make24() NOEXCEPT {base::make24(); return *this;}
CONSTCD14 time_of_day_storage& make12() NOEXCEPT {base::make12(); return *this;} CONSTCD14 time_of_day_storage& make12() NOEXCEPT {base::make12(); return *this;}
template<class CharT, class Traits>
friend friend
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const time_of_day_storage& t) operator<<(std::basic_ostream<CharT, Traits>& os, const time_of_day_storage& t)
{ {
using namespace std; using namespace std;
detail::save_stream _(os); detail::save_stream<CharT, Traits> _(os);
if (static_cast<precision>(t) < std::chrono::hours{0}) if (static_cast<precision>(t) < std::chrono::hours{0})
os << '-'; os << '-';
os.fill('0'); os.fill('0');
@ -3652,12 +3701,13 @@ public:
CONSTCD14 time_of_day_storage& make24() NOEXCEPT {base::make24(); return *this;} CONSTCD14 time_of_day_storage& make24() NOEXCEPT {base::make24(); return *this;}
CONSTCD14 time_of_day_storage& make12() NOEXCEPT {base::make12(); return *this;} CONSTCD14 time_of_day_storage& make12() NOEXCEPT {base::make12(); return *this;}
template<class CharT, class Traits>
friend friend
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const time_of_day_storage& t) operator<<(std::basic_ostream<CharT, Traits>& os, const time_of_day_storage& t)
{ {
using namespace std; using namespace std;
detail::save_stream _(os); detail::save_stream<CharT, Traits> _(os);
if (static_cast<precision>(t) < std::chrono::hours{0}) if (static_cast<precision>(t) < std::chrono::hours{0})
os << '-'; os << '-';
os.fill('0'); os.fill('0');
@ -3808,31 +3858,32 @@ make_time(std::chrono::hours h, std::chrono::minutes m, std::chrono::seconds s,
return time_of_day<std::chrono::duration<Rep, Period>>(h, m, s, sub_s, md); return time_of_day<std::chrono::duration<Rep, Period>>(h, m, s, sub_s, md);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
typename std::enable_if typename std::enable_if
< <
!std::chrono::treat_as_floating_point<typename Duration::rep>::value && !std::chrono::treat_as_floating_point<typename Duration::rep>::value &&
std::ratio_less<typename Duration::period, days::period>::value std::ratio_less<typename Duration::period, days::period>::value
, std::ostream& , std::basic_ostream<CharT, Traits>&
>::type >::type
operator<<(std::ostream& os, const sys_time<Duration>& tp) operator<<(std::basic_ostream<CharT, Traits>& os, const sys_time<Duration>& tp)
{ {
auto const dp = floor<days>(tp); auto const dp = floor<days>(tp);
return os << year_month_day(dp) << ' ' << make_time(tp-dp); return os << year_month_day(dp) << ' ' << make_time(tp-dp);
} }
template <class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const sys_days& dp) operator<<(std::basic_ostream<CharT, Traits>& os, const sys_days& dp)
{ {
return os << year_month_day(dp); return os << year_month_day(dp);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const local_time<Duration>& ut) operator<<(std::basic_ostream<CharT, Traits>& os, const local_time<Duration>& ut)
{ {
return os << sys_time<Duration>{ut.time_since_epoch()}; return os << sys_time<Duration>{ut.time_since_epoch()};
} }

View File

@ -125,7 +125,9 @@ CONSTCD14 weekday operator+(const days& x, const weekday& y) NOEXCEPT;
CONSTCD14 weekday operator-(const weekday& x, const days& y) NOEXCEPT; CONSTCD14 weekday operator-(const weekday& x, const days& y) NOEXCEPT;
CONSTCD14 days operator-(const weekday& x, const weekday& y) NOEXCEPT; CONSTCD14 days operator-(const weekday& x, const weekday& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const weekday& wd); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const weekday& wd);
// year // year
@ -163,7 +165,9 @@ CONSTCD11 year operator+(const years& x, const year& y) NOEXCEPT;
CONSTCD11 year operator-(const year& x, const years& y) NOEXCEPT; CONSTCD11 year operator-(const year& x, const years& y) NOEXCEPT;
CONSTCD11 years operator-(const year& x, const year& y) NOEXCEPT; CONSTCD11 years operator-(const year& x, const year& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year& y); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year& y);
// weeknum // weeknum
@ -198,7 +202,9 @@ CONSTCD11 weeknum operator+(const weeks& x, const weeknum& y) NOEXCEPT;
CONSTCD11 weeknum operator-(const weeknum& x, const weeks& y) NOEXCEPT; CONSTCD11 weeknum operator-(const weeknum& x, const weeks& y) NOEXCEPT;
CONSTCD11 weeks operator-(const weeknum& x, const weeknum& y) NOEXCEPT; CONSTCD11 weeks operator-(const weeknum& x, const weeknum& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const weeknum& wn); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const weeknum& wn);
// year_weeknum // year_weeknum
@ -230,7 +236,9 @@ CONSTCD11 year_weeknum operator+(const year_weeknum& ym, const years& dy) NOEXCE
CONSTCD11 year_weeknum operator+(const years& dy, const year_weeknum& ym) NOEXCEPT; CONSTCD11 year_weeknum operator+(const years& dy, const year_weeknum& ym) NOEXCEPT;
CONSTCD11 year_weeknum operator-(const year_weeknum& ym, const years& dy) NOEXCEPT; CONSTCD11 year_weeknum operator-(const year_weeknum& ym, const years& dy) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_weeknum& ym); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_weeknum& ym);
// year_lastweek // year_lastweek
@ -261,7 +269,9 @@ CONSTCD11 year_lastweek operator+(const year_lastweek& ym, const years& dy) NOEX
CONSTCD11 year_lastweek operator+(const years& dy, const year_lastweek& ym) NOEXCEPT; CONSTCD11 year_lastweek operator+(const years& dy, const year_lastweek& ym) NOEXCEPT;
CONSTCD11 year_lastweek operator-(const year_lastweek& ym, const years& dy) NOEXCEPT; CONSTCD11 year_lastweek operator-(const year_lastweek& ym, const years& dy) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_lastweek& ym); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_lastweek& ym);
// weeknum_weekday // weeknum_weekday
@ -287,7 +297,9 @@ CONSTCD11 bool operator> (const weeknum_weekday& x, const weeknum_weekday& y) NO
CONSTCD11 bool operator<=(const weeknum_weekday& x, const weeknum_weekday& y) NOEXCEPT; CONSTCD11 bool operator<=(const weeknum_weekday& x, const weeknum_weekday& y) NOEXCEPT;
CONSTCD11 bool operator>=(const weeknum_weekday& x, const weeknum_weekday& y) NOEXCEPT; CONSTCD11 bool operator>=(const weeknum_weekday& x, const weeknum_weekday& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const weeknum_weekday& md); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const weeknum_weekday& md);
// lastweek_weekday // lastweek_weekday
@ -310,7 +322,9 @@ CONSTCD11 bool operator> (const lastweek_weekday& x, const lastweek_weekday& y)
CONSTCD11 bool operator<=(const lastweek_weekday& x, const lastweek_weekday& y) NOEXCEPT; CONSTCD11 bool operator<=(const lastweek_weekday& x, const lastweek_weekday& y) NOEXCEPT;
CONSTCD11 bool operator>=(const lastweek_weekday& x, const lastweek_weekday& y) NOEXCEPT; CONSTCD11 bool operator>=(const lastweek_weekday& x, const lastweek_weekday& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const lastweek_weekday& md); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const lastweek_weekday& md);
// year_lastweek_weekday // year_lastweek_weekday
@ -346,7 +360,9 @@ CONSTCD11 year_lastweek_weekday operator+(const year_lastweek_weekday& ywnwd, co
CONSTCD11 year_lastweek_weekday operator+(const years& y, const year_lastweek_weekday& ywnwd) NOEXCEPT; CONSTCD11 year_lastweek_weekday operator+(const years& y, const year_lastweek_weekday& ywnwd) NOEXCEPT;
CONSTCD11 year_lastweek_weekday operator-(const year_lastweek_weekday& ywnwd, const years& y) NOEXCEPT; CONSTCD11 year_lastweek_weekday operator-(const year_lastweek_weekday& ywnwd, const years& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_lastweek_weekday& ywnwd); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_lastweek_weekday& ywnwd);
// class year_weeknum_weekday // class year_weeknum_weekday
@ -389,7 +405,9 @@ CONSTCD11 year_weeknum_weekday operator+(const year_weeknum_weekday& ywnwd, cons
CONSTCD11 year_weeknum_weekday operator+(const years& y, const year_weeknum_weekday& ywnwd) NOEXCEPT; CONSTCD11 year_weeknum_weekday operator+(const years& y, const year_weeknum_weekday& ywnwd) NOEXCEPT;
CONSTCD11 year_weeknum_weekday operator-(const year_weeknum_weekday& ywnwd, const years& y) NOEXCEPT; CONSTCD11 year_weeknum_weekday operator-(const year_weeknum_weekday& ywnwd, const years& y) NOEXCEPT;
std::ostream& operator<<(std::ostream& os, const year_weeknum_weekday& ywnwd); template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const year_weeknum_weekday& ywnwd);
//----------------+ //----------------+
// Implementation | // Implementation |
@ -534,9 +552,10 @@ operator-(const weekday& x, const days& y) NOEXCEPT
return x + -y; return x + -y;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const weekday& wd) operator<<(std::basic_ostream<CharT, Traits>& os, const weekday& wd)
{ {
switch (static_cast<unsigned>(wd)) switch (static_cast<unsigned>(wd))
{ {
@ -687,11 +706,12 @@ operator-(const year& x, const years& y) NOEXCEPT
return year{static_cast<int>(x) - y.count()}; return year{static_cast<int>(x) - y.count()};
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year& y) operator<<(std::basic_ostream<CharT, Traits>& os, const year& y)
{ {
date::detail::save_stream _(os); date::detail::save_stream<CharT, Traits> _(os);
os.fill('0'); os.fill('0');
os.flags(std::ios::dec | std::ios::internal); os.flags(std::ios::dec | std::ios::internal);
os.width(4 + (y < year{0})); os.width(4 + (y < year{0}));
@ -848,11 +868,12 @@ operator-(const weeknum& x, const weeks& y) NOEXCEPT
return x + -y; return x + -y;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const weeknum& wn) operator<<(std::basic_ostream<CharT, Traits>& os, const weeknum& wn)
{ {
date::detail::save_stream _(os); date::detail::save_stream<CharT, Traits> _(os);
os << 'W'; os << 'W';
os.fill('0'); os.fill('0');
os.flags(std::ios::dec | std::ios::right); os.flags(std::ios::dec | std::ios::right);
@ -967,9 +988,10 @@ operator-(const year_weeknum& ym, const years& dy) NOEXCEPT
return ym + -dy; return ym + -dy;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_weeknum& ywn) operator<<(std::basic_ostream<CharT, Traits>& os, const year_weeknum& ywn)
{ {
return os << ywn.year() << '-' << ywn.weeknum(); return os << ywn.year() << '-' << ywn.weeknum();
} }
@ -1086,9 +1108,10 @@ operator-(const year_lastweek& ym, const years& dy) NOEXCEPT
return ym + -dy; return ym + -dy;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_lastweek& ywn) operator<<(std::basic_ostream<CharT, Traits>& os, const year_lastweek& ywn)
{ {
return os << ywn.year() << "-W last"; return os << ywn.year() << "-W last";
} }
@ -1164,9 +1187,10 @@ operator>=(const weeknum_weekday& x, const weeknum_weekday& y) NOEXCEPT
return !(x < y); return !(x < y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const weeknum_weekday& md) operator<<(std::basic_ostream<CharT, Traits>& os, const weeknum_weekday& md)
{ {
return os << md.weeknum() << '-' << md.weekday(); return os << md.weeknum() << '-' << md.weekday();
} }
@ -1237,9 +1261,10 @@ operator>=(const lastweek_weekday& x, const lastweek_weekday& y) NOEXCEPT
return !(x < y); return !(x < y);
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const lastweek_weekday& md) operator<<(std::basic_ostream<CharT, Traits>& os, const lastweek_weekday& md)
{ {
return os << "W last-" << md.weekday(); return os << "W last-" << md.weekday();
} }
@ -1380,9 +1405,10 @@ operator-(const year_lastweek_weekday& ywnwd, const years& y) NOEXCEPT
return ywnwd + -y; return ywnwd + -y;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_lastweek_weekday& ywnwd) operator<<(std::basic_ostream<CharT, Traits>& os, const year_lastweek_weekday& ywnwd)
{ {
return os << ywnwd.year() << "-W last-" << ywnwd.weekday(); return os << ywnwd.year() << "-W last-" << ywnwd.weekday();
} }
@ -1557,9 +1583,10 @@ operator-(const year_weeknum_weekday& ywnwd, const years& y) NOEXCEPT
return ywnwd + -y; return ywnwd + -y;
} }
template<class CharT, class Traits>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const year_weeknum_weekday& ywnwd) operator<<(std::basic_ostream<CharT, Traits>& os, const year_weeknum_weekday& ywnwd)
{ {
return os << ywnwd.year() << '-' << ywnwd.weeknum() << '-' << ywnwd.weekday(); return os << ywnwd.year() << '-' << ywnwd.weeknum() << '-' << ywnwd.weekday();
} }

6
tz.cpp
View File

@ -1034,7 +1034,7 @@ operator<<(std::ostream& os, const Rule& r)
{ {
using namespace date; using namespace date;
using namespace std::chrono; using namespace std::chrono;
detail::save_stream _(os); detail::save_stream<char> _(os);
os.fill(' '); os.fill(' ');
os.flags(std::ios::dec | std::ios::left); os.flags(std::ios::dec | std::ios::left);
os.width(15); os.width(15);
@ -1853,7 +1853,7 @@ operator<<(std::ostream& os, const time_zone& z)
{ {
using namespace date; using namespace date;
using namespace std::chrono; using namespace std::chrono;
detail::save_stream _(os); detail::save_stream<char> _(os);
os.fill(' '); os.fill(' ');
os.flags(std::ios::dec | std::ios::left); os.flags(std::ios::dec | std::ios::left);
#if LAZY_INIT #if LAZY_INIT
@ -1919,7 +1919,7 @@ std::ostream&
operator<<(std::ostream& os, const link& x) operator<<(std::ostream& os, const link& x)
{ {
using namespace date; using namespace date;
detail::save_stream _(os); detail::save_stream<char> _(os);
os.fill(' '); os.fill(' ');
os.flags(std::ios::dec | std::ios::left); os.flags(std::ios::dec | std::ios::left);
os.width(35); os.width(35);

319
tz.h
View File

@ -210,8 +210,7 @@ struct sys_info
std::string abbrev; std::string abbrev;
}; };
std::ostream& std::ostream& operator<<(std::ostream& os, const sys_info& r);
operator<<(std::ostream& os, const sys_info& r);
struct local_info struct local_info
{ {
@ -220,12 +219,7 @@ struct local_info
sys_info second; sys_info second;
}; };
std::ostream& std::ostream& operator<<(std::ostream& os, const local_info& r);
operator<<(std::ostream& os, const local_info& r);
// deprecated:
using Info = sys_info;
class time_zone; class time_zone;
@ -277,10 +271,10 @@ public:
bool bool
operator==(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y); operator==(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y);
template <class Duration1> template <class CharT, class Traits, class Duration1>
friend friend
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const zoned_time<Duration1>& t); operator<<(std::basic_ostream<CharT, Traits>& os, const zoned_time<Duration1>& t);
private: private:
@ -726,7 +720,8 @@ struct TZ_DB
#endif // !defined(_MSC_VER) || (_MSC_VER >= 1900) #endif // !defined(_MSC_VER) || (_MSC_VER >= 1900)
}; };
std::ostream& operator<<(std::ostream& os, const TZ_DB& db); std::ostream&
operator<<(std::ostream& os, const TZ_DB& db);
const TZ_DB& get_tzdb(); const TZ_DB& get_tzdb();
const TZ_DB& reload_tzdb(); const TZ_DB& reload_tzdb();
@ -995,10 +990,10 @@ make_zoned(const std::string& name, const sys_time<Duration>& st)
return {name, st}; return {name, st};
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::ostream& std::basic_ostream<CharT, Traits>&
operator<<(std::ostream& os, const zoned_time<Duration>& t) operator<<(std::basic_ostream<CharT, Traits>& os, const zoned_time<Duration>& t)
{ {
auto i = t.zone_->get_info(t.tp_); auto i = t.zone_->get_info(t.tp_);
auto lt = t.tp_ + i.offset; auto lt = t.tp_ + i.offset;
@ -1088,9 +1083,9 @@ to_utc_time(sys_time<Duration> st)
namespace detail namespace detail
{ {
template <class Duration> template <class CharT, class Traits, class Duration>
std::string std::basic_string<CharT, Traits>
format(const std::locale& loc, std::string format, format(const std::locale& loc, std::basic_string<CharT, Traits> format,
local_time<Duration> tp, const time_zone* zone = nullptr) local_time<Duration> tp, const time_zone* zone = nullptr)
{ {
// Handle these specially // Handle these specially
@ -1111,7 +1106,7 @@ format(const std::locale& loc, std::string format,
case 'T': case 'T':
if (ratio_less<typename Duration::period, ratio<1>>::value) if (ratio_less<typename Duration::period, ratio<1>>::value)
{ {
ostringstream os; basic_ostringstream<CharT, Traits> os;
os.imbue(loc); os.imbue(loc);
os << make_time(tp - floor<seconds>(tp)); os << make_time(tp - floor<seconds>(tp));
auto s = os.str(); auto s = os.str();
@ -1127,7 +1122,7 @@ format(const std::locale& loc, std::string format,
{ {
auto info = zone->get_info(tp).first; auto info = zone->get_info(tp).first;
auto offset = duration_cast<minutes>(info.offset); auto offset = duration_cast<minutes>(info.offset);
ostringstream os; basic_ostringstream<CharT, Traits> os;
if (offset >= minutes{0}) if (offset >= minutes{0})
os << '+'; os << '+';
os << make_time(offset); os << make_time(offset);
@ -1143,15 +1138,16 @@ format(const std::locale& loc, std::string format,
else else
{ {
auto info = zone->get_info(tp).first; auto info = zone->get_info(tp).first;
format.replace(i, 2, info.abbrev); format.replace(i, 2, std::basic_string<CharT, Traits>
(info.abbrev.begin(), info.abbrev.end()));
i += info.abbrev.size() - 1; i += info.abbrev.size() - 1;
} }
break; break;
} }
} }
} }
auto& f = use_facet<time_put<char>>(loc); auto& f = use_facet<time_put<CharT>>(loc);
ostringstream os; basic_ostringstream<CharT, Traits> os;
auto tt = system_clock::to_time_t(sys_time<Duration>{tp.time_since_epoch()}); auto tt = system_clock::to_time_t(sys_time<Duration>{tp.time_since_epoch()});
std::tm tm{}; std::tm tm{};
#ifndef _MSC_VER #ifndef _MSC_VER
@ -1165,78 +1161,136 @@ format(const std::locale& loc, std::string format,
} // namespace detail } // namespace detail
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::string std::basic_string<CharT, Traits>
format(const std::locale& loc, std::string format, local_time<Duration> tp) format(const std::locale& loc, std::basic_string<CharT, Traits> format,
local_time<Duration> tp)
{ {
return detail::format(loc, std::move(format), tp); return detail::format(loc, std::move(format), tp);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::string std::basic_string<CharT, Traits>
format(std::string format, local_time<Duration> tp) format(std::basic_string<CharT, Traits> format, local_time<Duration> tp)
{ {
return detail::format(std::locale{}, std::move(format), tp); return detail::format(std::locale{}, std::move(format), tp);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::string std::basic_string<CharT, Traits>
format(const std::locale& loc, std::string format, const zoned_time<Duration>& tp) format(const std::locale& loc, std::basic_string<CharT, Traits> format,
const zoned_time<Duration>& tp)
{ {
return detail::format(loc, std::move(format), tp.get_local_time(), return detail::format(loc, std::move(format), tp.get_local_time(),
tp.get_time_zone()); tp.get_time_zone());
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::string std::basic_string<CharT, Traits>
format(std::string format, const zoned_time<Duration>& tp) format(std::basic_string<CharT, Traits> format, const zoned_time<Duration>& tp)
{ {
return detail::format(std::locale{}, std::move(format), tp.get_local_time(), return detail::format(std::locale{}, std::move(format), tp.get_local_time(),
tp.get_time_zone()); tp.get_time_zone());
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::string std::basic_string<CharT, Traits>
format(const std::locale& loc, std::string format, sys_time<Duration> tp) format(const std::locale& loc, std::basic_string<CharT, Traits> format,
sys_time<Duration> tp)
{ {
return detail::format(loc, std::move(format), return detail::format(loc, std::move(format),
local_time<Duration>{tp.time_since_epoch()}, locate_zone("UTC")); local_time<Duration>{tp.time_since_epoch()}, locate_zone("UTC"));
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::string std::basic_string<CharT, Traits>
format(std::string format, sys_time<Duration> tp) format(std::basic_string<CharT, Traits> format, sys_time<Duration> tp)
{ {
return detail::format(std::locale{}, std::move(format), return detail::format(std::locale{}, std::move(format),
local_time<Duration>{tp.time_since_epoch()}, locate_zone("UTC")); local_time<Duration>{tp.time_since_epoch()}, locate_zone("UTC"));
} }
// const CharT* formats
template <class CharT, class Duration>
inline
std::basic_string<CharT>
format(const std::locale& loc, const CharT* format, local_time<Duration> tp)
{
return detail::format(loc, std::basic_string<CharT>(format), tp);
}
template <class CharT, class Duration>
inline
std::basic_string<CharT>
format(const CharT* format, local_time<Duration> tp)
{
return detail::format(std::locale{}, std::basic_string<CharT>(format), tp);
}
template <class CharT, class Duration>
inline
std::basic_string<CharT>
format(const std::locale& loc, const CharT* format, const zoned_time<Duration>& tp)
{
return detail::format(loc, std::basic_string<CharT>(format), tp.get_local_time(),
tp.get_time_zone());
}
template <class CharT, class Duration>
inline
std::basic_string<CharT>
format(const CharT* format, const zoned_time<Duration>& tp)
{
return detail::format(std::locale{}, std::basic_string<CharT>(format),
tp.get_local_time(), tp.get_time_zone());
}
template <class CharT, class Duration>
inline
std::basic_string<CharT>
format(const std::locale& loc, const CharT* format, sys_time<Duration> tp)
{
return detail::format(loc, std::basic_string<CharT>(format),
local_time<Duration>{tp.time_since_epoch()}, locate_zone("UTC"));
}
template <class CharT, class Duration>
inline
std::basic_string<CharT>
format(const CharT* format, sys_time<Duration> tp)
{
return detail::format(std::locale{}, std::basic_string<CharT>(format),
local_time<Duration>{tp.time_since_epoch()}, locate_zone("UTC"));
}
// parse // parse
namespace detail namespace detail
{ {
template <class Duration> template <class CharT, class Traits, class Duration>
void void
parse(std::istream& is, const std::string& format, sys_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
std::string& abbrev, std::chrono::minutes& offset) const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{ {
using namespace std; using namespace std;
using namespace std::chrono; using namespace std::chrono;
istream::sentry ok{is}; typename basic_istream<CharT, Traits>::sentry ok{is};
if (ok) if (ok)
{ {
auto& f = use_facet<time_get<char>>(is.getloc()); auto& f = use_facet<time_get<CharT>>(is.getloc());
ios_base::iostate err = ios_base::goodbit; ios_base::iostate err = ios_base::goodbit;
std::tm tm{}; std::tm tm{};
Duration subseconds{}; Duration subseconds{};
std::string temp_abbrev; std::basic_string<CharT, Traits> temp_abbrev;
minutes temp_offset{}; minutes temp_offset{};
auto b = format.data(); auto b = format.data();
@ -1255,8 +1309,8 @@ parse(std::istream& is, const std::string& format, sys_time<Duration>& tp,
b = i+1; b = i+1;
if (*i == 'T') if (*i == 'T')
{ {
const char hm[] = "%H:%M:"; const CharT hm[] = {'%', 'H', ':', '%', 'M', ':', 0};
f.get(is, 0, is, err, &tm, hm, hm+6); f.get(is, 0, is, err, &tm, hm, hm);
} }
if (ratio_less<typename Duration::period, ratio<1>>::value) if (ratio_less<typename Duration::period, ratio<1>>::value)
{ {
@ -1269,7 +1323,7 @@ parse(std::istream& is, const std::string& format, sys_time<Duration>& tp,
} }
else else
{ {
const char hm[] = "%S"; const CharT hm[] = {'%', 'S'};
f.get(is, 0, is, err, &tm, hm, hm+2); f.get(is, 0, is, err, &tm, hm, hm+2);
} }
break; break;
@ -1279,7 +1333,7 @@ parse(std::istream& is, const std::string& format, sys_time<Duration>& tp,
b = i+1; b = i+1;
if ((err & ios_base::failbit) == 0) if ((err & ios_base::failbit) == 0)
{ {
char sign{}; CharT sign{};
is >> sign; is >> sign;
if (!is.fail() && (sign == '+' || sign == '-')) if (!is.fail() && (sign == '+' || sign == '-'))
{ {
@ -1339,23 +1393,25 @@ parse(std::istream& is, const std::string& format, sys_time<Duration>& tp,
} // namespace detail } // namespace detail
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, sys_time<Duration>& tp) parse(std::basic_istream<CharT, Traits>& is,
const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp)
{ {
std::string abbrev; std::basic_string<CharT, Traits> abbrev;
std::chrono::minutes offset{}; std::chrono::minutes offset{};
detail::parse(is, format, tp, abbrev, offset); detail::parse(is, format, tp, abbrev, offset);
if (!is.fail()) if (!is.fail())
tp = floor<Duration>(tp - offset); tp = floor<Duration>(tp - offset);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, sys_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
std::string& abbrev) const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{ {
std::chrono::minutes offset{}; std::chrono::minutes offset{};
detail::parse(is, format, tp, abbrev, offset); detail::parse(is, format, tp, abbrev, offset);
@ -1363,58 +1419,63 @@ parse(std::istream& is, const std::string& format, sys_time<Duration>& tp,
tp = floor<Duration>(tp - offset); tp = floor<Duration>(tp - offset);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, sys_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::chrono::minutes& offset) std::chrono::minutes& offset)
{ {
std::string abbrev; std::basic_string<CharT, Traits> abbrev;
detail::parse(is, format, tp, abbrev, offset); detail::parse(is, format, tp, abbrev, offset);
if (!is.fail()) if (!is.fail())
tp = floor<Duration>(tp - offset); tp = floor<Duration>(tp - offset);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, sys_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
std::string& abbrev, std::chrono::minutes& offset) const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{ {
detail::parse(is, format, tp, abbrev, offset); detail::parse(is, format, tp, abbrev, offset);
if (!is.fail()) if (!is.fail())
tp = floor<Duration>(tp - offset); tp = floor<Duration>(tp - offset);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, sys_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
std::chrono::minutes& offset, std::string& abbrev) const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{ {
detail::parse(is, format, tp, abbrev, offset); detail::parse(is, format, tp, abbrev, offset);
if (!is.fail()) if (!is.fail())
tp = floor<Duration>(tp - offset); tp = floor<Duration>(tp - offset);
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, local_time<Duration>& tp) parse(std::basic_istream<CharT, Traits>& is,
const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp)
{ {
sys_time<Duration> st; sys_time<Duration> st;
std::string abbrev; std::basic_string<CharT, Traits> abbrev;
std::chrono::minutes offset{}; std::chrono::minutes offset{};
detail::parse(is, format, st, abbrev, offset); detail::parse(is, format, st, abbrev, offset);
if (!is.fail()) if (!is.fail())
tp = local_time<Duration>{st.time_since_epoch()}; tp = local_time<Duration>{st.time_since_epoch()};
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, local_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
std::string& abbrev) const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{ {
sys_time<Duration> st; sys_time<Duration> st;
std::chrono::minutes offset{}; std::chrono::minutes offset{};
@ -1423,24 +1484,26 @@ parse(std::istream& is, const std::string& format, local_time<Duration>& tp,
tp = local_time<Duration>{st.time_since_epoch()}; tp = local_time<Duration>{st.time_since_epoch()};
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, local_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp,
std::chrono::minutes& offset) std::chrono::minutes& offset)
{ {
sys_time<Duration> st; sys_time<Duration> st;
std::string abbrev; std::basic_string<CharT, Traits> abbrev;
detail::parse(is, format, st, abbrev, offset); detail::parse(is, format, st, abbrev, offset);
if (!is.fail()) if (!is.fail())
tp = local_time<Duration>{st.time_since_epoch()}; tp = local_time<Duration>{st.time_since_epoch()};
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, local_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
std::string& abbrev, std::chrono::minutes& offset) const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{ {
sys_time<Duration> st; sys_time<Duration> st;
detail::parse(is, format, st, abbrev, offset); detail::parse(is, format, st, abbrev, offset);
@ -1448,11 +1511,12 @@ parse(std::istream& is, const std::string& format, local_time<Duration>& tp,
tp = local_time<Duration>{st.time_since_epoch()}; tp = local_time<Duration>{st.time_since_epoch()};
} }
template <class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
parse(std::istream& is, const std::string& format, local_time<Duration>& tp, parse(std::basic_istream<CharT, Traits>& is,
std::chrono::minutes& offset, std::string& abbrev) const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{ {
sys_time<Duration> st; sys_time<Duration> st;
detail::parse(is, format, st, abbrev, offset); detail::parse(is, format, st, abbrev, offset);
@ -1460,6 +1524,99 @@ parse(std::istream& is, const std::string& format, local_time<Duration>& tp,
tp = local_time<Duration>{st.time_since_epoch()}; tp = local_time<Duration>{st.time_since_epoch()};
} }
// const CharT* formats
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format, sys_time<Duration>& tp)
{
parse(is, std::basic_string<CharT, Traits>(format), tp);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
parse(is, std::basic_string<CharT, Traits>(format), tp, abbrev);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format, sys_time<Duration>& tp,
std::chrono::minutes& offset)
{
parse(is, std::basic_string<CharT, Traits>(format), tp, offset);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
parse(is, std::basic_string<CharT, Traits>(format), tp, abbrev, offset);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format, sys_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
parse(is, std::basic_string<CharT, Traits>(format), tp, abbrev, offset);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format,
local_time<Duration>& tp)
{
parse(is, std::basic_string<CharT, Traits>(format), tp);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format,
local_time<Duration>& tp, std::basic_string<CharT, Traits>& abbrev)
{
parse(is, std::basic_string<CharT, Traits>(format), tp, abbrev);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format,
local_time<Duration>& tp, std::chrono::minutes& offset)
{
parse(is, std::basic_string<CharT, Traits>(format), tp, offset);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format,
local_time<Duration>& tp, std::basic_string<CharT, Traits>& abbrev,
std::chrono::minutes& offset)
{
parse(is, std::basic_string<CharT, Traits>(format), tp, abbrev, offset);
}
template <class CharT, class Traits, class Duration>
inline
void
parse(std::basic_istream<CharT, Traits>& is, const CharT* format,
local_time<Duration>& tp, std::chrono::minutes& offset,
std::basic_string<CharT, Traits>& abbrev)
{
parse(is, std::basic_string<CharT, Traits>(format), tp, abbrev, offset);
}
} // namespace date } // namespace date
#endif // TZ_H #endif // TZ_H