Replace boost/date_time with c++11 in util.h
[novacoin.git] / src / util.h
index c0e35c4..d787141 100644 (file)
@@ -24,8 +24,6 @@
 #include <boost/thread.hpp>
 #include <boost/filesystem.hpp>
 #include <boost/filesystem/path.hpp>
-#include <boost/date_time/gregorian/gregorian_types.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
 #endif
 
 #include <stdarg.h>
@@ -48,12 +46,6 @@ static const int64_t CENT = 10000;
 #define END(a)              ((char*)&((&(a))[1]))
 #define UBEGIN(a)           ((unsigned char*)&(a))
 #define UEND(a)             ((unsigned char*)&((&(a))[1]))
-#define ARRAYLEN(array)     (sizeof(array)/sizeof((array)[0]))
-
-#define UVOIDBEGIN(a)        ((void*)&(a))
-#define CVOIDBEGIN(a)        ((const void*)&(a))
-#define UINTBEGIN(a)        ((uint32_t*)&(a))
-#define CUINTBEGIN(a)        ((const uint32_t*)&(a))
 
 #ifndef THROW_WITH_STACKTRACE
 #define THROW_WITH_STACKTRACE(exception)  \
@@ -97,9 +89,6 @@ void LogStackTrace();
   #define PRIpdd    "td"
 #endif
 
-// This is needed because the foreach macro can't get over the comma in pair<t1, t2>
-#define PAIRTYPE(t1, t2)    std::pair<t1, t2>
-
 // Align by increasing pointer, must have extra space at end of buffer
 template <size_t nBytes, typename T>
 T* alignup(T* p)
@@ -126,7 +115,7 @@ T* alignup(T* p)
 #define MAX_PATH            1024
 inline void Sleep(int64_t n)
 {
-    this_thread::sleep_for(std::chrono::seconds(n));
+    this_thread::sleep_for(std::chrono::milliseconds(n));
 }
 #endif
 
@@ -201,8 +190,8 @@ void ParseString(const std::string& str, char c, std::vector<std::string>& v);
 std::string FormatMoney(int64_t n, bool fPlus=false);
 bool ParseMoney(const std::string& str, int64_t& nRet);
 bool ParseMoney(const char* pszIn, int64_t& nRet);
-std::vector<unsigned char> ParseHex(const char* psz);
-std::vector<unsigned char> ParseHex(const std::string& str);
+std::vector<uint8_t> ParseHex(const char *str);
+inline std::vector<uint8_t> ParseHex(const std::string& str) { return ParseHex(str.c_str()); }
 bool IsHex(const std::string& str);
 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
 std::string DecodeBase64(const std::string& str);
@@ -237,6 +226,7 @@ void ShrinkDebugFile();
 int GetRandInt(int nMax);
 uint64_t GetRand(uint64_t nMax);
 uint256 GetRandHash();
+void FillRand(uint8_t *buffer, size_t nCount);
 int64_t GetTime();
 int64_t GetTimeMillis();
 int64_t GetTimeMicros();
@@ -253,36 +243,19 @@ void runCommand(std::string strCommand);
 
 
 
-
-
-
-
 inline std::string i64tostr(int64_t n)
 {
     return strprintf("%" PRId64, n);
 }
 
-inline std::string itostr(int n)
-{
-    return strprintf("%d", n);
-}
-
-inline int64_t atoi64(const char* psz)
+inline int64_t strtoll(const char* psz)
 {
-#ifdef _MSC_VER
-    return _atoi64(psz);
-#else
     return strtoll(psz, NULL, 10);
-#endif
 }
 
-inline int64_t atoi64(const std::string& str)
+inline int64_t strtoll(const std::string& str)
 {
-#ifdef _MSC_VER
-    return _atoi64(str.c_str());
-#else
     return strtoll(str.c_str(), NULL, 10);
-#endif
 }
 
 inline int32_t strtol(const char* psz)
@@ -320,11 +293,6 @@ inline int64_t roundint64(double d)
     return (int64_t)(d > 0 ? d + 0.5 : d - 0.5);
 }
 
-inline int64_t abs64(int64_t n)
-{
-    return (n >= 0 ? n : -n);
-}
-
 inline std::string leftTrim(std::string src, char chr)
 {
     std::string::size_type pos = src.find_first_not_of(chr, 0);
@@ -385,14 +353,12 @@ inline int64_t GetPerformanceCounter()
 
 inline int64_t GetTimeMillis()
 {
-    return (boost::posix_time::microsec_clock::universal_time() -
-            boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
+    return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
 }
 
 inline int64_t GetTimeMicros()
 {
-    return (boost::posix_time::microsec_clock::universal_time() -
-                   boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
+    return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
 }
 
 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);