C++ UTC Time Library
A class to hold UTC times, and associated functionality.
 All Classes Namespaces Files Functions Pages
utctime.h
Go to the documentation of this file.
1 
11 #ifndef PG_UTC_TIME_H
12 #define PG_UTC_TIME_H
13 
14 #include <string>
15 #include <ctime>
16 
17 
19 
20 namespace utctime {
21 
22 
30  private:
31  std::string m_error_message;
32 
33  public:
34 
39  explicit UTCTimeException(const std::string msg =
40  "No error message")
41  : m_error_message(msg) {}
42 
44  virtual ~UTCTimeException() {}
45 
51  const std::string& what() const { return m_error_message; }
52 };
53 
55 
57  public:
58 
60  explicit bad_time_init() :
61  UTCTimeException("Couldn't initialize time") {};
62 };
63 
65 
66 class bad_time : public UTCTimeException {
67  public:
68 
70  explicit bad_time() :
71  UTCTimeException("Couldn't get time") {};
72 };
73 
75 
77  public:
78 
80  explicit invalid_date(const std::string& msg) :
81  UTCTimeException(msg) {};
82 };
83 
84 
85 /*
86  * Standalone functions.
87  */
88 
89 bool validate_date(const int year, const int month,
90  const int day, const int hour,
91  const int minute, const int second);
92 time_t get_day_diff();
93 time_t get_hour_diff();
94 time_t get_sec_diff();
95 int tm_compare(const std::tm& first, const std::tm& second);
96 int tm_intraday_secs_diff(const std::tm& first, const std::tm& second);
97 bool is_leap_year(const int year);
98 std::tm* tm_increment_day(std::tm* changing_tm, const int quantity = 1);
99 std::tm* tm_increment_hour(std::tm* changing_tm, const int quantity = 1);
100 std::tm* tm_increment_minute(std::tm* changing_tm, const int quantity = 1);
101 std::tm* tm_increment_second(std::tm* changing_tm, const int quantity = 1);
102 std::tm* tm_decrement_day(std::tm* changing_tm, const int quantity = 1);
103 std::tm* tm_decrement_hour(std::tm* changing_tm, const int quantity = 1);
104 std::tm* tm_decrement_minute(std::tm* changing_tm, const int quantity = 1);
105 std::tm* tm_decrement_second(std::tm* changing_tm, const int quantity = 1);
106 bool check_utc_timestamp(const time_t check_time, int& secs_diff,
107  const int year, const int month,
108  const int day, const int hour,
109  const int minute, const int second);
110 time_t get_utc_timestamp(const int year, const int month,
111  const int day, const int hour,
112  const int minute, const int second);
113 int get_utc_timestamp_sec_diff(const time_t check_time,
114  const int year, const int month,
115  const int day, const int hour,
116  const int minute, const int second);
117 
118 
124 class UTCTime {
125  public:
126  explicit UTCTime();
127  explicit UTCTime(const std::tm& utc_tm);
128  explicit UTCTime(const int year, const int month,
129  const int day, const int hour,
130  const int minute, const int second);
131 
132  std::tm get_tm() const;
133  std::string time_string() const;
134  std::string time_string_inet() const;
135  time_t timestamp() const;
136 
137  bool operator<(const UTCTime& rhs) const;
138  bool operator>=(const UTCTime& rhs) const;
139  bool operator>(const UTCTime& rhs) const;
140  bool operator<=(const UTCTime& rhs) const;
141  bool operator==(const UTCTime& rhs) const;
142  bool operator!=(const UTCTime& rhs) const;
143 
144  double operator-(const UTCTime& rhs) const;
145 
146  private:
147  int m_year;
148  int m_month;
149  int m_day;
150  int m_hour;
151  int m_minute;
152  int m_second;
153  time_t m_timestamp;
154 };
155 
156 } // namespace utctime
157 
158 #endif // PG_UTC_TIME_H