Wt: Wt::WDateTime Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions
Wt::WDateTime Class Reference

A calendar date and clock time. More...

`#include <Wt/WDateTime>`

List of all members.

## Public Member Functions
| WDateTime ()
| Creates a Null datetime.
| WDateTime (const WDate &date)
| Creates a datetime given a date.
| WDateTime (const WDate &date, const WTime &time)
| Creates a datetime given a date and time.
void | setTime_t (std::time_t time)
| Sets the time in seconds from the Epoch.
void | setPosixTime (const boost::posix_time::ptime &time)
| Sets the date time.
WDateTime | addMSecs (int ms) const
| Adds milliseconds.
WDateTime | addSecs (int s) const
| Adds seconds.
WDateTime | addDays (int ndays) const
| Adds days.
WDateTime | addMonths (int nmonths) const
| Adds months.
WDateTime | addYears (int nyears) const
| Adds years.
bool | isNull () const
| Returns if this datetime is Null.
bool | isValid () const
| Returns if this datetime is valid.
void | setDate (const WDate &date)
| Sets the date part.
const WDate | date () const
| Returns the date part.
void | setTime (const WTime &time)
| Sets the time part.
const WTime | time () const
| Returns the time part.
std::time_t | toTime_t () const
| Returns the number of seconds since the Epoch.
boost::posix_time::ptime | toPosixTime () const
| Returns the boost time.
int | secsTo (const WDateTime &other) const
| Returns the difference between two datetime values (in seconds).
int | daysTo (const WDateTime &other) const
| Returns the difference between two datetime values (in days).
WString | timeTo (const WDateTime &other, int minValue=1) const
| Returns the difference between two datetime values (as text).
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.
WString | toString (const WString &format, bool localized=true) const
| Formats this time to a string using a specified format.
## Static Public Member Functions
static WDateTime | fromString (const WString &s)
| Parses a string to a time using a default format.
static WDateTime | fromString (const WString &s, const WString &format)
| Parses a string to a time using a specified format.
static WDateTime | currentDateTime ()
| Reports the current UTC datetime.
static WDateTime | fromTime_t (std::time_t seconds)
| Creates a date time based on a number of seconds since the Epoch.
static WDateTime | fromPosixTime (const boost::posix_time::ptime &datetime)
| Creates a date time from boost's date time type.

## Detailed Description

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.

Conventionally date time is assumed in UTC, which affects the behaviour of the following methods:

- toTime_t(), setTime_t()
- currentDateTime()

### i18n

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":

- Wt.WDateTime.null: Null

The timeTo() method contains the following localized strings:

- Wt.WDateTime.second: one second
- Wt.WDateTime.seconds: {1} seconds The placeholder {1} will be replaced by the actual number of seconds. The same keys also exist for minutes, hours, days, weeks, monts and years.

## Constructor & Destructor Documentation

Wt::WDateTime::WDateTime | ( |  | ) |

Creates a Null datetime.

A time for which isNull() returns `true`. A Null datetime is also invalid.

See also:
  isValid(), isNull()

Wt::WDateTime::WDateTime | ( | const WDate & | date | ) | `[explicit]`

Creates a datetime given a date.

The time is set to midnight (00:00). The datetime is valid if the `date` is valid.

Wt::WDateTime::WDateTime | ( | const WDate & | date,
|  | const WTime & | time
| ) |  |

Creates a datetime given a date and time.

The datetime is valid if both `date` and `time` are valid.

## Member Function Documentation

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.

See also:
  addMonths(), addYears()

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.

See also:
  addDays(), addYears()

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.

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.

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.

See also:
  addDays(), addMonths()

static WDateTime Wt::WDateTime::currentDateTime | ( |  | ) | `[static]`

Reports the current UTC datetime.

This method returns the datetime as indicated by the system clock of the server, in the UTC timezone.

const WDate Wt::WDateTime::date | ( |  | ) | const

Returns the date part.

Returns the date part.

See also:
  time()

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.

See also:
  secsTo()

static WDateTime Wt::WDateTime::fromPosixTime | ( | const boost::posix_time::ptime & | datetime | ) | `[static]`

Creates a date time from boost's date time type.

See also:
  toPosixTime()

static WDateTime Wt::WDateTime::fromString | ( | const WString & | s | ) | `[static]`

Parses a string to a time using a default format.

The default format is "ddd MMM d hh:mm:ss yyyy".

See also:
  WDate::fromString(), WTime::fromString().

static WDateTime Wt::WDateTime::fromString | ( | const WString & | s,
|  | const WString & | format
| ) |  | `[static]`

Parses a string to a time using a specified format.

The `format` is a string which mixes the format for WDate and WTime.

See also:
  WDate::fromString(), WTime::toString().

static WDateTime Wt::WDateTime::fromTime_t | ( | std::time_t | seconds | ) | `[static]`

Creates a date time based on a number of seconds since the Epoch.

See also:
  setTime_t()

bool Wt::WDateTime::isNull | ( |  | ) | const

Returns if this datetime is Null.

A null time is also invalid.

See also:
  isValid(), WDateTime()

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.

See also:
  daysTo()

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.

See also:
  setTime()

void Wt::WDateTime::setTime | ( | const WTime & | time | ) |

Sets the time part.

If no valid date is set, the time is not set either.

See also:
  setDate()

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).

See also:
  toTime_t()

const WTime Wt::WDateTime::time | ( |  | ) | const

Returns the time part.

See also:
  setTime()

WString Wt::WDateTime::timeTo | ( | const WDateTime & | other,
|  | int | minValue = `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`.

See also:
  daysTo(), secsTo()

boost::posix_time::ptime Wt::WDateTime::toPosixTime | ( |  | ) | const

Returns the boost time.

See also:
  fromPosixTime()

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".

WString Wt::WDateTime::toString | ( | const WString & | format,
|  | bool | localized = `true`
| ) |  | const

Formats this time to a string using a specified format.

The `format` is a string which mixes the format for WDate and WTime.

See also:
  WDate::toString(), WTime::toString()

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.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator