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);
90 explicit operator bool()
const {
97 accuracy get_accuracy()
const {
return a_; }
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; }
140 datetime& operator-=(duration
const& op);
141 datetime operator-(duration
const& op)
const { datetime t(*
this); t -= op;
return t; }
153 bool set(
zone z,
int year,
int month,
int day,
int hour = -1,
int minute = -1,
int second = -1,
int millisecond = -1);
164 bool set(std::string_view
const& str,
zone z);
165 bool set(std::wstring_view
const& str,
zone z);
174 #if defined(FZ_UNIX) || defined(FZ_MAC)
189 bool imbue_time(
int hour,
int minute,
int second = -1,
int millisecond = -1);
198 std::wstring format(std::wstring
const& format,
zone z)
const;
206 static bool verify_format(std::wstring
const& fmt);
249 bool set_rfc822(std::wstring_view
const& str);
263 bool set_rfc3339(std::wstring_view
const& str);
266 int FZ_PRIVATE_SYMBOL compare_slow(
datetime const& op)
const;
268 bool FZ_PRIVATE_SYMBOL clamped();
270 enum invalid_t : int64_t {
271 invalid = std::numeric_limits<int64_t>::min()
294 int64_t get_days()
const {
return ms_ / 1000 / 3600 / 24; }
295 int64_t get_hours()
const {
return ms_ / 1000 / 3600; }
296 int64_t get_minutes()
const {
return ms_ / 1000 / 60; }
297 int64_t get_seconds()
const {
return ms_ / 1000; }
298 int64_t get_milliseconds()
const {
return ms_; }
301 static duration from_days(int64_t m) {
302 return duration(m * 1000 * 60 * 60 * 24);
304 static duration from_hours(int64_t m) {
305 return duration(m * 1000 * 60 * 60);
307 static duration from_minutes(int64_t m) {
310 static duration from_seconds(int64_t m) {
313 static duration from_milliseconds(int64_t m) {
332 explicit operator bool()
const {
341 bool operator<(
duration const& op)
const {
return ms_ < op.ms_; }
342 bool operator<=(
duration const& op)
const {
return ms_ <= op.ms_; }
343 bool operator>(
duration const& op)
const {
return ms_ > op.ms_; }
344 bool operator>=(
duration const& op)
const {
return ms_ >= op.ms_; }
349 explicit FZ_PRIVATE_SYMBOL
duration(int64_t ms) : ms_(ms) {}
359 inline duration operator+(duration
const& a, duration
const& b)
361 return duration(a) += b;
402 typedef std::chrono::steady_clock clock_type;
403 static_assert(std::chrono::steady_clock::is_steady,
"Nonconforming stdlib, your steady_clock isn't steady");
411 explicit operator bool()
const {
412 return t_ != clock_type::time_point();
415 monotonic_clock& operator+=(duration
const& d)
417 t_ += std::chrono::milliseconds(d.get_milliseconds());
421 monotonic_clock& operator-=(duration
const& d)
423 t_ -= std::chrono::milliseconds(d.get_milliseconds());
428 explicit FZ_PRIVATE_SYMBOL monotonic_clock(clock_type::time_point
const& t)
432 clock_type::time_point t_;
434 friend duration operator-(monotonic_clock
const& a, monotonic_clock
const& b);
435 friend bool operator==(monotonic_clock
const& a, monotonic_clock
const& b);
436 friend bool operator<(monotonic_clock
const& a, monotonic_clock
const& b);
437 friend bool operator<=(monotonic_clock
const& a, monotonic_clock
const& b);
438 friend bool operator>(monotonic_clock
const& a, monotonic_clock
const& b);
439 friend bool operator>=(monotonic_clock
const& a, monotonic_clock
const& b);
448 return duration::from_milliseconds(std::chrono::duration_cast<std::chrono::milliseconds>(a.t_ - b.t_).count());
458 inline bool operator<(monotonic_clock
const& a, monotonic_clock
const& b)
464 inline bool operator<=(monotonic_clock
const& a, monotonic_clock
const& b)
470 inline bool operator>(monotonic_clock
const& a, monotonic_clock
const& b)
476 inline bool operator>=(monotonic_clock
const& a, monotonic_clock
const& b)
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:209
bool later_than(datetime const &op) const
Equivalent to compare(op) > 0.
Definition: time.hpp:130
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.
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.
datetime()=default
A default-constructed timestamp is empty()
friend 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
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:127
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)
datetime(std::string_view const &s, zone z)
Construct from string, looks for YYYYmmDD[[[[HH]MM]SS]sss].
The duration class represents a time interval in milliseconds.
Definition: time.hpp:286
duration operator-(datetime const &a, datetime const &b)
Gets the difference between two timestamps as duration.
A monotonic clock (aka steady clock) is independent from walltime.
Definition: time.hpp:383
static monotonic_clock now()
Gets the current point in time time.
Definition: time.hpp:407
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.