/*
* This file is part of the Flowee project
* Copyright (C) 2009-2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
* Copyright (C) 2025 Tom Zander
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "utiltime.h"
#include
#include
#include
#include
#include
static int64_t nMockTime = 0; //! For unit testing
int64_t GetTime()
{
if (nMockTime) return nMockTime;
time_t now = time(NULL);
assert(now > 0);
return now;
}
void SetMockTime(int64_t nMockTimeIn)
{
nMockTime = nMockTimeIn;
}
int64_t GetTimeMillis()
{
auto now = std::chrono::system_clock::now();
auto sinceEpoch = now.time_since_epoch();
return std::chrono::duration_cast(sinceEpoch).count();
}
int64_t GetTimeMicros()
{
auto now = std::chrono::system_clock::now();
auto sinceEpoch = now.time_since_epoch();
return std::chrono::duration_cast(sinceEpoch).count();
}
void MilliSleep(int64_t n)
{
std::this_thread::sleep_for(std::chrono::milliseconds(n));
}
std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
{
// std::locale takes ownership of the pointer
std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
std::stringstream ss;
ss.imbue(loc);
ss << boost::posix_time::from_time_t(nTime);
return ss.str();
}