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 21:15:30 +07:00
|
|
|
template <typename Type> auto getTime(void)
|
|
|
|
-> decltype(std::chrono::duration_cast<Type>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch()).count ())
|
|
|
|
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<Type>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch()).count ();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type> auto getTime(uint64_t offset)
|
|
|
|
-> decltype(getTime<Type>()){
|
|
|
|
|
2018-02-28 21:20:29 +07:00
|
|
|
bool Time_Correcting, Time_UseNTP;
|
|
|
|
i2p::config::GetOption("time.correcting", Time_Correcting);
|
|
|
|
i2p::config::GetOption("time.use_ntp", Time_UseNTP);
|
2018-02-28 21:15:30 +07:00
|
|
|
return (Time_Correcting || Time_UseNTP) ? getTime<Type>() + offset : getTime<Type>();
|
|
|
|
}
|
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 ()
|
|
|
|
{
|
2018-02-28 21:15:30 +07:00
|
|
|
return getTime<std::chrono::seconds>(i2p::util::g_TimeOffset);
|
2018-02-28 09:02:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64_t GetMillisecondsSinceEpoch ()
|
|
|
|
{
|
2018-02-28 21:15:30 +07:00
|
|
|
return getTime<std::chrono::milliseconds>(i2p::util::g_TimeOffset*1000);
|
2018-02-28 09:02:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t GetHoursSinceEpoch ()
|
|
|
|
{
|
2018-02-28 21:15:30 +07:00
|
|
|
return getTime<std::chrono::hours>(i2p::util::g_TimeOffset/120);
|
2018-02-28 09:02:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-10 08:10:49 -05:00
|
|
|
}
|
2013-10-27 11:29:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|