util: Fix conversion of large dates from strings to internal type
Change-Id: I13f8d41892dedd0d2feef131a0f09ae5939d124b Refs: #4478
This commit is contained in:
1 parent
22f856821a
commit
e66040efa6
2 files changed
+28
-14
No files matched your search
+15
-13
@@ -140,17 +140,23 @@ toIsoString(const system_clock::TimePoint& timePoint)
|
||||
return bpt::to_iso_string(ptime);
|
||||
}
|
||||
|
||||
static system_clock::TimePoint
|
||||
convertToTimePoint(const boost::posix_time::ptime& ptime)
|
||||
{
|
||||
namespace bpt = boost::posix_time;
|
||||
static bpt::ptime epoch(boost::gregorian::date(1970, 1, 1));
|
||||
|
||||
// .total_seconds() has issue with large dates until Boost 1.66. See Issue #4478
|
||||
// from_time_t has issues with large dates on 32-bit platforms
|
||||
auto point = system_clock::time_point(seconds((ptime - epoch).ticks() / bpt::time_duration::ticks_per_second()));
|
||||
point += microseconds((ptime - epoch).total_microseconds() % 1000000);
|
||||
return point;
|
||||
}
|
||||
|
||||
system_clock::TimePoint
|
||||
fromIsoString(const std::string& isoString)
|
||||
{
|
||||
namespace bpt = boost::posix_time;
|
||||
static bpt::ptime epoch = bpt::from_time_t(0);
|
||||
|
||||
bpt::ptime ptime = bpt::from_iso_string(isoString);
|
||||
auto point = system_clock::from_time_t((ptime - epoch).total_seconds());
|
||||
point += microseconds((ptime - epoch).total_microseconds() % 1000000);
|
||||
|
||||
return point;
|
||||
return convertToTimePoint(boost::posix_time::from_iso_string(isoString));
|
||||
}
|
||||
|
||||
std::string
|
||||
@@ -178,7 +184,6 @@ fromString(const std::string& timePointStr,
|
||||
const std::locale& locale/* = std::locale("C")*/)
|
||||
{
|
||||
namespace bpt = boost::posix_time;
|
||||
static bpt::ptime epoch = bpt::from_time_t(0);
|
||||
|
||||
bpt::time_input_facet* facet = new bpt::time_input_facet(format);
|
||||
std::istringstream is(timePointStr);
|
||||
@@ -186,10 +191,7 @@ fromString(const std::string& timePointStr,
|
||||
bpt::ptime ptime;
|
||||
is >> ptime;
|
||||
|
||||
auto point = system_clock::from_time_t((ptime - epoch).total_seconds());
|
||||
point += microseconds((ptime - epoch).total_microseconds() % 1000000);
|
||||
|
||||
return point;
|
||||
return convertToTimePoint(ptime);
|
||||
}
|
||||
|
||||
} // namespace time
|
||||
|
||||
@@ -82,9 +82,11 @@ BOOST_AUTO_TEST_CASE(LargeDates)
|
||||
{
|
||||
auto value = fromUnixTimestamp(milliseconds(1390966967032LL));
|
||||
BOOST_CHECK_EQUAL(toIsoString(value), "20140129T034247.032000");
|
||||
BOOST_CHECK_EQUAL(fromIsoString("20140129T034247.032000"), value);
|
||||
|
||||
value += days(365 * 100 + 25 - 1); // 36524 days
|
||||
value += 36524_days;
|
||||
BOOST_CHECK_EQUAL(toIsoString(value), "21140129T034247.032000");
|
||||
BOOST_CHECK_EQUAL(fromIsoString("21140129T034247.032000"), value);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Literals)
|
||||
@@ -115,6 +117,16 @@ BOOST_AUTO_TEST_CASE(Literals)
|
||||
BOOST_CHECK_EQUAL(5.5_ns, 0.0055_us);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Year2038)
|
||||
{
|
||||
auto year2042 = fromIsoString("20420101T000001.042000");
|
||||
auto year2010 = fromIsoString("20100101T000001.042000");
|
||||
|
||||
BOOST_CHECK_EQUAL(to_string(year2010), "1262304001042000000 nanoseconds since Jan 1, 1970");
|
||||
BOOST_CHECK_EQUAL(to_string(year2042), "2272147201042000000 nanoseconds since Jan 1, 1970");
|
||||
BOOST_CHECK_GT(year2042, year2010);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END() // TestTime
|
||||
BOOST_AUTO_TEST_SUITE_END() // Util
|
||||
|
||||
|
||||
Reference in new issue
Block a user