1 #ifndef LIBFILEZILLA_TIME_HEADER
2 #define LIBFILEZILLA_TIME_HEADER
12 #include "glue/windows.hpp"
21 class FZ_PUBLIC_SYMBOL duration;
66 datetime(zone z,
int year,
int month,
int day,
int hour = -1,
int minute = -1,
int second = -1,
int millisecond = -1);
68 explicit
datetime(time_t, accuracy a);
74 explicit
datetime(
std::string_view const& s, zone z);
75 explicit
datetime(
std::wstring_view const& s, zone z);
78 explicit datetime(FILETIME
const& ft, accuracy a);
90 explicit operator
bool()
const {
97 accuracy get_accuracy()
const {
return a_; }
100 static datetime now();
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; }
125 int compare(datetime
const& op)
const;
141 datetime& operator-=(duration
const& op);
142 datetime operator-(duration
const& op)
const { datetime t(*
this); t -= op;
return t; }
145 friend duration FZ_PUBLIC_SYMBOL operator-(datetime
const& a, datetime
const& b);
154 bool set(zone z,
int year,
int month,
int day,
int hour = -1,
int minute = -1,
int second = -1,
int millisecond = -1);
165 bool set(std::string_view
const& str, zone z);
166 bool set(std::wstring_view
const& str, zone z);
169 bool set(FILETIME
const& ft, accuracy a);
172 bool set(SYSTEMTIME
const& ft, accuracy a, zone z);
175 #if defined(FZ_UNIX) || defined(FZ_MAC)
181 bool set(tm & t, accuracy a, zone z);
190 bool imbue_time(
int hour,
int minute,
int second = -1,
int millisecond = -1);
198 std::string format(std::string
const& format, zone z)
const;
199 std::wstring format(std::wstring
const& format, zone z)
const;
206 static bool verify_format(std::string
const& fmt);
207 static bool verify_format(std::wstring
const& fmt);
213 time_t get_time_t()
const;
221 tm get_tm(zone z)
const;
224 FILETIME get_filetime()
const;
234 std::string get_rfc822()
const;
251 bool set_rfc822(std::string_view
const& str);
252 bool set_rfc822(std::wstring_view
const& str);
265 bool set_rfc3339(std::string_view
const& str);
266 bool set_rfc3339(std::wstring_view
const& str);
269 int FZ_PRIVATE_SYMBOL compare_slow(
datetime const& op)
const;
271 bool FZ_PRIVATE_SYMBOL clamped();
273 enum invalid_t : int64_t {
274 invalid = std::numeric_limits<int64_t>::min()
299 int64_t get_days()
const {
return ms_ / 1000 / 3600 / 24; }
300 int64_t get_hours()
const {
return ms_ / 1000 / 3600; }
301 int64_t get_minutes()
const {
return ms_ / 1000 / 60; }
302 int64_t get_seconds()
const {
return ms_ / 1000; }
303 int64_t get_milliseconds()
const {
return ms_; }
306 static duration from_days(int64_t m) {
307 return duration(m * 1000 * 60 * 60 * 24);
309 static duration from_hours(int64_t m) {
310 return duration(m * 1000 * 60 * 60);
312 static duration from_minutes(int64_t m) {
315 static duration from_seconds(int64_t m) {
318 static duration from_milliseconds(int64_t m) {
342 explicit operator bool()
const {
352 return (ms_ < 0) ?
duration(-ms_) : *
this;
355 bool operator<(
duration const& op)
const {
return ms_ < op.ms_; }
356 bool operator<=(
duration const& op)
const {
return ms_ <= op.ms_; }
357 bool operator>(
duration const& op)
const {
return ms_ > op.ms_; }
358 bool operator>=(
duration const& op)
const {
return ms_ >= op.ms_; }
364 explicit FZ_PRIVATE_SYMBOL
duration(int64_t ms) : ms_(ms) {}
374 inline duration operator+(duration
const& a, duration
const& b)
376 return duration(a) += b;
379 inline duration operator/(duration
const& a, int64_t b)
381 return duration(a) /= b;
390 duration FZ_PUBLIC_SYMBOL operator-(datetime
const& a, datetime
const& b);
426 typedef std::chrono::steady_clock clock_type;
427 static_assert(std::chrono::steady_clock::is_steady,
"Nonconforming stdlib, your steady_clock isn't steady");
435 explicit operator bool()
const {
436 return t_ != clock_type::time_point();
439 monotonic_clock& operator+=(duration
const& d)
441 t_ += std::chrono::milliseconds(d.get_milliseconds());
445 monotonic_clock& operator-=(duration
const& d)
447 t_ -= std::chrono::milliseconds(d.get_milliseconds());
452 explicit FZ_PRIVATE_SYMBOL monotonic_clock(clock_type::time_point
const& t)
456 clock_type::time_point t_;
458 friend duration operator-(monotonic_clock
const& a, monotonic_clock
const& b);
459 friend bool operator==(monotonic_clock
const& a, monotonic_clock
const& b);
460 friend bool operator<(monotonic_clock
const& a, monotonic_clock
const& b);
461 friend bool operator<=(monotonic_clock
const& a, monotonic_clock
const& b);
462 friend bool operator>(monotonic_clock
const& a, monotonic_clock
const& b);
463 friend bool operator>=(monotonic_clock
const& a, monotonic_clock
const& b);
472 return duration::from_milliseconds(std::chrono::duration_cast<std::chrono::milliseconds>(a.t_ - b.t_).count());
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:476
Definition: impersonation.hpp:81
bool operator<(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:482
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:402
bool operator>(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:494
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:290
Sets some global macros and further includes string.hpp.
bool operator<=(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:488
bool operator>=(monotonic_clock const &a, monotonic_clock const &b)
Definition: time.hpp:500
static monotonic_clock now()
Gets the current point in time time.
Definition: time.hpp:431
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