Wt
4.11.3
|
A calendar date and clock time. More...
#include <Wt/WDateTime.h>
Public Member Functions | |
WDateTime () | |
Creates a Null datetime. More... | |
WDateTime (const WDate &date) | |
Creates a datetime given a date. More... | |
WDateTime (const WDate &date, const WTime &time) | |
Creates a datetime given a date and time. More... | |
WDateTime (const std::chrono::system_clock::time_point timepoint) | |
Convert std::chrono::system_clock::time_point to a date time. | |
void | setTime_t (std::time_t time) |
Sets the time in seconds from the Epoch. More... | |
void | setTimePoint (const std::chrono::system_clock::time_point &timepoint) |
Sets the date time. | |
WDateTime | addMSecs (int ms) const |
Adds milliseconds. More... | |
WDateTime | addSecs (int s) const |
Adds seconds. More... | |
WDateTime | addDays (int ndays) const |
Adds days. More... | |
WDateTime | addMonths (int nmonths) const |
Adds months. More... | |
WDateTime | addYears (int nyears) const |
Adds years. More... | |
bool | isNull () const |
Returns if this datetime is Null. More... | |
bool | isValid () const |
Returns if this datetime is valid. More... | |
void | setDate (const WDate &date) |
Sets the date part. More... | |
const WDate | date () const |
Returns the date part. More... | |
void | setTime (const WTime &time) |
Sets the time part. More... | |
const WTime | time () const |
Returns the time part. More... | |
std::time_t | toTime_t () const |
Returns the number of seconds since the Epoch. More... | |
std::chrono::system_clock::time_point | toTimePoint () const |
Returns the boost time. More... | |
operator std::chrono::system_clock::time_point () const | |
Convert to std::chrono::system_clock::time_point. | |
WLocalDateTime | toLocalTime (const WLocale &locale=WLocale::currentLocale()) const |
Converts to a local time. More... | |
int | secsTo (const WDateTime &other) const |
Returns the difference between two datetime values (in seconds). More... | |
int | daysTo (const WDateTime &other) const |
Returns the difference between two datetime values (in days). More... | |
WString | timeTo (const WDateTime &other, std::chrono::seconds minValue=std::chrono::seconds(1)) const |
Returns the difference between two datetime values (as text). More... | |
bool | operator< (const WDateTime &other) const |
Compares two datetime values. | |
bool | operator<= (const WDateTime &other) const |
Compares two datetime values. | |
bool | operator> (const WDateTime &other) const |
Compares two datetime values. | |
bool | operator>= (const WDateTime &other) const |
Compares two datetime values. | |
bool | operator== (const WDateTime &other) const |
Compares two datetime values. | |
bool | operator!= (const WDateTime &other) const |
Compares two datetime values. | |
WString | toString () const |
Formats this datetime to a string using a default format. More... | |
WString | toString (const WString &format, bool localizedString=true) const |
Formats this time to a string using a specified format. More... | |
Static Public Member Functions | |
static WDateTime | fromString (const WString &s) |
Parses a string to a time using a default format. More... | |
static WDateTime | fromString (const WString &s, const WString &format) |
Parses a string to a time using a specified format. More... | |
static WDateTime | currentDateTime () |
Reports the current datetime (UTC clock). More... | |
static WDateTime | fromTime_t (std::time_t seconds) |
Creates a date time based on a number of seconds since the Epoch. More... | |
static WDateTime | fromTimePoint (const std::chrono::system_clock::time_point &datetime) |
Creates a date time from boost's date time type. More... | |
A calendar date and clock time.
The date time class combines the functionality of a WDate (for a calendar date) and WTime (for clock time) into a single class.
This class stores and represents the date time in UTC. To deal with local (wall clock) time, see WLocalDateTime. To convert from UTC to local time use toLocalTime().
The strings returned by toString() and timeTo() can be localized. If the thread using a WDateTime is bound to a WApplication (i.e. WApplication::instance() does not return 0), the strings can be localized by overriding the default values for the localized string keys in the resource bundles of the WApplication. If the thread using a WDateTime is not bound to a WApplication (i.e. WApplication::instance() returns 0), English strings will be used.
For toString(), localization is handled through the i18n functionality of the WDate class. An invalid WDateTime is converted to a string as "Null":
The timeTo() method contains the following localized strings:
The default translations of Wt.WDateTime.seconds are "one second" and "{1} seconds". The WString::trn() function is used for the translation, so different strings can be provided for different plural forms. The placeholder {1} will be replaced by the actual number of seconds. The same keys also exist for minutes, hours, days, weeks, months and years.
Wt::WDateTime::WDateTime | ( | ) |
|
explicit |
Creates a datetime given a date.
The time is set to midnight (00:00). The datetime is valid if the date
is valid.
Creates a datetime given a date and time.
The datetime is valid if both date
and time
are valid.
WDateTime Wt::WDateTime::addDays | ( | int | ndays | ) | const |
Adds days.
Returns a datetime that is ndays
later than this datetime. Negative values for ndays
will result in a datetime that is as many days earlier.
Returns a null date if the current date time is invalid or the new date time is out of range.
WDateTime Wt::WDateTime::addMonths | ( | int | nmonths | ) | const |
Adds months.
Returns a datetime that is the same day of the month, but nmonths
later than this date. Negative values for nmonths
will result in a datetime that is as many months earlier.
Returns a null date if the current date time is invalid or the new date time is out of range.
WDateTime Wt::WDateTime::addMSecs | ( | int | ms | ) | const |
Adds milliseconds.
Returns a datetime that is ms
milliseconds later than this datetime. Negative values for ms
will result in a datetime that is as many milliseconds earlier.
Returns a null date if the current date time is invalid or the new date time is out of range.
WDateTime Wt::WDateTime::addSecs | ( | int | s | ) | const |
Adds seconds.
Returns a datetime that is s
seconds later than this datetime. Negative values for s
will result in a datetime that is as many seconds earlier.
Returns a null date if the current date time is invalid or the new date time is out of range.
WDateTime Wt::WDateTime::addYears | ( | int | nyears | ) | const |
Adds years.
Returns a datetime that is nyears
later than this datetime. Negative values for nyears
will result in a datetime that is as many years earlier.
Returns a null date if the current date time is invalid or the new date time is out of range.
|
static |
Reports the current datetime (UTC clock).
This method returns the datetime as indicated by the system clock of the server, in UTC.
const WDate Wt::WDateTime::date | ( | ) | const |
int Wt::WDateTime::daysTo | ( | const WDateTime & | other | ) | const |
Returns the difference between two datetime values (in days).
The result is negative if other is earlier than this.
Returns 0 if either date is invalid.
Parses a string to a time using a default format.
The default format is "ddd MMM d hh:mm:ss yyyy".
Parses a string to a time using a specified format.
The format
is a string which mixes the format for WDate and WTime.
|
static |
Creates a date time based on a number of seconds since the Epoch.
|
static |
Creates a date time from boost's date time type.
bool Wt::WDateTime::isNull | ( | ) | const |
bool Wt::WDateTime::isValid | ( | ) | const |
Returns if this datetime is valid.
A date time is only valid if its date and time parts are valid.
int Wt::WDateTime::secsTo | ( | const WDateTime & | other | ) | const |
Returns the difference between two datetime values (in seconds).
The result is negative if other is earlier than this.
Returns 0 if either date is invalid.
void Wt::WDateTime::setDate | ( | const WDate & | date | ) |
Sets the date part.
Changes the date part part, leaving the time unmodified. If no time was set, it is set to 00:00.
void Wt::WDateTime::setTime | ( | const WTime & | time | ) |
void Wt::WDateTime::setTime_t | ( | std::time_t | time | ) |
Sets the time in seconds from the Epoch.
The time
is the number of seconds since the Epoch (00:00:00 UTC, January 1, 1970).
WString Wt::WDateTime::timeTo | ( | const WDateTime & | other, |
std::chrono::seconds | minValue = std::chrono::seconds(1) |
||
) | const |
Returns the difference between two datetime values (as text).
This returns a textual representation of the approximate difference between this time and other
. The textual representation returns the difference as a number of seconds, minutes, hours, days, weeks, months, or years, using the coarsest unit that is more than minValue
.
Returns an empty string if either date is invalid.
WLocalDateTime Wt::WDateTime::toLocalTime | ( | const WLocale & | locale = WLocale::currentLocale() | ) | const |
Converts to a local time.
The conversion is based on the fact that WDateTime represents UTC time.
This is the reverse of WLocalDateTime::toUTC()
WString Wt::WDateTime::toString | ( | ) | const |
Formats this datetime to a string using a default format.
The default format is "ddd MMM d hh:mm:ss yyyy".
Formats this time to a string using a specified format.
The format
is a string which mixes the format for WDate and WTime.
The localizedString
allows for the potential strings that are created to be localized or not.
E.g. If "ddd" is part of the format, the abbreviated day is used. If localizedString
is true, and if the abbreviations are defined in a resource bundle (overwriting the default "Wt.WDate.3.Mon" for example), they will be used.
std::time_t Wt::WDateTime::toTime_t | ( | ) | const |
Returns the number of seconds since the Epoch.
This returns the number of seconds since the Epoch (00:00:00 UTC, January 1, 1970) represented by this datetime.
std::chrono::system_clock::time_point Wt::WDateTime::toTimePoint | ( | ) | const |
Returns the boost time.