libfilezilla
Loading...
Searching...
No Matches
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
18
19namespace fz {
20
21class FZ_PUBLIC_SYMBOL duration;
22
40class FZ_PUBLIC_SYMBOL datetime final
41{
42public:
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
79 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
139 datetime& operator+=(duration const& op);
140 datetime operator+(duration const& op) const { datetime t(*this); t += op; return t; }
141
142 datetime& operator-=(duration const& op);
143 datetime operator-(duration const& op) const { datetime t(*this); t -= op; return t; }
145
146 friend duration FZ_PUBLIC_SYMBOL operator-(datetime const& a, datetime const& b);
147
155 bool set(zone z, int year, int month, int day, int hour = -1, int minute = -1, int second = -1, int millisecond = -1);
156
166 bool set(std::string_view const& str, zone z);
167 bool set(std::wstring_view const& str, zone z);
168
169#ifdef FZ_WINDOWS
171 bool set(FILETIME const& ft, accuracy a);
173 bool set(SYSTEMTIME const& ft, accuracy a, zone z);
174#endif
175
176#if defined(FZ_UNIX) || defined(FZ_MAC)
182 bool set(tm & t, accuracy a, zone z);
183#endif
184
191 bool imbue_time(int hour, int minute, int second = -1, int millisecond = -1);
192
199 std::string format(std::string const& format, zone z) const;
200 std::wstring format(std::wstring const& format, zone z) const;
201
207 static bool verify_format(std::string const& fmt);
208 static bool verify_format(std::wstring const& fmt);
209
211 int get_milliseconds() const {
212 int ms = static_cast<int>(t_ % 1000);
213 if (ms < 0) {
214 ms += 1000;
215 }
216 return ms;
217 }
218
220 time_t get_time_t() const;
221
228 tm get_tm(zone z) const;
229
230#ifdef FZ_WINDOWS
232 FILETIME get_filetime() const;
233#endif
234
241 std::string get_rfc822() const;
242
258 bool set_rfc822(std::string_view const& str);
259 bool set_rfc822(std::wstring_view const& str);
260
272 bool set_rfc3339(std::string_view const& str);
273 bool set_rfc3339(std::wstring_view const& str);
274
275private:
276 int FZ_PRIVATE_SYMBOL compare_slow(datetime const& op) const;
277
278 bool FZ_PRIVATE_SYMBOL clamped();
279
280 enum invalid_t : int64_t {
281 invalid = std::numeric_limits<int64_t>::min()
282 };
283
284 int64_t t_{invalid};
285 accuracy a_{days};
286};
287
297class FZ_PUBLIC_SYMBOL duration final
298{
299public:
300 duration() noexcept = default;
301
306 int64_t get_days() const { return ms_ / 1000 / 3600 / 24; }
307 int64_t get_hours() const { return ms_ / 1000 / 3600; }
308 int64_t get_minutes() const { return ms_ / 1000 / 60; }
309 int64_t get_seconds() const { return ms_ / 1000; }
310 int64_t get_milliseconds() const { return ms_; }
312
316 static duration from_days(int64_t m) {
317 return duration(m * 1000 * 60 * 60 * 24);
318 }
319 static duration from_hours(int64_t m) {
320 return duration(m * 1000 * 60 * 60);
321 }
322 static duration from_minutes(int64_t m) {
323 return duration(m * 1000 * 60);
324 }
325 static duration from_seconds(int64_t m) {
326 return duration(m * 1000);
327 }
328 static duration from_milliseconds(int64_t m) {
329 return duration(m);
330 }
332
334 duration& operator+=(duration const& op) {
335 ms_ += op.ms_;
336 return *this;
337 }
338
339 duration& operator-=(duration const& op) {
340 ms_ -= op.ms_;
341 return *this;
342 }
343
344 duration& operator/=(int64_t op) {
345 ms_ /= op;
346 return *this;
347 }
348
349 duration operator-() const {
350 return duration(-ms_);
351 }
352
353 explicit operator bool() const {
354 return ms_ != 0;
355 }
356
357 duration& operator*=(int64_t op) {
358 ms_ *= op;
359 return *this;
360 }
362
363 duration absolute() const {
364 return (ms_ < 0) ? duration(-ms_) : *this;
365 }
366
367 bool operator<(duration const& op) const { return ms_ < op.ms_; }
368 bool operator<=(duration const& op) const { return ms_ <= op.ms_; }
369 bool operator>(duration const& op) const { return ms_ > op.ms_; }
370 bool operator>=(duration const& op) const { return ms_ >= op.ms_; }
371
372 friend duration FZ_PUBLIC_SYMBOL operator-(duration const& a, duration const& b);
373 friend duration FZ_PUBLIC_SYMBOL operator+(duration const& a, duration const& b);
374 friend duration FZ_PUBLIC_SYMBOL operator/(duration const& a, int64_t b);
375private:
376 explicit FZ_PRIVATE_SYMBOL duration(int64_t ms) : ms_(ms) {}
377
378 int64_t ms_{};
379};
380
381inline duration operator-(duration const& a, duration const& b)
382{
383 return duration(a) -= b;
384}
385
386inline duration operator+(duration const& a, duration const& b)
387{
388 return duration(a) += b;
389}
390
391inline duration operator/(duration const& a, int64_t b)
392{
393 return duration(a) /= b;
394}
395
401duration FZ_PUBLIC_SYMBOL operator-(datetime const& a, datetime const& b);
402
403
404
405
413class FZ_PUBLIC_SYMBOL monotonic_clock final
414{
415public:
420 monotonic_clock() = default;
421
422 monotonic_clock(monotonic_clock const&) = default;
423 monotonic_clock(monotonic_clock &&) noexcept = default;
424 monotonic_clock& operator=(monotonic_clock const&) = default;
425 monotonic_clock& operator=(monotonic_clock &&) noexcept = default;
426
427 monotonic_clock const operator+(duration const& d) const
428 {
429 return monotonic_clock(*this) += d;
430 }
431 monotonic_clock const operator-(duration const& d) const
432 {
433 return monotonic_clock(*this) -= d;
434 }
435
436private:
437 typedef std::chrono::steady_clock clock_type;
438 static_assert(std::chrono::steady_clock::is_steady, "Nonconforming stdlib, your steady_clock isn't steady");
439
440public:
443 return monotonic_clock(std::chrono::time_point_cast<std::chrono::milliseconds>(clock_type::now()));
444 }
445
446 explicit operator bool() const {
447 return t_ != clock_type::time_point();
448 }
449
450 monotonic_clock& operator+=(duration const& d)
451 {
452 t_ += std::chrono::milliseconds(d.get_milliseconds());
453 return *this;
454 }
455
456 monotonic_clock& operator-=(duration const& d)
457 {
458 t_ -= std::chrono::milliseconds(d.get_milliseconds());
459 return *this;
460 }
461
462private:
463 explicit FZ_PRIVATE_SYMBOL monotonic_clock(clock_type::time_point const& t)
464 : t_(t)
465 {}
466
467 clock_type::time_point t_;
468
469 friend duration operator-(monotonic_clock const& a, monotonic_clock const& b);
470 friend bool operator==(monotonic_clock const& a, monotonic_clock const& b);
471 friend bool operator<(monotonic_clock const& a, monotonic_clock const& b);
472 friend bool operator<=(monotonic_clock const& a, monotonic_clock const& b);
473 friend bool operator>(monotonic_clock const& a, monotonic_clock const& b);
474 friend bool operator>=(monotonic_clock const& a, monotonic_clock const& b);
475};
476
481{
482 return duration::from_milliseconds(std::chrono::duration_cast<std::chrono::milliseconds>(a.t_ - b.t_).count());
483}
484
486inline bool operator==(monotonic_clock const& a, monotonic_clock const& b)
487{
488 return a.t_ == b.t_;
489}
490
492inline bool operator<(monotonic_clock const& a, monotonic_clock const& b)
493{
494 return a.t_ < b.t_;
495}
496
498inline bool operator<=(monotonic_clock const& a, monotonic_clock const& b)
499{
500 return a.t_ <= b.t_;
501}
502
504inline bool operator>(monotonic_clock const& a, monotonic_clock const& b)
505{
506 return a.t_ > b.t_;
507}
508
510inline bool operator>=(monotonic_clock const& a, monotonic_clock const& b)
511{
512 return a.t_ >= b.t_;
513}
514
515}
516
517#endif
Represents a point of time in wallclock, tracking the timestamps accuracy/precision.
Definition time.hpp:41
FILETIME get_filetime() const
Windows-only: Get timestamp as FILETIME.
int get_milliseconds() const
Get millisecond part of timestamp.
Definition time.hpp:211
bool later_than(datetime const &op) const
Equivalent to compare(op) > 0.
Definition time.hpp:131
bool set(std::string_view const &str, zone z)
Set from string, looks for YYYYmmDD[[[[HH]MM]SS]sss].
accuracy
The datetime's accuracy.
Definition time.hpp:46
tm get_tm(zone z) const
Get timestamp as struct tm.
bool empty() const
time_t get_time_t() const
Get timestamp as time_t, seconds since 1970-01-01 00:00:00.
bool set(tm &t, accuracy a, zone z)
bool imbue_time(int hour, int minute, int second=-1, int millisecond=-1)
Adds time to timestamps that only have a day-accuracy.
duration operator-(datetime const &a, datetime const &b)
Gets the difference between two timestamps as duration.
datetime(FILETIME const &ft, accuracy a)
Windows-only: Construct from FILETIME.
zone
When importing or exporting a timestamp, zone is used to explicitly specify whether the conversion is...
Definition time.hpp:58
static datetime now()
Returns the current date/time.
bool set_rfc3339(std::string_view const &str)
bool set(FILETIME const &ft, accuracy a)
Windows-only: Set timestamp from FILETIME.
std::string format(std::string const &format, zone z) const
datetime() noexcept=default
A default-constructed timestamp is empty().
bool set(SYSTEMTIME const &ft, accuracy a, zone z)
Windows-only: Set timestamp from SYSTEMTIME.
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.
bool earlier_than(datetime const &op) const
Equivalent to compare(op) < 0.
Definition time.hpp:128
void clear()
Resulting timestamp is empty().
int compare(datetime const &op) const
Accuracy-aware comparison against another timestamp.
std::string get_rfc822() const
static bool verify_format(std::string const &fmt)
bool set_rfc822(std::string_view const &str)
The duration class represents a time interval in milliseconds.
Definition time.hpp:298
A monotonic clock (aka steady clock) is independent from walltime.
Definition time.hpp:414
duration operator-(monotonic_clock const &a, monotonic_clock const &b)
Definition time.hpp:480
static monotonic_clock now()
Gets the current point in time time.
Definition time.hpp:442
monotonic_clock()=default
Constructs empty clock.
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.
bool operator<(strtokenizer< LhsString, LhsDelims > const &lhs, strtokenizer< RhsString, RhsDelims > const &rhs)
strtokenizer class less-than comparator.
Definition string.hpp:565