Migrate off of posix_time and most uses of C time_t.

This commit is contained in:
Howard Hinnant
2016-04-26 19:32:57 -04:00
committed by Vinnie Falco
parent 2e2a7509cd
commit 5d9e53a37d
26 changed files with 174 additions and 166 deletions

View File

@@ -18,11 +18,11 @@
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/chrono.h>
#include <ripple/basics/Log.h>
#include <boost/algorithm/string.hpp>
// VFALCO TODO Use std::chrono
#include <boost/date_time/posix_time/posix_time.hpp>
#include <cassert>
#include <iostream>
#include <fstream>
namespace ripple {
@@ -311,8 +311,7 @@ Logs::format (std::string& output, std::string const& message,
{
output.reserve (message.size() + partition.size() + 100);
output = boost::posix_time::to_simple_string (
boost::posix_time::second_clock::universal_time ());
output = to_string(std::chrono::system_clock::now());
output += " ";
if (! partition.empty ())

View File

@@ -18,47 +18,66 @@
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/Time.h>
#include <ripple/beast/clock/chrono_util.h>
#include <ripple/basics/chrono.h>
#include <iomanip>
#include <sstream>
#include <tuple>
namespace ripple {
// VFALCO TODO Tidy this up into a RippleTime object
//
// Time support
// We have our own epoch.
//
boost::posix_time::ptime ptEpoch ()
static
std::tuple<int, unsigned, unsigned>
civil_from_days(int z) noexcept
{
return boost::posix_time::ptime (boost::gregorian::date (2000, boost::gregorian::Jan, 1));
z += 719468;
const int era = (z >= 0 ? z : z - 146096) / 146097;
const unsigned doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
const int y = static_cast<int>(yoe) + era * 400;
const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
const unsigned mp = (5*doy + 2)/153; // [0, 11]
const unsigned d = doy - (153*mp+2)/5 + 1; // [1, 31]
const unsigned m = mp + (mp < 10 ? 3 : -9); // [1, 12]
return std::tuple<int, unsigned, unsigned>(y + (m <= 2), m, d);
}
int iToSeconds (boost::posix_time::ptime ptWhen)
std::string
to_string(std::chrono::system_clock::time_point tp)
{
return ptWhen.is_not_a_date_time ()
? -1
: (ptWhen - ptEpoch ()).total_seconds ();
const char* months[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
using namespace std::chrono;
auto s = floor<seconds>(tp.time_since_epoch());
auto sd = floor<days>(s); // number of days
s -= sd; // time of day in seconds
auto h = floor<hours>(s);
s -= h;
auto m = floor<minutes>(s);
s -= m;
int y;
unsigned mn, d;
std::tie(y, mn, d) = civil_from_days(static_cast<int>(sd.count()));
// Date-time in y/mn/d h:m:s
std::ostringstream str;
str.fill('0');
str.flags(std::ios::dec | std::ios::right);
using std::setw;
str << y << '-' << months[mn-1] << '-' << setw(2) << d << ' '
<< setw(2) << h.count() << ':'
<< setw(2) << m.count() << ':'
<< setw(2) << s.count();
return str.str();
}
// Convert our time in seconds to a ptime.
boost::posix_time::ptime ptFromSeconds (int iSeconds)
std::string
to_string(NetClock::time_point tp)
{
return iSeconds < 0
? boost::posix_time::ptime (boost::posix_time::not_a_date_time)
: ptEpoch () + boost::posix_time::seconds (iSeconds);
}
// Convert from our time to UNIX time in seconds.
uint64_t utFromSeconds (int iSeconds)
{
boost::posix_time::time_duration tdDelta =
boost::posix_time::ptime (boost::gregorian::date (2000, boost::gregorian::Jan, 1))
- boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1))
+ boost::posix_time::seconds (iSeconds)
;
return tdDelta.total_seconds ();
using namespace std::chrono;
return to_string(system_clock::time_point{tp.time_since_epoch() + 946684800s});
}
} // ripple