rad  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
helper.hpp
Go to the documentation of this file.
1 
9 #ifndef RAD_HELPER_HPP
10 #define RAD_HELPER_HPP
11 
12 //#include <cstring>
13 #include <stdio.h>
14 #include <sys/time.h>
15 #include <sstream>
16 #include <string>
17 
18 namespace rad {
19 
23 class Helper {
24  public:
25  static constexpr const char* INTROOT = "INTROOT";
26  static constexpr const char* CFGPATH = "CFGPATH";
27 
28  static std::string GetHostname(const bool with_domain = false);
29 
30  static std::string FindFile(const std::string& filename);
31  static bool FileExists(const std::string& filename);
32  static std::string GetEnvVar(const std::string& key);
33  static std::string CreateIdentity();
34  static void SplitAddrPort(const std::string addr_port, std::string& addr, int& port);
35  static std::string GetVersion();
36 
37  Helper() = delete;
38  Helper(const Helper&) = delete;
39  Helper& operator=(const Helper&) = delete;
40 };
41 
45 inline static double GetTime() {
46  struct timeval tp;
47  gettimeofday(&tp, nullptr);
48  return (tp.tv_sec + (tp.tv_usec / 1e6));
49 }
50 
57 inline static std::string ConvertToIsoTime(const double timestamp) {
58  char tmp_iso_time[80];
59  long seconds = timestamp;
60  strftime(tmp_iso_time, 80, "%Y-%m-%dT%H:%M:%S", localtime(&seconds));
61  char tmp_mus[80];
62  snprintf(tmp_mus, sizeof(tmp_mus), "%.6f", (timestamp - seconds));
63  char iso_time[87];
64  snprintf(iso_time, sizeof(iso_time), "%s.%s", tmp_iso_time,
65  std::string(tmp_mus).substr(2).c_str());
66  return iso_time;
67 }
68 
72 inline static std::string GetTimestamp() { return rad::ConvertToIsoTime(rad::GetTime()); }
73 
74 } // namespace rad
75 
76 #endif // RAD_HELPER_HPP
static constexpr const char * INTROOT
Definition: helper.hpp:25
static std::string GetHostname(const bool with_domain=false)
Definition: helper.cpp:29
static std::string GetEnvVar(const std::string &key)
Definition: helper.cpp:121
static void SplitAddrPort(const std::string addr_port, std::string &addr, int &port)
Definition: helper.cpp:152
static std::string GetVersion()
Definition: helper.cpp:167
static std::string CreateIdentity()
Definition: helper.cpp:135
static std::string FindFile(const std::string &filename)
Definition: helper.cpp:47
Definition: helper.hpp:23
Helper()=delete
static constexpr const char * CFGPATH
Definition: helper.hpp:26
static bool FileExists(const std::string &filename)
Definition: helper.cpp:101
Helper & operator=(const Helper &)=delete