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_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; }
114 
124  int compare(datetime const& op) const;
125 
127  bool earlier_than(datetime const& op) const { return compare(op) < 0; };
128 
130  bool later_than(datetime const& op) const { return compare(op) > 0; };
131 
137  datetime& operator+=(duration const& op);
138  datetime operator+(duration const& op) const { datetime t(*this); t += op; return t; }
139 
140  datetime& operator-=(duration const& op);
141  datetime operator-(duration const& op) const { datetime t(*this); t -= op; return t; }
143 
144  friend duration FZ_PUBLIC_SYMBOL operator-(datetime const& a, datetime const& b);
145 
153  bool set(zone z, int year, int month, int day, int hour = -1, int minute = -1, int second = -1, int millisecond = -1);
154 
164  bool set(std::string_view const& str, zone z);
165  bool set(std::wstring_view const& str, zone z);
166 
167 #ifdef FZ_WINDOWS
168  bool set(FILETIME const& ft, accuracy a);
171  bool set(SYSTEMTIME const& ft, accuracy a, zone z);
172 #endif
173 
174 #if defined(FZ_UNIX) || defined(FZ_MAC)
175 
180  bool set(tm & t, accuracy a, zone z);
181 #endif
182 
189  bool imbue_time(int hour, int minute, int second = -1, int millisecond = -1);
190 
197  std::string format(std::string const& format, zone z) const;
198  std::wstring format(std::wstring const& format, zone z) const;
199 
205  static bool verify_format(std::string const& fmt);
206  static bool verify_format(std::wstring const& fmt);
207 
209  int get_milliseconds() const { return t_ % 1000; }
210 
212  time_t get_time_t() const;
213 
218  tm get_tm(zone z) const;
219 
220 #ifdef FZ_WINDOWS
221  FILETIME get_filetime() const;
223 #endif
224 
231  std::string get_rfc822() const;
232 
248  bool set_rfc822(std::string_view const& str);
249  bool set_rfc822(std::wstring_view const& str);
250 
251 private:
252  int FZ_PRIVATE_SYMBOL compare_slow(datetime const& op) const;
253 
254  bool FZ_PRIVATE_SYMBOL clamped();
255 
256  enum invalid_t : int64_t {
257  invalid = std::numeric_limits<int64_t>::min()
258  };
259 
260  int64_t t_{invalid};
261  accuracy a_{days};
262 };
263 
271 class FZ_PUBLIC_SYMBOL duration final
272 {
273 public:
274  duration() = default;
275 
280  int64_t get_days() const { return ms_ / 1000 / 3600 / 24; }
281  int64_t get_hours() const { return ms_ / 1000 / 3600; }
282  int64_t get_minutes() const { return ms_ / 1000 / 60; }
283  int64_t get_seconds() const { return ms_ / 1000; }
284  int64_t get_milliseconds() const { return ms_; }
286 
287  static duration from_days(int64_t m) {
288  return duration(m * 1000 * 60 * 60 * 24);
289  }
290  static duration from_hours(int64_t m) {
291  return duration(m * 1000 * 60 * 60);
292  }
293  static duration from_minutes(int64_t m) {
294  return duration(m * 1000 * 60);
295  }
296  static duration from_seconds(int64_t m) {
297  return duration(m * 1000);
298  }
299  static duration from_milliseconds(int64_t m) {
300  return duration(m);
301  }
303 
304  duration& operator+=(duration const& op) {
305  ms_ += op.ms_;
306  return *this;
307  }
308 
309  duration& operator-=(duration const& op) {
310  ms_ -= op.ms_;
311  return *this;
312  }
313 
314  duration operator-() const {
315  return duration(-ms_);
316  }
317 
318  explicit operator bool() const {
319  return ms_ != 0;
320  }
321 
322  duration& operator*=(int64_t op) {
323  ms_ *= op;
324  return *this;
325  }
326 
327  bool operator<(duration const& op) const { return ms_ < op.ms_; }
328  bool operator<=(duration const& op) const { return ms_ <= op.ms_; }
329  bool operator>(duration const& op) const { return ms_ > op.ms_; }
330  bool operator>=(duration const& op) const { return ms_ >= op.ms_; }
331 
332  friend duration FZ_PUBLIC_SYMBOL operator-(duration const& a, duration const& b);
333  friend duration FZ_PUBLIC_SYMBOL operator+(duration const& a, duration const& b);
334 private:
335  explicit FZ_PRIVATE_SYMBOL duration(int64_t ms) : ms_(ms) {}
336 
337  int64_t ms_{};
338 };
339 
340 inline duration operator-(duration const& a, duration const& b)
341 {
342  return duration(a) -= b;
343 }
344 
345 inline duration operator+(duration const& a, duration const& b)
346 {
347  return duration(a) += b;
348 }
349 
356 duration FZ_PUBLIC_SYMBOL operator-(datetime const& a, datetime const& b);
357 
358 
359 
360 
368 class FZ_PUBLIC_SYMBOL monotonic_clock final
369 {
370 public:
375  monotonic_clock() = default;
376 
377  monotonic_clock(monotonic_clock const&) = default;
378  monotonic_clock(monotonic_clock &&) noexcept = default;
379  monotonic_clock& operator=(monotonic_clock const&) = default;
380  monotonic_clock& operator=(monotonic_clock &&) noexcept = default;
381 
382  monotonic_clock const operator+(duration const& d) const
383  {
384  return monotonic_clock(*this) += d;
385  }
386 
387 private:
388  typedef std::chrono::steady_clock clock_type;
389  static_assert(std::chrono::steady_clock::is_steady, "Nonconforming stdlib, your steady_clock isn't steady");
390 
391 public:
393  static monotonic_clock now() {
394  return monotonic_clock(clock_type::now());
395  }
396 
397  explicit operator bool() const {
398  return t_ != clock_type::time_point();
399  }
400 
401  monotonic_clock& operator+=(duration const& d)
402  {
403  t_ += std::chrono::milliseconds(d.get_milliseconds());
404  return *this;
405  }
406 
407  monotonic_clock& operator-=(duration const& d)
408  {
409  t_ -= std::chrono::milliseconds(d.get_milliseconds());
410  return *this;
411  }
412 
413 private:
414  explicit FZ_PRIVATE_SYMBOL monotonic_clock(clock_type::time_point const& t)
415  : t_(t)
416  {}
417 
418  clock_type::time_point t_;
419 
420  friend duration operator-(monotonic_clock const& a, monotonic_clock const& b);
421  friend bool operator==(monotonic_clock const& a, monotonic_clock const& b);
422  friend bool operator<(monotonic_clock const& a, monotonic_clock const& b);
423  friend bool operator<=(monotonic_clock const& a, monotonic_clock const& b);
424  friend bool operator>(monotonic_clock const& a, monotonic_clock const& b);
425  friend bool operator>=(monotonic_clock const& a, monotonic_clock const& b);
426 };
427 
432 inline duration operator-(monotonic_clock const& a, monotonic_clock const& b)
433 {
434  return duration::from_milliseconds(std::chrono::duration_cast<std::chrono::milliseconds>(a.t_ - b.t_).count());
435 }
436 
438 inline bool operator==(monotonic_clock const& a, monotonic_clock const& b)
439 {
440  return a.t_ == b.t_;
441 }
442 
444 inline bool operator<(monotonic_clock const& a, monotonic_clock const& b)
445 {
446  return a.t_ < b.t_;
447 }
448 
450 inline bool operator<=(monotonic_clock const& a, monotonic_clock const& b)
451 {
452  return a.t_ <= b.t_;
453 }
454 
456 inline bool operator>(monotonic_clock const& a, monotonic_clock const& b)
457 {
458  return a.t_ > b.t_;
459 }
460 
462 inline bool operator>=(monotonic_clock const& a, monotonic_clock const& b)
463 {
464  return a.t_ >= b.t_;
465 }
466 
467 }
468 
469 #endif
fz::operator==
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.
fz::monotonic_clock
A monotonic clock (aka steady clock) is independent from walltime.
Definition: time.hpp:369
fz::datetime::verify_format
static bool verify_format(std::string const &fmt)
fz::datetime::zone
zone
When importing or exporting a timestamp, zone is used to explicitly specify whether the conversion is...
Definition: time.hpp:58
fz::datetime::get_time_t
time_t get_time_t() const
Get timestamp as time_t, seconds since 1970-01-01 00:00:00.
fz::datetime::get_milliseconds
int get_milliseconds() const
Get millisecond part of timestamp.
Definition: time.hpp:209
fz::duration
The duration class represents a time interval in milliseconds.
Definition: time.hpp:272
fz::datetime::operator-
friend duration operator-(datetime const &a, datetime const &b)
Gets the difference between two timestamps as duration.
fz::datetime::format
std::string format(std::string const &format, zone z) const
fz::datetime::get_tm
tm get_tm(zone z) const
Get timestamp as struct tm.
fz::datetime::set
bool set(tm &t, accuracy a, zone z)
fz::datetime::imbue_time
bool imbue_time(int hour, int minute, int second=-1, int millisecond=-1)
Adds time to timestamps that only have a day-accuracy.
fz::datetime::set
bool set(std::string_view const &str, zone z)
Set from string, looks for YYYYmmDD[[[[HH]MM]SS]sss].
fz::datetime::datetime
datetime()=default
A default-constructed timestamp is empty()
fz::datetime::earlier_than
bool earlier_than(datetime const &op) const
Equivalent to compare(op) < 0.
Definition: time.hpp:127
fz::datetime::set
bool set(SYSTEMTIME const &ft, accuracy a, zone z)
Windows-only: Set timestamp from SYSTEMTIME.
fz::datetime::set_rfc822
bool set_rfc822(std::string_view const &str)
fz::datetime::now
static datetime now()
Returns the current date/time.
fz::datetime::later_than
bool later_than(datetime const &op) const
Equivalent to compare(op) > 0.
Definition: time.hpp:130
fz::monotonic_clock::now
static monotonic_clock now()
Gets the current point in time time.
Definition: time.hpp:393
fz::monotonic_clock::monotonic_clock
monotonic_clock()=default
Constructs empty clock.
fz::datetime::get_rfc822
std::string get_rfc822() const
fz::datetime::clear
void clear()
Resulting timestamp is empty()
fz::datetime::empty
bool empty() const
fz::datetime::datetime
datetime(std::string_view const &s, zone z)
Construct from string, looks for YYYYmmDD[[[[HH]MM]SS]sss].
libfilezilla.hpp
Sets some global macros and further includes string.hpp.
fz::datetime::compare
int compare(datetime const &op) const
Accuracy-aware comparison against another timestamp.
fz::datetime::accuracy
accuracy
The datetime's accuracy.
Definition: time.hpp:46
fz::datetime
Represents a point of time in wallclock, tracking the timestamps accuracy/precision.
Definition: time.hpp:41
fz
The namespace used by libfilezilla.
Definition: apply.hpp:17
fz::datetime::set
bool set(zone z, int year, int month, int day, int hour=-1, int minute=-1, int second=-1, int millisecond=-1)
Sets the timestamp.