45 using Value = std::chrono::system_clock::time_point;
49 return DateTime(std::chrono::system_clock::now());
63 if(result ==
nullptr) {
119 _value +=
other._value.time_since_epoch();
125 _value -=
other._value.time_since_epoch();
135 return DateTime(_value + std::chrono::duration_cast<std::chrono::system_clock::duration>(
136 std::chrono::duration<double>(seconds)));
145 return DateTime(_value - std::chrono::duration_cast<std::chrono::system_clock::duration>(
146 std::chrono::duration<double>(seconds)));
155 _value += std::chrono::duration_cast<std::chrono::system_clock::duration>(
156 std::chrono::duration<double>(seconds));
166 _value -= std::chrono::duration_cast<std::chrono::system_clock::duration>(
167 std::chrono::duration<double>(seconds));
173 return _value ==
other._value;
178 return _value !=
other._value;
183 return _value <
other._value;
188 return _value <=
other._value;
193 return _value >
other._value;
198 return _value >=
other._value;
218 return std::chrono::system_clock::to_time_t(_value);
226 return std::chrono::duration_cast<std::chrono::duration<double>>(
227 _value.time_since_epoch()).count();
Wall-clock date and time based on std::chrono::system_clock.
Definition datetime.h:39
bool operator<(const DateTime &other) const
Returns true if this DateTime is earlier than other.
Definition datetime.h:182
DateTime(time_t val)
Constructs a DateTime from a time_t value.
Definition datetime.h:105
DateTime & operator+=(const DateTime &other)
Adds another DateTime's duration to this one.
Definition datetime.h:118
bool operator>=(const DateTime &other) const
Returns true if this DateTime is later than or equal to other.
Definition datetime.h:197
DateTime()
Default constructor. The resulting time point is epoch (uninitialized).
Definition datetime.h:87
static String strftime(const std::tm &tm, const char *format=DefaultFormat)
Formats a std::tm value using a strftime-style format string.
Value value() const
Returns the underlying system_clock time point.
Definition datetime.h:234
static DateTime fromNow(const String &description)
Creates a DateTime relative to the current time from a natural-language description.
static constexpr const char * DefaultFormat
Default strftime format string ("%F %T" = "YYYY-MM-DD HH:MM:SS").
Definition datetime.h:42
DateTime & operator-=(double seconds)
Moves this DateTime backward by the given number of seconds.
Definition datetime.h:165
static DateTime now()
Returns a DateTime representing the current wall-clock time.
Definition datetime.h:48
bool operator==(const DateTime &other) const
Returns true if both DateTimes represent the same time point.
Definition datetime.h:172
time_t toTimeT() const
Converts the DateTime to a POSIX time_t value.
Definition datetime.h:217
DateTime operator+(double seconds) const
Returns a DateTime offset forward by the given number of seconds.
Definition datetime.h:134
bool operator!=(const DateTime &other) const
Returns true if the DateTimes represent different time points.
Definition datetime.h:177
double toDouble() const
Converts the DateTime to a floating-point seconds value.
Definition datetime.h:225
DateTime & operator+=(double seconds)
Advances this DateTime forward by the given number of seconds.
Definition datetime.h:154
DateTime operator+(const DateTime &other) const
Returns the sum of two DateTime time points.
Definition datetime.h:108
DateTime(const Value &val)
Constructs a DateTime from a system_clock time point.
Definition datetime.h:93
bool operator>(const DateTime &other) const
Returns true if this DateTime is later than other.
Definition datetime.h:192
DateTime(std::tm val)
Constructs a DateTime from a broken-down std::tm value.
Definition datetime.h:99
bool operator<=(const DateTime &other) const
Returns true if this DateTime is earlier than or equal to other.
Definition datetime.h:187
static DateTime fromString(const String &str, const char *fmt=DefaultFormat, Error *err=nullptr)
Parses a DateTime from a formatted string.
Definition datetime.h:59
DateTime operator-(const DateTime &other) const
Returns the difference of two DateTime time points.
Definition datetime.h:113
DateTime & operator-=(const DateTime &other)
Subtracts another DateTime's duration from this one.
Definition datetime.h:124
String toString(const char *format=DefaultFormat) const
Formats the DateTime as a string.
DateTime operator-(double seconds) const
Returns a DateTime offset backward by the given number of seconds.
Definition datetime.h:144
std::chrono::system_clock::time_point Value
Underlying time point type from the system clock.
Definition datetime.h:45
Lightweight error code wrapper for the promeki library.
Definition error.h:39
@ Ok
No error.
Definition error.h:51
@ Invalid
Invalid value or argument (EINVAL).
Definition error.h:66
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
const char * cstr() const
Returns a null-terminated C string pointer.
Definition string.h:289
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19