问题
I'm modifying an old project, and at the same time I'm updating several things to bring it up to C++11.
I'd like to replace various uses of boost::date_time with the new functionality in std::chrono. But I cannot figure out what is the C++11 equivalent of boost::date_time::not_a_date_time.
Isn't there an equivalent in C++11 to indicate that a time_point variable hasn't yet been assigned, or doesn't contain a valid timestamp?
回答1:
Given the fact that it exists as part of a group
bool is_infinity() const bool is_neg_infinity() const bool is_pos_infinity() const bool is_not_a_date_time() const
it is pretty clear that this is done by using a floating-point type for the internal representation and setting the value to a NaN (not-a-number).
In std::chrono
, the representation type is required to be an arithmetic type. Floating-point types therefore qualify, and you can use the same trick.
Given a std::duration
, you could then test it using
std::isnan(dur.count())
(Naturally, you should use a quiet NaN value, not a signalling NaN, so you don't trigger floating-point traps)
回答2:
boost::date_time
uses integer time representations internally, and defines the special values inside boost/date_time/int_adapter.hpp:
static const int_adapter pos_infinity()
{
return (::std::numeric_limits<int_type>::max)();
}
static const int_adapter neg_infinity()
{
return (::std::numeric_limits<int_type>::min)();
}
static const int_adapter not_a_number()
{
return (::std::numeric_limits<int_type>::max)()-1;
}
static int_adapter max BOOST_PREVENT_MACRO_SUBSTITUTION ()
{
return (::std::numeric_limits<int_type>::max)()-2;
}
static int_adapter min BOOST_PREVENT_MACRO_SUBSTITUTION ()
{
return (::std::numeric_limits<int_type>::min)()+1;
}
Essentially, it reserves certain integer values to have special meanings.
However, as others have pointed out, std::chrono does not offer these special values (there are only min and max functions); and std::numeric_limits
is also not specialized (see Why does std::numeric_limits<seconds>::max() return 0?).
Ben Voigt's answer presents a possible workaround, but be aware that since the std::chrono
classes do not specify such semantics, handing a NaN timestamp or duration to any function you did not write yourself is likely going to trigger undefined behaviour.
来源:https://stackoverflow.com/questions/25880503/what-is-the-c11-equivalent-to-boostdate-timenot-a-date-time