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 "glue/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() noexcept = 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_view const& s, zone z);
75  explicit datetime(std::wstring_view 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 
90  explicit operator bool() const {
91  return !empty();
92  }
93 
95  void clear();
96 
97  accuracy get_accuracy() const { return a_; }
98 
100  static datetime now();
101 
108  bool operator==(datetime const& op) const;
109  bool operator!=(datetime const& op) const { return !(*this == op); }
110  bool operator<(datetime const& op) const;
111  bool operator<=(datetime const& op) const;
112  bool operator>(datetime const& op) const { return op < *this; }
113  bool operator>=(datetime const& op) const { return op <= *this; }
115 
125  int compare(datetime const& op) const;
126 
128  bool earlier_than(datetime const& op) const { return compare(op) < 0; };
129 
131  bool later_than(datetime const& op) const { return compare(op) > 0; };
132 
138  datetime& operator+=(duration const& op);
139  datetime operator+(duration const& op) const { datetime t(*this); t += op; return t; }
140 
141  datetime& operator-=(duration const& op);
142  datetime operator-(duration const& op) const { datetime t(*this); t -= op; return t; }
144 
145  friend duration FZ_PUBLIC_SYMBOL operator-(datetime const& a, datetime const& b);
146 
154  bool set(zone z, int year, int month, int day, int hour = -1, int minute = -1, int second = -1, int millisecond = -1);
155 
165  bool set(std::string_view const& str, zone z);
166  bool set(std::wstring_view const& str, zone z);
167 
168 #ifdef FZ_WINDOWS
169  bool set(FILETIME const& ft, accuracy a);
172  bool set(SYSTEMTIME const& ft, accuracy a, zone z);
173 #endif
174 
175 #if defined(FZ_UNIX) || defined(FZ_MAC)
176 
181  bool set(tm & t, accuracy a, zone z);
182 #endif
183 
190  bool imbue_time(int hour, int minute, int second = -1, int millisecond = -1);
191 
198  std::string format(std::string const& format, zone z) const;
199  std::wstring format(std::wstring const& format, zone z) const;
200 
206  static bool verify_format(std::string const& fmt);
207  static bool verify_format(std::wstring const& fmt);
208 
210  int get_milliseconds() const { return t_ % 1000; }
211 
213  time_t get_time_t() const;
214 
219  tm get_tm(zone z) const;
220 
221 #ifdef FZ_WINDOWS
222  FILETIME get_filetime() const;
224 #endif
225 
232  std::string get_rfc822() const;
233 
249  bool set_rfc822(std::string_view const& str);
250  bool set_rfc822(std::wstring_view const& str);
251 
263  bool set_rfc3339(std::string_view const& str);
264  bool set_rfc3339(std::wstring_view const& str);
265 
266 private:
267  int FZ_PRIVATE_SYMBOL compare_slow(datetime const& op) const;
268 
269  bool FZ_PRIVATE_SYMBOL clamped();
270 
271  enum invalid_t : int64_t {
272  invalid = std::numeric_limits<int64_t>::min()
273  };
274 
275  int64_t t_{invalid};
276  accuracy a_{days};
277 };
278 
288 class FZ_PUBLIC_SYMBOL duration final
289 {
290 public:
291  duration() noexcept = default;
292 
297  int64_t get_days() const { return ms_ / 1000 / 3600 / 24; }
298  int64_t get_hours() const { return ms_ / 1000 / 3600; }
299  int64_t get_minutes() const { return ms_ / 1000 / 60; }
300  int64_t get_seconds() const { return ms_ / 1000; }
301  int64_t get_milliseconds() const { return ms_; }
303 
304  static duration from_days(int64_t m) {
305  return duration(m * 1000 * 60 * 60 * 24);
306  }
307  static duration from_hours(int64_t m) {
308  return duration(m * 1000 * 60 * 60);
309  }
310  static duration from_minutes(int64_t m) {
311  return duration(m * 1000 * 60);
312  }
313  static duration from_seconds(int64_t m) {
314  return duration(m * 1000);
315  }
316  static duration from_milliseconds(int64_t m) {
317  return duration(m);
318  }
320 
321  duration& operator+=(duration const& op) {
322  ms_ += op.ms_;
323  return *this;
324  }
325 
326  duration& operator-=(duration const& op) {
327  ms_ -= op.ms_;
328  return *this;
329  }
330 
331  duration operator-() const {
332  return duration(-ms_);
333  }
334 
335  explicit operator bool() const {
336  return ms_ != 0;
337  }
338 
339  duration& operator*=(int64_t op) {
340  ms_ *= op;
341  return *this;
342  }
343 
344  duration absolute() const {
345  return (ms_ < 0) ? duration(-ms_) : *this;
346  }
347 
348  bool operator<(duration const& op) const { return ms_ < op.ms_; }
349  bool operator<=(duration const& op) const { return ms_ <= op.ms_; }
350  bool operator>(duration const& op) const { return ms_ > op.ms_; }
351  bool operator>=(duration const& op) const { return ms_ >= op.ms_; }
352 
353  friend duration FZ_PUBLIC_SYMBOL operator-(duration const& a, duration const& b);
354  friend duration FZ_PUBLIC_SYMBOL operator+(duration const& a, duration const& b);
355 private:
356  explicit FZ_PRIVATE_SYMBOL duration(int64_t ms) : ms_(ms) {}
357 
358  int64_t ms_{};
359 };
360 
361 inline duration operator-(duration const& a, duration const& b)
362 {
363  return duration(a) -= b;
364 }
365 
366 inline duration operator+(duration const& a, duration const& b)
367 {
368  return duration(a) += b;
369 }
370 
377 duration FZ_PUBLIC_SYMBOL operator-(datetime const& a, datetime const& b);
378 
379 
380 
381 
389 class FZ_PUBLIC_SYMBOL monotonic_clock final
390 {
391 public:
396  monotonic_clock() = default;
397 
398  monotonic_clock(monotonic_clock const&) = default;
399  monotonic_clock(monotonic_clock &&) noexcept = default;
400  monotonic_clock& operator=(monotonic_clock const&) = default;
401  monotonic_clock& operator=(monotonic_clock &&) noexcept = default;
402 
403  monotonic_clock const operator+(duration const& d) const
404  {
405  return monotonic_clock(*this) += d;
406  }
407 
408 private:
409  typedef std::chrono::steady_clock clock_type;
410  static_assert(std::chrono::steady_clock::is_steady, "Nonconforming stdlib, your steady_clock isn't steady");
411 
412 public:
414  static monotonic_clock now() {
415  return monotonic_clock(clock_type::now());
416  }
417 
418  explicit operator bool() const {
419  return t_ != clock_type::time_point();
420  }
421 
422  monotonic_clock& operator+=(duration const& d)
423  {
424  t_ += std::chrono::milliseconds(d.get_milliseconds());
425  return *this;
426  }
427 
428  monotonic_clock& operator-=(duration const& d)
429  {
430  t_ -= std::chrono::milliseconds(d.get_milliseconds());
431  return *this;
432  }
433 
434 private:
435  explicit FZ_PRIVATE_SYMBOL monotonic_clock(clock_type::time_point const& t)
436  : t_(t)
437  {}
438 
439  clock_type::time_point t_;
440 
441  friend duration operator-(monotonic_clock const& a, monotonic_clock const& b);
442  friend bool operator==(monotonic_clock const& a, monotonic_clock const& b);
443  friend bool operator<(monotonic_clock const& a, monotonic_clock const& b);
444  friend bool operator<=(monotonic_clock const& a, monotonic_clock const& b);
445  friend bool operator>(monotonic_clock const& a, monotonic_clock const& b);
446  friend bool operator>=(monotonic_clock const& a, monotonic_clock const& b);
447 };
448 
453 inline duration operator-(monotonic_clock const& a, monotonic_clock const& b)
454 {
455  return duration::from_milliseconds(std::chrono::duration_cast<std::chrono::milliseconds>(a.t_ - b.t_).count());
456 }
457 
459 inline bool operator==(monotonic_clock const& a, monotonic_clock const& b)
460 {
461  return a.t_ == b.t_;
462 }
463 
465 inline bool operator<(monotonic_clock const& a, monotonic_clock const& b)
466 {
467  return a.t_ < b.t_;
468 }
469 
471 inline bool operator<=(monotonic_clock const& a, monotonic_clock const& b)
472 {
473  return a.t_ <= b.t_;
474 }
475 
477 inline bool operator>(monotonic_clock const& a, monotonic_clock const& b)
478 {
479  return a.t_ > b.t_;
480 }
481 
483 inline bool operator>=(monotonic_clock const& a, monotonic_clock const& b)
484 {
485  return a.t_ >= b.t_;
486 }
487 
488 }
489 
490 #endif
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.
int get_milliseconds() const
Get millisecond part of timestamp.
Definition: time.hpp:210
bool operator==(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:459
Definition: impersonation.hpp:78
bool operator<(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:465
Represents a point of time in wallclock, tracking the timestamps accuracy/precision.
Definition: time.hpp:40
accuracy
The datetime's accuracy.
Definition: time.hpp:46
A monotonic clock (aka steady clock) is independent from walltime.
Definition: time.hpp:389
bool operator>(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:477
The namespace used by libfilezilla.
Definition: apply.hpp:17
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:288
Sets some global macros and further includes string.hpp.
bool operator<=(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:471
bool operator>=(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:483
static monotonic_clock now()
Gets the current point in time time.
Definition: time.hpp:414
bool earlier_than(datetime const &op) const
Equivalent to compare(op) < 0.
Definition: time.hpp:128
bool later_than(datetime const &op) const
Equivalent to compare(op) > 0.
Definition: time.hpp:131