Simplify the implementation of format and parse:

* For format, all a type must do is implement to_stream.

* For parse, each type X must specialize parse_manip<X,CharT,Traits>.
  Each specialization must include a public typedef to itself named
  type.

* Each parse_manip specialization must have a stream extraction
  operator.

* This commit depends on expression-SFINAE.  If this commit breaks
  your build, it is likely that your compiler/version does not
  support expression-SFINAE.  To fix this NO_EXPRESSION_SFINAE
  needs to be defined in the configuration area of date.h for
  that compiler/version.
This commit is contained in:
Howard Hinnant 2017-02-26 14:10:10 -05:00
parent ffc8cd0a3b
commit 1e7e7a214d
4 changed files with 513 additions and 942 deletions

691
date.h
View File

@ -3386,6 +3386,22 @@ operator/(const month_weekday_last& mwdl, int y) NOEXCEPT
return year(y) / mwdl; return year(y) / mwdl;
} }
template <class Duration>
struct fields;
template <class CharT, class Traits, class Duration>
void
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>
void
parse(std::basic_istream<CharT, Traits>& is,
const CharT* fmt, fields<Duration>& fds,
std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr);
// time_of_day // time_of_day
enum {am = 1, pm}; enum {am = 1, pm};
@ -3531,22 +3547,6 @@ public:
} }
}; };
template <class Duration>
struct fields;
template <class CharT, class Traits, class Duration>
void
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>
void
parse(std::basic_istream<CharT, Traits>& is,
const CharT* fmt, fields<Duration>& fds,
std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr);
enum class classify enum class classify
{ {
not_valid, not_valid,
@ -3911,15 +3911,15 @@ public:
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
friend friend
void void
detail::to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, date::to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const detail::fields<Duration>& fds, const std::string* abbrev, const fields<Duration>& fds, const std::string* abbrev,
const std::chrono::seconds* offset_sec); const std::chrono::seconds* offset_sec);
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
friend friend
void void
detail::parse(std::basic_istream<CharT, Traits>& is, const CharT* fmt, date::parse(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
detail::fields<Duration>& fds, fields<Duration>& fds,
std::basic_string<CharT, Traits>* abbrev, std::chrono::minutes* offset); std::basic_string<CharT, Traits>* abbrev, std::chrono::minutes* offset);
}; };
@ -4015,15 +4015,15 @@ public:
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
friend friend
void void
detail::to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt, date::to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const detail::fields<Duration>& fds, const std::string* abbrev, const fields<Duration>& fds, const std::string* abbrev,
const std::chrono::seconds* offset_sec); const std::chrono::seconds* offset_sec);
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
friend friend
void void
detail::parse(std::basic_istream<CharT, Traits>& is, const CharT* fmt, date::parse(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
detail::fields<Duration>& fds, fields<Duration>& fds,
std::basic_string<CharT, Traits>* abbrev, std::chrono::minutes* offset); std::basic_string<CharT, Traits>* abbrev, std::chrono::minutes* offset);
}; };
@ -4130,9 +4130,6 @@ operator<<(std::basic_ostream<CharT, Traits>& os, const local_time<Duration>& ut
// to_stream // to_stream
namespace detail
{
template <class Duration> template <class Duration>
struct fields struct fields
{ {
@ -4969,7 +4966,16 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
os << modified; os << modified;
} }
} // namespace detail template <class CharT, class Traits>
inline
void
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, time_of_day<CT>{}};
to_stream(os, fmt, fds);
}
template <class CharT, class Traits, class Rep, class Period> template <class CharT, class Traits, class Rep, class Period>
inline inline
@ -4979,8 +4985,8 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
{ {
using Duration = std::chrono::duration<Rep, Period>; using Duration = std::chrono::duration<Rep, Period>;
using CT = typename std::common_type<Duration, std::chrono::seconds>::type; using CT = typename std::common_type<Duration, std::chrono::seconds>::type;
detail::fields<Duration> fds{year_month_day{}, time_of_day<CT>{d}}; fields<Duration> fds{year_month_day{}, time_of_day<CT>{d}};
detail::to_stream(os, fmt, fds); to_stream(os, fmt, fds);
} }
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
@ -4991,8 +4997,8 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
{ {
using CT = typename std::common_type<Duration, std::chrono::seconds>::type; using CT = typename std::common_type<Duration, std::chrono::seconds>::type;
auto ld = floor<days>(tp); auto ld = floor<days>(tp);
detail::fields<CT> fds{year_month_day{ld}, time_of_day<CT>{tp-ld}}; fields<CT> fds{year_month_day{ld}, time_of_day<CT>{tp-ld}};
detail::to_stream(os, fmt, fds, abbrev, offset_sec); to_stream(os, fmt, fds, abbrev, offset_sec);
} }
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
@ -5004,14 +5010,84 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const std::string abbrev("UTC"); const std::string abbrev("UTC");
CONSTDATA std::chrono::seconds offset{0}; CONSTDATA std::chrono::seconds offset{0};
auto sd = floor<days>(tp); auto sd = floor<days>(tp);
detail::fields<CT> fds{year_month_day{sd}, time_of_day<CT>{tp-sd}}; fields<CT> fds{year_month_day{sd}, time_of_day<CT>{tp-sd}};
detail::to_stream(os, fmt, fds, &abbrev, &offset); to_stream(os, fmt, fds, &abbrev, &offset);
} }
// format // format
#ifndef NO_EXPRESSION_SFINAE
template <class CharT, class Streamable>
auto
format(const std::locale& loc, const CharT* fmt, const Streamable& tp)
-> decltype(to_stream(std::declval<std::basic_ostream<CharT>&>(), fmt, tp),
std::basic_string<CharT>{})
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Streamable>
auto
format(const CharT* fmt, const Streamable& tp)
-> decltype(to_stream(std::declval<std::basic_ostream<CharT>&>(), fmt, tp),
std::basic_string<CharT>{})
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Traits, class Streamable>
auto
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const Streamable& tp)
-> decltype(to_stream(std::declval<std::basic_ostream<CharT, Traits>&>(), fmt, tp),
std::basic_string<CharT, Traits>{})
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Traits, class Streamable>
auto
format(const std::basic_string<CharT, Traits>& fmt, const Streamable& tp)
-> decltype(to_stream(std::declval<std::basic_ostream<CharT, Traits>&>(), fmt, tp),
std::basic_string<CharT, Traits>{})
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), tp);
return os.str();
}
#else // NO_EXPRESSION_SFINAE
// const CharT* formats // const CharT* formats
template <class CharT>
std::basic_string<CharT>
format(const std::locale& loc, const CharT* fmt, const year_month_day& ymd)
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, ymd);
return os.str();
}
template <class CharT>
std::basic_string<CharT>
format(const CharT* fmt, const year_month_day& ymd)
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, ymd);
return os.str();
}
template <class CharT, class Duration> template <class CharT, class Duration>
std::basic_string<CharT> std::basic_string<CharT>
format(const std::locale& loc, const CharT* fmt, const local_time<Duration>& tp) format(const std::locale& loc, const CharT* fmt, const local_time<Duration>& tp)
@ -5072,6 +5148,26 @@ format(const CharT* fmt, const std::chrono::duration<Rep, Period>& d)
// basic_string formats // basic_string formats
template <class CharT, class Traits>
std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const year_month_day& ymd)
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), ymd);
return os.str();
}
template <class CharT, class Traits>
std::basic_string<CharT, Traits>
format(const std::basic_string<CharT, Traits>& fmt, const year_month_day& ymd)
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), ymd);
return os.str();
}
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits> std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt, format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
@ -5133,6 +5229,8 @@ format(const std::basic_string<CharT, Traits>& fmt,
return os.str(); return os.str();
} }
#endif // NO_EXPRESSION_SFINAE
// parse // parse
namespace detail namespace detail
@ -5366,6 +5464,8 @@ read(std::basic_istream<CharT, Traits>& is, rld a0, Args&& ...args)
read(is, std::forward<Args>(args)...); read(is, std::forward<Args>(args)...);
} }
} // namespace detail;
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
void void
parse(std::basic_istream<CharT, Traits>& is, const CharT* fmt, fields<Duration>& fds, parse(std::basic_istream<CharT, Traits>& is, const CharT* fmt, fields<Duration>& fds,
@ -6255,7 +6355,7 @@ parse(std::basic_istream<CharT, Traits>& is, const CharT* fmt, fields<Duration>&
goto broken; goto broken;
} }
fds.tod = time_of_day<Duration>(hours{h} + minutes{min}); fds.tod = time_of_day<Duration>(hours{h} + minutes{min});
fds.tod.s_ = decimal_format_seconds<Duration>{s}; fds.tod.s_ = detail::decimal_format_seconds<Duration>{s};
if (abbrev != nullptr) if (abbrev != nullptr)
*abbrev = std::move(temp_abbrev); *abbrev = std::move(temp_abbrev);
if (offset != nullptr) if (offset != nullptr)
@ -6267,16 +6367,127 @@ broken:
is.setstate(ios_base::failbit); is.setstate(ios_base::failbit);
} }
template <class Duration, class CharT, class Traits = std::char_traits<CharT>> template <class Parsable, class CharT, class Traits = std::char_traits<CharT>>
struct parse_local_manip struct parse_manip;
template <class Parsable, class CharT, class Traits>
inline
typename parse_manip<Parsable, CharT, Traits>::type
parse(const std::basic_string<CharT, Traits>& format, Parsable& tp)
{ {
return {format, tp};
}
template <class Parsable, class CharT, class Traits>
inline
typename parse_manip<Parsable, CharT, Traits>::type
parse(const std::basic_string<CharT, Traits>& format, Parsable& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Parsable, class CharT, class Traits>
inline
typename parse_manip<Parsable, CharT, Traits>::type
parse(const std::basic_string<CharT, Traits>& format, Parsable& tp,
std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Parsable, class CharT, class Traits>
inline
typename parse_manip<Parsable, CharT, Traits>::type
parse(const std::basic_string<CharT, Traits>& format, Parsable& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
// const CharT* formats
template <class Parsable, class CharT>
inline
typename parse_manip<Parsable, CharT>::type
parse(const CharT* format, Parsable& tp)
{
return {format, tp};
}
template <class Parsable, class CharT, class Traits>
inline
typename parse_manip<Parsable, CharT>::type
parse(const CharT* format, Parsable& tp, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Parsable, class CharT>
inline
typename parse_manip<Parsable, CharT>::type
parse(const CharT* format, Parsable& tp, std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Parsable, class CharT, class Traits>
inline
typename parse_manip<Parsable, CharT>::type
parse(const CharT* format, Parsable& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class CharT, class Traits>
struct parse_manip<year_month_day, CharT, Traits>
{
using type = parse_manip;
const std::basic_string<CharT, Traits> format_;
year_month_day& ymd_;
std::basic_string<CharT, Traits>* abbrev_;
std::chrono::minutes* offset_;
parse_manip(std::basic_string<CharT, Traits> format,
year_month_day& ymd, std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
: format_(std::move(format))
, ymd_(ymd)
, abbrev_(abbrev)
, offset_(offset)
{}
};
template <class CharT, class Traits>
inline
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is,
const parse_manip<year_month_day, CharT, Traits>& x)
{
using namespace std;
using namespace std::chrono;
using CT = seconds;
fields<CT> fds{};
parse(is, x.format_.c_str(), fds, x.abbrev_, x.offset_);
if (!fds.ymd.ok())
is.setstate(ios::failbit);
if (!is.fail())
x.ymd_ = fds.ymd;
return is;
}
template <class Duration, class CharT, class Traits>
struct parse_manip<local_time<Duration>, CharT, Traits>
{
using type = parse_manip;
const std::basic_string<CharT, Traits> format_; const std::basic_string<CharT, Traits> format_;
local_time<Duration>& tp_; local_time<Duration>& tp_;
std::basic_string<CharT, Traits>* abbrev_; std::basic_string<CharT, Traits>* abbrev_;
std::chrono::minutes* offset_; std::chrono::minutes* offset_;
public: public:
parse_local_manip(std::basic_string<CharT, Traits> format, parse_manip(std::basic_string<CharT, Traits> format,
local_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr, local_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr) std::chrono::minutes* offset = nullptr)
: format_(std::move(format)) : format_(std::move(format))
@ -6291,7 +6502,7 @@ template <class Duration, class CharT, class Traits>
inline inline
std::basic_istream<CharT, Traits>& std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, operator>>(std::basic_istream<CharT, Traits>& is,
const parse_local_manip<Duration, CharT, Traits>& x) const parse_manip<local_time<Duration>, CharT, Traits>& x)
{ {
using namespace std; using namespace std;
using namespace std::chrono; using namespace std::chrono;
@ -6305,16 +6516,17 @@ operator>>(std::basic_istream<CharT, Traits>& is,
return is; return is;
} }
template <class Duration, class CharT, class Traits = std::char_traits<CharT>> template <class Duration, class CharT, class Traits>
struct parse_sys_manip struct parse_manip<sys_time<Duration>, CharT, Traits>
{ {
using type = parse_manip;
const std::basic_string<CharT, Traits> format_; const std::basic_string<CharT, Traits> format_;
sys_time<Duration>& tp_; sys_time<Duration>& tp_;
std::basic_string<CharT, Traits>* abbrev_; std::basic_string<CharT, Traits>* abbrev_;
std::chrono::minutes* offset_; std::chrono::minutes* offset_;
public: public:
parse_sys_manip(std::basic_string<CharT, Traits> format, parse_manip(std::basic_string<CharT, Traits> format,
sys_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr, sys_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr) std::chrono::minutes* offset = nullptr)
: format_(std::move(format)) : format_(std::move(format))
@ -6328,7 +6540,7 @@ public:
template <class Duration, class CharT, class Traits> template <class Duration, class CharT, class Traits>
std::basic_istream<CharT, Traits>& std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, operator>>(std::basic_istream<CharT, Traits>& is,
const parse_sys_manip<Duration, CharT, Traits>& x) const parse_manip<sys_time<Duration>, CharT, Traits>& x)
{ {
using namespace std; using namespace std;
using namespace std::chrono; using namespace std::chrono;
@ -6345,28 +6557,31 @@ operator>>(std::basic_istream<CharT, Traits>& is,
return is; return is;
} }
template <class Duration, class CharT, class Traits = std::char_traits<CharT>> template <class Rep, class Period, class CharT, class Traits>
struct parse_duration struct parse_manip<std::chrono::duration<Rep, Period>, CharT, Traits>
{ {
using type = parse_manip;
using Duration = std::chrono::duration<Rep, Period>;
const std::basic_string<CharT, Traits> format_; const std::basic_string<CharT, Traits> format_;
Duration& d_; Duration& d_;
public: public:
parse_duration(std::basic_string<CharT, Traits> format, Duration& d) parse_manip(std::basic_string<CharT, Traits> format, Duration& d)
: format_(std::move(format)) : format_(std::move(format))
, d_(d) , d_(d)
{} {}
}; };
template <class Duration, class CharT, class Traits> template <class Rep, class Period, class CharT, class Traits>
inline inline
std::basic_istream<CharT, Traits>& std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, operator>>(std::basic_istream<CharT, Traits>& is,
const parse_duration<Duration, CharT, Traits>& x) const parse_manip<std::chrono::duration<Rep, Period>, CharT, Traits>& x)
{ {
using namespace std; using namespace std;
using namespace std::chrono; using namespace std::chrono;
using Duration = std::chrono::duration<Rep, Period>;
using CT = typename common_type<Duration, seconds>::type; using CT = typename common_type<Duration, seconds>::type;
fields<CT> fds{}; fields<CT> fds{};
parse(is, x.format_.c_str(), fds); parse(is, x.format_.c_str(), fds);
@ -6377,105 +6592,6 @@ operator>>(std::basic_istream<CharT, Traits>& is,
return is; return is;
} }
} // namespace detail
template <class Duration, class CharT, class Traits>
inline
detail::parse_sys_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_sys_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_sys_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_sys_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_sys_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, sys_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_local_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_local_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_local_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp,
std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_local_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_local_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, local_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
template <class Rep, class Period, class CharT, class Traits>
inline
detail::parse_duration<std::chrono::duration<Rep, Period>, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format,
std::chrono::duration<Rep, Period>& d)
{
return {format, d};
}
#if 0 // use in >> parse(format, x); instead #if 0 // use in >> parse(format, x); instead
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
@ -6593,106 +6709,6 @@ parse(std::basic_istream<CharT, Traits>& is,
detail::parse(is, format.c_str(), tp, &abbrev, &offset); detail::parse(is, format.c_str(), tp, &abbrev, &offset);
} }
#endif // use in >> parse(format, x); instead
// const CharT* formats
template <class Duration, class CharT>
inline
detail::parse_sys_manip<Duration, CharT>
parse(const CharT* format, sys_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_sys_manip<Duration, CharT, Traits>
parse(const CharT* format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT>
inline
detail::parse_sys_manip<Duration, CharT>
parse(const CharT* format, sys_time<Duration>& tp, std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_sys_manip<Duration, CharT, Traits>
parse(const CharT* format, sys_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_sys_manip<Duration, CharT, Traits>
parse(const CharT* format, sys_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT>
inline
detail::parse_local_manip<Duration, CharT>
parse(const CharT* format, local_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_local_manip<Duration, CharT, Traits>
parse(const CharT* format, local_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT>
inline
detail::parse_local_manip<Duration, CharT>
parse(const CharT* format, local_time<Duration>& tp, std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_local_manip<Duration, CharT, Traits>
parse(const CharT* format, local_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_local_manip<Duration, CharT, Traits>
parse(const CharT* format, local_time<Duration>& tp, std::chrono::minutes& offset,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
template <class Rep, class Period, class CharT>
inline
detail::parse_duration<std::chrono::duration<Rep, Period>, CharT>
parse(const CharT* format, std::chrono::duration<Rep, Period>& d)
{
return {format, d};
}
#if 0 // use in >> parse(format, x); instead
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
inline inline
void void
@ -6803,187 +6819,6 @@ parse(std::basic_istream<CharT, Traits>& is, const CharT* format,
#endif // use in >> parse(format, x); instead #endif // use in >> parse(format, x); instead
template <class CharT, class Traits>
void
to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
const year_month_day& ymd)
{
using CT = std::chrono::seconds;
detail::fields<CT> fds{ymd, time_of_day<CT>{}};
detail::to_stream(os, fmt, fds);
}
template <class CharT>
std::basic_string<CharT>
format(const std::locale& loc, const CharT* fmt, const year_month_day& ymd)
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, ymd);
return os.str();
}
template <class CharT>
std::basic_string<CharT>
format(const CharT* fmt, const year_month_day& ymd)
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, ymd);
return os.str();
}
template <class CharT, class Traits>
std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const year_month_day& ymd)
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), ymd);
return os.str();
}
template <class CharT, class Traits>
std::basic_string<CharT, Traits>
format(const std::basic_string<CharT, Traits>& fmt, const year_month_day& ymd)
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), ymd);
return os.str();
}
namespace detail
{
template <class CharT, class Traits = std::char_traits<CharT>>
struct parse_ymd_manip
{
const std::basic_string<CharT, Traits> format_;
year_month_day& ymd_;
std::basic_string<CharT, Traits>* abbrev_;
std::chrono::minutes* offset_;
public:
parse_ymd_manip(std::basic_string<CharT, Traits> format,
year_month_day& ymd, std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
: format_(std::move(format))
, ymd_(ymd)
, abbrev_(abbrev)
, offset_(offset)
{}
};
template <class CharT, class Traits>
inline
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is,
const parse_ymd_manip<CharT, Traits>& x)
{
using namespace std;
using namespace std::chrono;
using CT = seconds;
fields<CT> fds{};
parse(is, x.format_.c_str(), fds, x.abbrev_, x.offset_);
if (!fds.ymd.ok())
is.setstate(ios::failbit);
if (!is.fail())
x.ymd_ = fds.ymd;
return is;
}
} // namespace detail
template <class CharT, class Traits>
inline
detail::parse_ymd_manip<CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, year_month_day& ymd)
{
return {format, ymd};
}
template <class CharT, class Traits>
inline
detail::parse_ymd_manip<CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, year_month_day& ymd,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, ymd, &abbrev};
}
template <class CharT, class Traits>
inline
detail::parse_ymd_manip<CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, year_month_day& ymd,
std::chrono::minutes& offset)
{
return {format, ymd, nullptr, &offset};
}
template <class CharT, class Traits>
inline
detail::parse_ymd_manip<CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, year_month_day& ymd,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, ymd, &abbrev, &offset};
}
template <class CharT, class Traits>
inline
detail::parse_ymd_manip<CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, year_month_day& ymd,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, ymd, &abbrev, &offset};
}
// const CharT* formats
template <class CharT>
inline
detail::parse_ymd_manip<CharT>
parse(const CharT* format, year_month_day& ymd)
{
return {format, ymd};
}
template <class CharT, class Traits>
inline
detail::parse_ymd_manip<CharT, Traits>
parse(const CharT* format, year_month_day& ymd,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, ymd, &abbrev};
}
template <class CharT>
inline
detail::parse_ymd_manip<CharT>
parse(const CharT* format, year_month_day& ymd, std::chrono::minutes& offset)
{
return {format, ymd, nullptr, &offset};
}
template <class CharT, class Traits>
inline
detail::parse_ymd_manip<CharT, Traits>
parse(const CharT* format, year_month_day& ymd,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, ymd, &abbrev, &offset};
}
template <class CharT, class Traits>
inline
detail::parse_ymd_manip<CharT, Traits>
parse(const CharT* format, year_month_day& ymd,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, ymd, &abbrev, &offset};
}
} // namespace date } // namespace date
#endif // DATE_H #endif // DATE_H

View File

@ -67,4 +67,12 @@ main()
os.str(""); os.str("");
os << format("%F %T", sys_days{dec/last/year::max()} + microfortnights{1}); os << format("%F %T", sys_days{dec/last/year::max()} + microfortnights{1});
assert(os.str() == "32767-12-31 00:00:01.2096"); assert(os.str() == "32767-12-31 00:00:01.2096");
os.str("");
os << format("%F %T", jan/1/year::min());
assert(os.str() == "-32768-01-01 00:00:00");
os.str("");
os << format("%F %T", dec/last/year::max());
assert(os.str() == "32767-12-31 00:00:00");
os.str("");
} }

View File

@ -390,6 +390,14 @@ test_F()
assert(!in.bad()); assert(!in.bad());
assert(tp == 2016_y/12/13); assert(tp == 2016_y/12/13);
} }
{
std::istringstream in{"2016-12-13"};
year_month_day tp;
in >> parse("%F", tp);
assert(!in.fail());
assert(!in.bad());
assert(tp == 2016_y/12/13);
}
} }
void void

748
tz.h
View File

@ -1036,14 +1036,22 @@ make_zoned(const std::string& name, const sys_time<Duration>& st)
return {name, st}; return {name, st};
} }
template <class CharT, class Traits, class Duration>
void
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);
}
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
inline inline
std::basic_ostream<CharT, Traits>& std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& 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_); to_stream(os, "%F %T %Z", t);
auto lt = t.tp_ + i.offset; return os;
return os << lt << ' ' << i.abbrev;
} }
class utc_clock class utc_clock
@ -1138,8 +1146,8 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
ymd = sd; ymd = sd;
time = make_time(tp - sd); time = make_time(tp - sd);
time.seconds() += ls; time.seconds() += ls;
detail::fields<CT> fds{ymd, time}; fields<CT> fds{ymd, time};
detail::to_stream(os, fmt, fds, &abbrev, &offset); to_stream(os, fmt, fds, &abbrev, &offset);
} }
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
@ -1150,60 +1158,19 @@ operator<<(std::basic_ostream<CharT, Traits>& os, const utc_time<Duration>& t)
return os; return os;
} }
template <class CharT, class Duration> template <class Duration, class CharT, class Traits>
std::basic_string<CharT> struct parse_manip<utc_time<Duration>, CharT, Traits>
format(const std::locale& loc, const CharT* fmt, const utc_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const CharT* fmt, const utc_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const utc_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::basic_string<CharT, Traits>& fmt, const utc_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), tp);
return os.str();
}
namespace detail
{
template <class Duration, class CharT, class Traits = std::char_traits<CharT>>
struct parse_utc_manip
{ {
using type = parse_manip;
const std::basic_string<CharT, Traits> format_; const std::basic_string<CharT, Traits> format_;
utc_time<Duration>& tp_; utc_time<Duration>& tp_;
std::basic_string<CharT, Traits>* abbrev_; std::basic_string<CharT, Traits>* abbrev_;
std::chrono::minutes* offset_; std::chrono::minutes* offset_;
public: public:
parse_utc_manip(std::basic_string<CharT, Traits> format, parse_manip(std::basic_string<CharT, Traits> format,
utc_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr, utc_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr) std::chrono::minutes* offset = nullptr)
: format_(std::move(format)) : format_(std::move(format))
, tp_(tp) , tp_(tp)
, abbrev_(abbrev) , abbrev_(abbrev)
@ -1215,7 +1182,7 @@ public:
template <class Duration, class CharT, class Traits> template <class Duration, class CharT, class Traits>
std::basic_istream<CharT, Traits>& std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, operator>>(std::basic_istream<CharT, Traits>& is,
const parse_utc_manip<Duration, CharT, Traits>& x) const parse_manip<utc_time<Duration>, CharT, Traits>& x)
{ {
using namespace std; using namespace std;
using namespace std::chrono; using namespace std::chrono;
@ -1239,97 +1206,6 @@ operator>>(std::basic_istream<CharT, Traits>& is,
return is; return is;
} }
} // namespace detail
template <class Duration, class CharT, class Traits>
inline
detail::parse_utc_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, utc_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_utc_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, utc_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_utc_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, utc_time<Duration>& tp,
std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_utc_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, utc_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_utc_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, utc_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
// const CharT* formats
template <class Duration, class CharT>
inline
detail::parse_utc_manip<Duration, CharT>
parse(const CharT* format, utc_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_utc_manip<Duration, CharT, Traits>
parse(const CharT* format, utc_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT>
inline
detail::parse_utc_manip<Duration, CharT>
parse(const CharT* format, utc_time<Duration>& tp, std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_utc_manip<Duration, CharT, Traits>
parse(const CharT* format, utc_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_utc_manip<Duration, CharT, Traits>
parse(const CharT* format, utc_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
// tai_clock // tai_clock
class tai_clock class tai_clock
@ -1402,8 +1278,8 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
auto const sd = floor<days>(tp); auto const sd = floor<days>(tp);
year_month_day ymd = sd; year_month_day ymd = sd;
auto time = make_time(tp - sd); auto time = make_time(tp - sd);
detail::fields<CT> fds{ymd, time}; fields<CT> fds{ymd, time};
detail::to_stream(os, fmt, fds, &abbrev, &offset); to_stream(os, fmt, fds, &abbrev, &offset);
} }
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
@ -1414,60 +1290,19 @@ operator<<(std::basic_ostream<CharT, Traits>& os, const tai_time<Duration>& t)
return os; return os;
} }
template <class CharT, class Duration> template <class Duration, class CharT, class Traits>
std::basic_string<CharT> struct parse_manip<tai_time<Duration>, CharT, Traits>
format(const std::locale& loc, const CharT* fmt, const tai_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const CharT* fmt, const tai_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const tai_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::basic_string<CharT, Traits>& fmt, const tai_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), tp);
return os.str();
}
namespace detail
{
template <class Duration, class CharT, class Traits = std::char_traits<CharT>>
struct parse_tai_manip
{ {
using type = parse_manip;
const std::basic_string<CharT, Traits> format_; const std::basic_string<CharT, Traits> format_;
tai_time<Duration>& tp_; tai_time<Duration>& tp_;
std::basic_string<CharT, Traits>* abbrev_; std::basic_string<CharT, Traits>* abbrev_;
std::chrono::minutes* offset_; std::chrono::minutes* offset_;
public: public:
parse_tai_manip(std::basic_string<CharT, Traits> format, parse_manip(std::basic_string<CharT, Traits> format,
tai_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr, tai_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr) std::chrono::minutes* offset = nullptr)
: format_(std::move(format)) : format_(std::move(format))
, tp_(tp) , tp_(tp)
, abbrev_(abbrev) , abbrev_(abbrev)
@ -1479,7 +1314,7 @@ public:
template <class Duration, class CharT, class Traits> template <class Duration, class CharT, class Traits>
std::basic_istream<CharT, Traits>& std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, operator>>(std::basic_istream<CharT, Traits>& is,
const parse_tai_manip<Duration, CharT, Traits>& x) const parse_manip<tai_time<Duration>, CharT, Traits>& x)
{ {
using namespace std; using namespace std;
using namespace std::chrono; using namespace std::chrono;
@ -1497,97 +1332,6 @@ operator>>(std::basic_istream<CharT, Traits>& is,
return is; return is;
} }
} // namespace detail
template <class Duration, class CharT, class Traits>
inline
detail::parse_tai_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, tai_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_tai_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, tai_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_tai_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, tai_time<Duration>& tp,
std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_tai_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, tai_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_tai_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, tai_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
// const CharT* formats
template <class Duration, class CharT>
inline
detail::parse_tai_manip<Duration, CharT>
parse(const CharT* format, tai_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_tai_manip<Duration, CharT, Traits>
parse(const CharT* format, tai_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT>
inline
detail::parse_tai_manip<Duration, CharT>
parse(const CharT* format, tai_time<Duration>& tp, std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_tai_manip<Duration, CharT, Traits>
parse(const CharT* format, tai_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_tai_manip<Duration, CharT, Traits>
parse(const CharT* format, tai_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
// gps_clock // gps_clock
class gps_clock class gps_clock
@ -1660,8 +1404,8 @@ to_stream(std::basic_ostream<CharT, Traits>& os, const CharT* fmt,
auto const sd = floor<days>(tp); auto const sd = floor<days>(tp);
year_month_day ymd = sd; year_month_day ymd = sd;
auto time = make_time(tp - sd); auto time = make_time(tp - sd);
detail::fields<CT> fds{ymd, time}; fields<CT> fds{ymd, time};
detail::to_stream(os, fmt, fds, &abbrev, &offset); to_stream(os, fmt, fds, &abbrev, &offset);
} }
template <class CharT, class Traits, class Duration> template <class CharT, class Traits, class Duration>
@ -1672,6 +1416,208 @@ operator<<(std::basic_ostream<CharT, Traits>& os, const gps_time<Duration>& t)
return os; return os;
} }
template <class Duration, class CharT, class Traits>
struct parse_manip<gps_time<Duration>, CharT, Traits>
{
using type = parse_manip;
const std::basic_string<CharT, Traits> format_;
gps_time<Duration>& tp_;
std::basic_string<CharT, Traits>* abbrev_;
std::chrono::minutes* offset_;
public:
parse_manip(std::basic_string<CharT, Traits> format,
gps_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
: format_(std::move(format))
, tp_(tp)
, abbrev_(abbrev)
, offset_(offset)
{}
};
template <class Duration, class CharT, class Traits>
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is,
const parse_manip<gps_time<Duration>, CharT, Traits>& x)
{
using namespace std;
using namespace std::chrono;
using CT = typename common_type<Duration, seconds>::type;
minutes offset{};
auto offptr = x.offset_ ? x.offset_ : &offset;
fields<CT> fds{};
parse(is, x.format_.c_str(), fds, x.abbrev_, offptr);
if (!fds.ymd.ok())
is.setstate(ios::failbit);
if (!is.fail())
x.tp_ = gps_time<Duration>{duration_cast<Duration>(
(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>
inline
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys_time(const tai_time<Duration>& t)
{
return to_sys_time(to_utc_time(t));
}
template <class Duration>
inline
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys_time(const gps_time<Duration>& t)
{
return to_sys_time(to_utc_time(t));
}
template <class Duration>
inline
tai_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_tai_time(const gps_time<Duration>& t) NOEXCEPT
{
using namespace std::chrono;
using duration = typename std::common_type<Duration, seconds>::type;
return tai_time<duration>{t.time_since_epoch()} +
(sys_days(year{1980}/jan/sun[1]) - sys_days(year{1958}/jan/1) + seconds{19});
}
template <class Duration>
inline
gps_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_gps_time(const tai_time<Duration>& t) NOEXCEPT
{
using namespace std::chrono;
using duration = typename std::common_type<Duration, seconds>::type;
return gps_time<duration>{t.time_since_epoch()} -
(sys_days(year{1980}/jan/sun[1]) - sys_days(year{1958}/jan/1) + seconds{19});
}
#ifdef NO_EXPRESSION_SFINAE
// format
// basic_string formats
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const zoned_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::basic_string<CharT, Traits>& fmt, const zoned_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const std::locale& loc, const CharT* fmt, const zoned_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const CharT* fmt, const zoned_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const std::locale& loc, const CharT* fmt, const utc_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const CharT* fmt, const utc_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const utc_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::basic_string<CharT, Traits>& fmt, const utc_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const std::locale& loc, const CharT* fmt, const tai_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const CharT* fmt, const tai_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const tai_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::basic_string<CharT, Traits>& fmt, const tai_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Duration> template <class CharT, class Duration>
std::basic_string<CharT> std::basic_string<CharT>
format(const std::locale& loc, const CharT* fmt, const gps_time<Duration>& tp) format(const std::locale& loc, const CharT* fmt, const gps_time<Duration>& tp)
@ -1711,233 +1657,7 @@ format(const std::basic_string<CharT, Traits>& fmt, const gps_time<Duration>& tp
return os.str(); return os.str();
} }
namespace detail #endif // NO_EXPRESSION_SFINAE
{
template <class Duration, class CharT, class Traits = std::char_traits<CharT>>
struct parse_gps_manip
{
const std::basic_string<CharT, Traits> format_;
gps_time<Duration>& tp_;
std::basic_string<CharT, Traits>* abbrev_;
std::chrono::minutes* offset_;
public:
parse_gps_manip(std::basic_string<CharT, Traits> format,
gps_time<Duration>& tp, std::basic_string<CharT, Traits>* abbrev = nullptr,
std::chrono::minutes* offset = nullptr)
: format_(std::move(format))
, tp_(tp)
, abbrev_(abbrev)
, offset_(offset)
{}
};
template <class Duration, class CharT, class Traits>
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is,
const parse_gps_manip<Duration, CharT, Traits>& x)
{
using namespace std;
using namespace std::chrono;
using CT = typename common_type<Duration, seconds>::type;
minutes offset{};
auto offptr = x.offset_ ? x.offset_ : &offset;
fields<CT> fds{};
parse(is, x.format_.c_str(), fds, x.abbrev_, offptr);
if (!fds.ymd.ok())
is.setstate(ios::failbit);
if (!is.fail())
x.tp_ = gps_time<Duration>{duration_cast<Duration>(
(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;
}
} // namespace detail
template <class Duration, class CharT, class Traits>
inline
detail::parse_gps_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, gps_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_gps_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, gps_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_gps_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, gps_time<Duration>& tp,
std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_gps_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, gps_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_gps_manip<Duration, CharT, Traits>
parse(const std::basic_string<CharT, Traits>& format, gps_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
// const CharT* formats
template <class Duration, class CharT>
inline
detail::parse_gps_manip<Duration, CharT>
parse(const CharT* format, gps_time<Duration>& tp)
{
return {format, tp};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_gps_manip<Duration, CharT, Traits>
parse(const CharT* format, gps_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev};
}
template <class Duration, class CharT>
inline
detail::parse_gps_manip<Duration, CharT>
parse(const CharT* format, gps_time<Duration>& tp, std::chrono::minutes& offset)
{
return {format, tp, nullptr, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_gps_manip<Duration, CharT, Traits>
parse(const CharT* format, gps_time<Duration>& tp,
std::basic_string<CharT, Traits>& abbrev, std::chrono::minutes& offset)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration, class CharT, class Traits>
inline
detail::parse_gps_manip<Duration, CharT, Traits>
parse(const CharT* format, gps_time<Duration>& tp,
std::chrono::minutes& offset, std::basic_string<CharT, Traits>& abbrev)
{
return {format, tp, &abbrev, &offset};
}
template <class Duration>
inline
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys_time(const tai_time<Duration>& t)
{
return to_sys_time(to_utc_time(t));
}
template <class Duration>
inline
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys_time(const gps_time<Duration>& t)
{
return to_sys_time(to_utc_time(t));
}
template <class Duration>
inline
tai_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_tai_time(const gps_time<Duration>& t) NOEXCEPT
{
using namespace std::chrono;
using duration = typename std::common_type<Duration, seconds>::type;
return tai_time<duration>{t.time_since_epoch()} +
(sys_days(year{1980}/jan/sun[1]) - sys_days(year{1958}/jan/1) + seconds{19});
}
template <class Duration>
inline
gps_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_gps_time(const tai_time<Duration>& t) NOEXCEPT
{
using namespace std::chrono;
using duration = typename std::common_type<Duration, seconds>::type;
return gps_time<duration>{t.time_since_epoch()} -
(sys_days(year{1980}/jan/sun[1]) - sys_days(year{1958}/jan/1) + seconds{19});
}
// format
template <class CharT, class Traits, class Duration>
void
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);
}
// basic_string formats
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::locale& loc, const std::basic_string<CharT, Traits>& fmt,
const zoned_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
os.imbue(loc);
to_stream(os, fmt.c_str(), tp);
return os.str();
}
template <class CharT, class Traits, class Duration>
std::basic_string<CharT, Traits>
format(const std::basic_string<CharT, Traits>& fmt, const zoned_time<Duration>& tp)
{
std::basic_ostringstream<CharT, Traits> os;
to_stream(os, fmt.c_str(), tp);
return os.str();
}
// const CharT* formats
template <class CharT, class Duration>
std::basic_string<CharT>
format(const std::locale& loc, const CharT* fmt, const zoned_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
os.imbue(loc);
to_stream(os, fmt, tp);
return os.str();
}
template <class CharT, class Duration>
std::basic_string<CharT>
format(const CharT* fmt, const zoned_time<Duration>& tp)
{
std::basic_ostringstream<CharT> os;
to_stream(os, fmt, tp);
return os.str();
}
} // namespace date } // namespace date