libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
DateTime Class Reference

Wall-clock date and time based on std::chrono::system_clock. More...

#include <datetime.h>

Public Types

using Value = std::chrono::system_clock::time_point
 Underlying time point type from the system clock.
 

Public Member Functions

 DateTime ()
 Default constructor. The resulting time point is epoch (uninitialized).
 
 DateTime (const Value &val)
 Constructs a DateTime from a system_clock time point.
 
 DateTime (std::tm val)
 Constructs a DateTime from a broken-down std::tm value.
 
 DateTime (time_t val)
 Constructs a DateTime from a time_t value.
 
DateTime operator+ (const DateTime &other) const
 Returns the sum of two DateTime time points.
 
DateTime operator- (const DateTime &other) const
 Returns the difference of two DateTime time points.
 
DateTimeoperator+= (const DateTime &other)
 Adds another DateTime's duration to this one.
 
DateTimeoperator-= (const DateTime &other)
 Subtracts another DateTime's duration from this one.
 
DateTime operator+ (double seconds) const
 Returns a DateTime offset forward by the given number of seconds.
 
DateTime operator- (double seconds) const
 Returns a DateTime offset backward by the given number of seconds.
 
DateTimeoperator+= (double seconds)
 Advances this DateTime forward by the given number of seconds.
 
DateTimeoperator-= (double seconds)
 Moves this DateTime backward by the given number of seconds.
 
bool operator== (const DateTime &other) const
 Returns true if both DateTimes represent the same time point.
 
bool operator!= (const DateTime &other) const
 Returns true if the DateTimes represent different time points.
 
bool operator< (const DateTime &other) const
 Returns true if this DateTime is earlier than other.
 
bool operator<= (const DateTime &other) const
 Returns true if this DateTime is earlier than or equal to other.
 
bool operator> (const DateTime &other) const
 Returns true if this DateTime is later than other.
 
bool operator>= (const DateTime &other) const
 Returns true if this DateTime is later than or equal to other.
 
String toString (const char *format=DefaultFormat) const
 Formats the DateTime as a string.
 
 operator String () const
 Implicit conversion to String via toString().
 
time_t toTimeT () const
 Converts the DateTime to a POSIX time_t value.
 
double toDouble () const
 Converts the DateTime to a floating-point seconds value.
 
Value value () const
 Returns the underlying system_clock time point.
 

Static Public Member Functions

static DateTime now ()
 Returns a DateTime representing the current wall-clock time.
 
static DateTime fromString (const String &str, const char *fmt=DefaultFormat, Error *err=nullptr)
 Parses a DateTime from a formatted string.
 
static DateTime fromNow (const String &description)
 Creates a DateTime relative to the current time from a natural-language description.
 
static String strftime (const std::tm &tm, const char *format=DefaultFormat)
 Formats a std::tm value using a strftime-style format string.
 

Static Public Attributes

static constexpr const charDefaultFormat = "%F %T"
 Default strftime format string ("%F %T" = "YYYY-MM-DD HH:MM:SS").
 

Detailed Description

Wall-clock date and time based on std::chrono::system_clock.

Wraps a system_clock time_point and provides construction from strings, time_t, and std::tm values. Supports arithmetic with other DateTime instances and with floating-point second offsets. Output formatting

Example
String formatted = now.toString("%Y-%m-%d %H:%M:%S");
// Parse from string
DateTime dt = DateTime::fromString("2025-03-15 14:30:00");
// Arithmetic with seconds
DateTime later = now + 3600.0; // one hour later
Wall-clock date and time based on std::chrono::system_clock.
Definition datetime.h:39
static DateTime now()
Returns a DateTime representing the current wall-clock time.
Definition datetime.h:48
static DateTime fromString(const String &str, const char *fmt=DefaultFormat, Error *err=nullptr)
Parses a DateTime from a formatted string.
Definition datetime.h:59
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
uses strftime-style format strings.

Constructor & Destructor Documentation

◆ DateTime() [1/3]

DateTime::DateTime ( const Value val)
inline

Constructs a DateTime from a system_clock time point.

Parameters
valThe time point value.

◆ DateTime() [2/3]

DateTime::DateTime ( std::tm  val)
inline

Constructs a DateTime from a broken-down std::tm value.

Parameters
valThe calendar time to convert.

◆ DateTime() [3/3]

DateTime::DateTime ( time_t  val)
inline

Constructs a DateTime from a time_t value.

Parameters
valThe POSIX time value.

Member Function Documentation

◆ fromNow()

static DateTime DateTime::fromNow ( const String description)
static

Creates a DateTime relative to the current time from a natural-language description.

Parameters
descriptionA human-readable offset description (e.g. "2 hours ago").
Returns
The computed DateTime.

◆ fromString()

static DateTime DateTime::fromString ( const String str,
const char fmt = DefaultFormat,
Error err = nullptr 
)
inlinestatic

Parses a DateTime from a formatted string.

Parameters
strThe date/time string to parse.
fmtThe strftime-style format string (default: DefaultFormat).
errOptional error output; set to Error::Invalid on parse failure.
Returns
The parsed DateTime, or a default-constructed DateTime on failure.

◆ operator+()

DateTime DateTime::operator+ ( double  seconds) const
inline

Returns a DateTime offset forward by the given number of seconds.

Parameters
secondsNumber of seconds to add.
Returns
The offset DateTime.

◆ operator+=()

DateTime & DateTime::operator+= ( double  seconds)
inline

Advances this DateTime forward by the given number of seconds.

Parameters
secondsNumber of seconds to add.
Returns
Reference to this DateTime.

◆ operator-()

DateTime DateTime::operator- ( double  seconds) const
inline

Returns a DateTime offset backward by the given number of seconds.

Parameters
secondsNumber of seconds to subtract.
Returns
The offset DateTime.

◆ operator-=()

DateTime & DateTime::operator-= ( double  seconds)
inline

Moves this DateTime backward by the given number of seconds.

Parameters
secondsNumber of seconds to subtract.
Returns
Reference to this DateTime.

◆ strftime()

static String DateTime::strftime ( const std::tm &  tm,
const char format = DefaultFormat 
)
static

Formats a std::tm value using a strftime-style format string.

Parameters
tmThe broken-down time to format.
formatThe strftime-style format string (default: DefaultFormat).
Returns
The formatted date/time string.

◆ toDouble()

double DateTime::toDouble ( ) const
inline

Converts the DateTime to a floating-point seconds value.

Returns
Seconds since the Unix epoch as a double.

◆ toString()

String DateTime::toString ( const char format = DefaultFormat) const

Formats the DateTime as a string.

Parameters
formatThe strftime-style format string (default: DefaultFormat).
Returns
The formatted date/time string.

◆ toTimeT()

time_t DateTime::toTimeT ( ) const
inline

Converts the DateTime to a POSIX time_t value.

Returns
The time as seconds since the Unix epoch.

◆ value()

Value DateTime::value ( ) const
inline

Returns the underlying system_clock time point.

Returns
The stored time point value.

The documentation for this class was generated from the following file: