libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
datetime.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <chrono>
11#include <ctime>
13#include <promeki/core/string.h>
14#include <promeki/core/error.h>
15
17
39class DateTime {
40 public:
42 constexpr static const char *DefaultFormat = "%F %T";
43
45 using Value = std::chrono::system_clock::time_point;
46
48 static DateTime now() {
49 return DateTime(std::chrono::system_clock::now());
50 }
51
59 static DateTime fromString(const String &str, const char *fmt = DefaultFormat, Error *err = nullptr) {
60 std::tm tm = {};
61 tm.tm_isdst = -1;
62 const char *result = strptime(str.cstr(), fmt, &tm);
63 if(result == nullptr) {
64 if(err != nullptr) *err = Error::Invalid;
65 return DateTime();
66 }
67 if(err != nullptr) *err = Error::Ok;
68 return DateTime(tm);
69 }
70
77
84 static String strftime(const std::tm &tm, const char *format = DefaultFormat);
85
88
93 DateTime(const Value &val) : _value(val) { }
94
99 DateTime(std::tm val) : _value(std::chrono::system_clock::from_time_t(std::mktime(&val))) { }
100
106
109 return DateTime(_value + other._value.time_since_epoch());
110 }
111
114 return DateTime(_value - other._value.time_since_epoch());
115 }
116
119 _value += other._value.time_since_epoch();
120 return *this;
121 }
122
125 _value -= other._value.time_since_epoch();
126 return *this;
127 }
128
134 DateTime operator+(double seconds) const {
135 return DateTime(_value + std::chrono::duration_cast<std::chrono::system_clock::duration>(
136 std::chrono::duration<double>(seconds)));
137 }
138
144 DateTime operator-(double seconds) const {
145 return DateTime(_value - std::chrono::duration_cast<std::chrono::system_clock::duration>(
146 std::chrono::duration<double>(seconds)));
147 }
148
154 DateTime &operator+=(double seconds) {
155 _value += std::chrono::duration_cast<std::chrono::system_clock::duration>(
156 std::chrono::duration<double>(seconds));
157 return *this;
158 }
159
165 DateTime& operator-=(double seconds) {
166 _value -= std::chrono::duration_cast<std::chrono::system_clock::duration>(
167 std::chrono::duration<double>(seconds));
168 return *this;
169 }
170
172 bool operator==(const DateTime &other) const {
173 return _value == other._value;
174 }
175
177 bool operator!=(const DateTime &other) const {
178 return _value != other._value;
179 }
180
182 bool operator<(const DateTime &other) const {
183 return _value < other._value;
184 }
185
187 bool operator<=(const DateTime &other) const {
188 return _value <= other._value;
189 }
190
192 bool operator>(const DateTime &other) const {
193 return _value > other._value;
194 }
195
197 bool operator>=(const DateTime& other) const {
198 return _value >= other._value;
199 }
200
206 String toString(const char *format = DefaultFormat) const;
207
209 operator String() const {
210 return toString();
211 }
212
217 time_t toTimeT() const {
218 return std::chrono::system_clock::to_time_t(_value);
219 }
220
225 double toDouble() const {
226 return std::chrono::duration_cast<std::chrono::duration<double>>(
227 _value.time_since_epoch()).count();
228 }
229
234 Value value() const {
235 return _value;
236 }
237
238 private:
239 Value _value;
240};
241
242
244
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