mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-27 11:17:49 +02:00
Time experemental options
This commit is contained in:
parent
5a2b795440
commit
ea899fbbfd
17 changed files with 264 additions and 129 deletions
|
@ -3,30 +3,83 @@
|
|||
|
||||
#include <inttypes.h>
|
||||
#include <chrono>
|
||||
#include"Config.h"
|
||||
#include "Log.h"
|
||||
|
||||
namespace i2p
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
inline uint64_t GetMillisecondsSinceEpoch ()
|
||||
namespace util
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count ();
|
||||
}
|
||||
constexpr char NTPMaxTimeConnecting = 15;
|
||||
extern int64_t g_TimeOffset; // in seconds
|
||||
|
||||
inline uint32_t GetHoursSinceEpoch ()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::hours>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count ();
|
||||
}
|
||||
enum class TimeType{
|
||||
milliseconds, seconds, hours
|
||||
};
|
||||
|
||||
inline uint64_t GetSecondsSinceEpoch ()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(
|
||||
template <typename Type> inline uint64_t getTime(void)
|
||||
{
|
||||
return std::chrono::duration_cast<Type>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count ();
|
||||
}
|
||||
|
||||
template <typename Type> auto getTime(TimeType tt) -> decltype(getTime<Type>()){
|
||||
|
||||
|
||||
LogPrint (eLogDebug, "I2Pd Time Correcting: offset = ", g_TimeOffset );
|
||||
|
||||
|
||||
|
||||
bool Time_Correcting, Time_UseNTP;
|
||||
i2p::config::GetOption("time.correcting", Time_Correcting);
|
||||
i2p::config::GetOption("time.use_ntp", Time_UseNTP);
|
||||
if(Time_Correcting || Time_UseNTP)
|
||||
LogPrint (eLogDebug, "I2Pd Time Correcting: Return time with offset ", getTime<Type>() + g_TimeOffset );
|
||||
else
|
||||
LogPrint(eLogDebug, "I2Pd Time Correcting: return real time");
|
||||
if(Time_Correcting || Time_UseNTP){
|
||||
auto tmpTime = getTime<Type>() ;
|
||||
switch(tt){
|
||||
case TimeType::milliseconds:
|
||||
return tmpTime+g_TimeOffset*1000;
|
||||
case TimeType::seconds:
|
||||
return tmpTime+g_TimeOffset;
|
||||
break;
|
||||
case TimeType::hours:
|
||||
if(tmpTime)
|
||||
return tmpTime + g_TimeOffset/120;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return getTime<Type>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool timeCorrecting(uint32_t signedOnTime, uint32_t ts, uint32_t skew, const char * ErrorMsg);
|
||||
bool SyncTimeWithNTP (void);
|
||||
|
||||
|
||||
inline uint64_t GetSecondsSinceEpoch ()
|
||||
{
|
||||
return getTime<std::chrono::seconds>();
|
||||
}
|
||||
|
||||
inline uint64_t GetMillisecondsSinceEpoch ()
|
||||
{
|
||||
return getTime<std::chrono::milliseconds>();
|
||||
}
|
||||
|
||||
inline uint32_t GetHoursSinceEpoch ()
|
||||
{
|
||||
return getTime<std::chrono::hours>();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue