2013-10-27 11:29:34 -04:00
|
|
|
#ifndef TIMESTAMP_H__
|
|
|
|
#define TIMESTAMP_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <chrono>
|
2018-02-28 09:02:26 +07:00
|
|
|
#include"Config.h"
|
|
|
|
|
2013-10-27 11:29:34 -04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2018-02-28 09:02:26 +07:00
|
|
|
namespace util
|
2013-10-27 11:29:34 -04:00
|
|
|
{
|
|
|
|
|
2018-02-28 09:02:26 +07:00
|
|
|
static int64_t g_TimeOffset = 0; // in seconds
|
|
|
|
inline void setTimeOffset(int64_t ts){g_TimeOffset=ts;}
|
2013-12-10 08:10:49 -05:00
|
|
|
|
2018-02-28 09:02:26 +07:00
|
|
|
bool timeCorrecting(uint32_t signedOnTime, uint32_t ts, uint32_t skew, const char * ErrorMsg);
|
2018-02-28 09:03:59 +07:00
|
|
|
void SyncTimeWithNTP (void);
|
2018-02-28 09:02:26 +07:00
|
|
|
|
|
|
|
|
|
|
|
inline uint64_t GetSecondsSinceEpoch ()
|
|
|
|
{
|
|
|
|
bool Time_Correcting, Time_UseNTP; i2p::config::GetOption("time.correcting", Time_Correcting);i2p::config::GetOption("time.use_ntp", Time_UseNTP);
|
|
|
|
|
|
|
|
auto tmp_time = std::chrono::duration_cast<std::chrono::seconds>(
|
2018-01-06 12:01:44 +08:00
|
|
|
std::chrono::system_clock::now().time_since_epoch()).count ();
|
2018-02-28 09:02:26 +07:00
|
|
|
|
|
|
|
return (Time_Correcting || Time_UseNTP) ? tmp_time + i2p::util::g_TimeOffset : tmp_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64_t GetMillisecondsSinceEpoch ()
|
|
|
|
{
|
|
|
|
return GetSecondsSinceEpoch()*1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t GetHoursSinceEpoch ()
|
|
|
|
{
|
|
|
|
return GetSecondsSinceEpoch()/120;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-10 08:10:49 -05:00
|
|
|
}
|
2013-10-27 11:29:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|