1 /*
2 * Copyright (c) 2013-2020, The PurpleI2P Project
3 *
4 * This file is part of Purple i2pd project and licensed under BSD3
5 *
6 * See full license text in LICENSE file at top of project tree
7 */
8 
9 #ifndef TIMESTAMP_H__
10 #define TIMESTAMP_H__
11 
12 #include <inttypes.h>
13 #include <thread>
14 #include <vector>
15 #include <string>
16 #include <boost/asio.hpp>
17 
18 namespace i2p
19 {
20 namespace util
21 {
22 	uint64_t GetMillisecondsSinceEpoch ();
23 	uint64_t GetSecondsSinceEpoch ();
24 	uint32_t GetMinutesSinceEpoch ();
25 	uint32_t GetHoursSinceEpoch ();
26 
27 	void GetCurrentDate (char * date); // returns date as YYYYMMDD string, 9 bytes
28 	void GetDateString (uint64_t timestamp, char * date); // timestap is seconds since epoch, returns date as YYYYMMDD string, 9 bytes
29 
30 	class NTPTimeSync
31 	{
32 		public:
33 
34 			NTPTimeSync ();
35 			~NTPTimeSync ();
36 
37 			void Start ();
38 			void Stop ();
39 
40 		private:
41 
42 			void Run ();
43 			void Sync ();
44 
45 		private:
46 
47 			bool m_IsRunning;
48 			std::unique_ptr<std::thread> m_Thread;
49 			boost::asio::io_service m_Service;
50 			boost::asio::deadline_timer m_Timer;
51 			int m_SyncInterval;
52 			std::vector<std::string> m_NTPServersList;
53 	};
54 }
55 }
56 
57 #endif
58