libfilezilla
time.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_TIME_HEADER
2 #define LIBFILEZILLA_TIME_HEADER
3 
4 #include "libfilezilla.hpp"
5 
6 #include <chrono>
7 #include <ctime>
8 
9 #include <limits>
10 
11 #ifdef FZ_WINDOWS
12 #include "private/windows.hpp"
13 #endif
14 
19 namespace fz {
20 
21 class FZ_PUBLIC_SYMBOL duration;
22 
40 class FZ_PUBLIC_SYMBOL datetime final
41 {
42 public:
46  enum accuracy : char {
47  days,
48  hours,
49  minutes,
50  seconds,
51  milliseconds
52  };
53 
58  enum zone {
59  utc,
60  local
61  };
62 
64  datetime() = default;
65 
66  datetime(zone z, int year, int month, int day, int hour = -1, int minute = -1, int second = -1, int millisecond = -1);
67 
68  explicit datetime(time_t, accuracy a);
69 
74  explicit datetime(std::string const& s, zone z);
75  explicit datetime(std::wstring const& s, zone z);
76 
77 #ifdef FZ_WINDOWS
78  explicit datetime(FILETIME const& ft, accuracy a);
80 #endif
81 
82  datetime(datetime const& op) = default;
83  datetime(datetime && op) noexcept = default;
84  datetime& operator=(datetime const& op) = default;
85  datetime& operator=(datetime && op) noexcept = default;
86 
88  bool empty() const;
89 
91  void clear();
92 
93  accuracy get_accuracy() const { return a_; }
94 
96  static datetime now();
97 
104  bool operator==(datetime const& op) const;
105  bool operator!=(datetime const& op) const { return !(*this == op); }
106  bool operator<(datetime const& op) const;
107  bool operator<=(datetime const& op) const;
108  bool operator>(datetime const& op) const { return op < *this; }
110 
120  int compare(datetime const& op) const;
121 
123  bool earlier_than(datetime const& op) const { return compare(op) < 0; };
124 
126  bool later_than(datetime const& op) const { return compare(op) > 0; };
127 
133  datetime& operator+=(duration const& op);
134  datetime operator+(duration const& op) const { datetime t(*this); t += op; return t; }
135 
136  datetime& operator-=(duration const& op);
137  datetime operator-(duration const& op) const { datetime t(*this); t -= op; return t; }
139 
140  friend duration FZ_PUBLIC_SYMBOL operator-(datetime const& a, datetime const& b);
141 
149  bool set(zone z, int year, int month, int day, int hour = -1, int minute = -1, int second = -1, int millisecond = -1);
150 
160  bool set(std::string const& str, zone z);
161  bool set(std::wstring const& str, zone z);
162 
163 #ifdef FZ_WINDOWS
164  bool set(FILETIME const& ft, accuracy a);
167  bool set(SYSTEMTIME const& ft, accuracy a, zone z);
168 #endif
169 
170 #if defined(FZ_UNIX) || defined(FZ_MAC)
171 
176  bool set(tm & t, accuracy a, zone z);
177 #endif
178 
185  bool imbue_time(int hour, int minute, int second = -1, int millisecond = -1);
186 
191  std::string format(std::string const& format, zone z) const;
192  std::wstring format(std::wstring const& format, zone z) const;
193 
199  static bool verify_format(std::string const& fmt);
200  static bool verify_format(std::wstring const& fmt);
201 
203  int get_milliseconds() const { return t_ % 1000; }
204 
206  time_t get_time_t() const;
207 
209  tm get_tm(zone z) const;
210 
211 #ifdef FZ_WINDOWS
212  FILETIME get_filetime() const;
214 #endif
215 
216 private:
217  int FZ_PRIVATE_SYMBOL compare_slow(datetime const& op) const;
218 
219  bool FZ_PRIVATE_SYMBOL clamped();
220 
221  enum invalid_t : int64_t {
222  invalid = std::numeric_limits<int64_t>::min()
223  };
224 
225  int64_t t_{invalid};
226  accuracy a_{days};
227 };
228 
236 class FZ_PUBLIC_SYMBOL duration final
237 {
238 public:
239  duration() = default;
240 
245  int64_t get_days() const { return ms_ / 1000 / 3600 / 24; }
246  int64_t get_hours() const { return ms_ / 1000 / 3600; }
247  int64_t get_minutes() const { return ms_ / 1000 / 60; }
248  int64_t get_seconds() const { return ms_ / 1000; }
249  int64_t get_milliseconds() const { return ms_; }
251 
252  static duration from_days(int64_t m) {
253  return duration(m * 1000 * 60 * 60 * 24);
254  }
255  static duration from_hours(int64_t m) {
256  return duration(m * 1000 * 60 * 60);
257  }
258  static duration from_minutes(int64_t m) {
259  return duration(m * 1000 * 60);
260  }
261  static duration from_seconds(int64_t m) {
262  return duration(m * 1000);
263  }
264  static duration from_milliseconds(int64_t m) {
265  return duration(m);
266  }
268 
269  duration& operator-=(duration const& op) {
270  ms_ -= op.ms_;
271  return *this;
272  }
273 
274  duration operator-() const {
275  return duration(-ms_);
276  }
277 
278  explicit operator bool() const {
279  return ms_ != 0;
280  }
281 
282  bool operator<(duration const& op) const { return ms_ < op.ms_; }
283  bool operator<=(duration const& op) const { return ms_ <= op.ms_; }
284  bool operator>(duration const& op) const { return ms_ > op.ms_; }
285  bool operator>=(duration const& op) const { return ms_ >= op.ms_; }
286 
287  friend duration FZ_PUBLIC_SYMBOL operator-(duration const& a, duration const& b);
288 private:
289  explicit duration(int64_t ms) : ms_(ms) {}
290 
291  int64_t ms_{};
292 };
293 
294 inline duration operator-(duration const& a, duration const& b)
295 {
296  return duration(a) -= b;
297 }
298 
299 
306 duration FZ_PUBLIC_SYMBOL operator-(datetime const& a, datetime const& b);
307 
308 
309 
310 
318 class FZ_PUBLIC_SYMBOL monotonic_clock final
319 {
320 public:
325  monotonic_clock() = default;
326 
327  monotonic_clock(monotonic_clock const&) = default;
328  monotonic_clock(monotonic_clock &&) noexcept = default;
329  monotonic_clock& operator=(monotonic_clock const&) = default;
330  monotonic_clock& operator=(monotonic_clock &&) noexcept = default;
331 
332  monotonic_clock const operator+(duration const& d) const
333  {
334  return monotonic_clock(*this) += d;
335  }
336 
337 private:
338  typedef std::chrono::steady_clock clock_type;
339  static_assert(std::chrono::steady_clock::is_steady, "Nonconforming stdlib, your steady_clock isn't steady");
340 
341 public:
343  static monotonic_clock now() {
344  return monotonic_clock(clock_type::now());
345  }
346 
347  explicit operator bool() const {
348  return t_ != clock_type::time_point();
349  }
350 
351  monotonic_clock& operator+=(duration const& d)
352  {
353  t_ += std::chrono::milliseconds(d.get_milliseconds());
354  return *this;
355  }
356 
357  monotonic_clock& operator-=(duration const& d)
358  {
359  t_ -= std::chrono::milliseconds(d.get_milliseconds());
360  return *this;
361  }
362 
363 private:
364  explicit monotonic_clock(clock_type::time_point const& t)
365  : t_(t)
366  {}
367 
368  clock_type::time_point t_;
369 
370  friend duration operator-(monotonic_clock const& a, monotonic_clock const& b);
371  friend bool operator==(monotonic_clock const& a, monotonic_clock const& b);
372  friend bool operator<(monotonic_clock const& a, monotonic_clock const& b);
373  friend bool operator<=(monotonic_clock const& a, monotonic_clock const& b);
374  friend bool operator>(monotonic_clock const& a, monotonic_clock const& b);
375  friend bool operator>=(monotonic_clock const& a, monotonic_clock const& b);
376 };
377 
382 inline duration operator-(monotonic_clock const& a, monotonic_clock const& b)
383 {
384  return duration::from_milliseconds(std::chrono::duration_cast<std::chrono::milliseconds>(a.t_ - b.t_).count());
385 }
386 
388 inline bool operator==(monotonic_clock const& a, monotonic_clock const& b)
389 {
390  return a.t_ == b.t_;
391 }
392 
394 inline bool operator<(monotonic_clock const& a, monotonic_clock const& b)
395 {
396  return a.t_ < b.t_;
397 }
398 
400 inline bool operator<=(monotonic_clock const& a, monotonic_clock const& b)
401 {
402  return a.t_ <= b.t_;
403 }
404 
406 inline bool operator>(monotonic_clock const& a, monotonic_clock const& b)
407 {
408  return a.t_ > b.t_;
409 }
410 
412 inline bool operator>=(monotonic_clock const& a, monotonic_clock const& b)
413 {
414  return a.t_ >= b.t_;
415 }
416 
417 }
418 
419 #endif
bool operator==(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:388
bool earlier_than(datetime const &op) const
Equivalent to compare(op) < 0.
Definition: time.hpp:123
bool operator<(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:394
int get_milliseconds() const
Get millisecond part of timestamp.
Definition: time.hpp:203
Represents a point of time in wallclock, tracking the timestamps accuracy/precision.
Definition: time.hpp:40
accuracy
The datetime&#39;s accuracy.
Definition: time.hpp:46
bool later_than(datetime const &op) const
Equivalent to compare(op) > 0.
Definition: time.hpp:126
A monotonic clock (aka steady clock) is independent from walltime.
Definition: time.hpp:318
bool operator>(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:406
The namespace used by libfilezilla.
Definition: apply.hpp:16
zone
When importing or exporting a timestamp, zone is used to explicitly specify whether the conversion is...
Definition: time.hpp:58
The duration class represents a time interval in milliseconds.
Definition: time.hpp:236
Sets some global macros and further includes string.hpp.
bool operator<=(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:400
bool operator>=(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:412
static monotonic_clock now()
Gets the current point in time time.
Definition: time.hpp:343