Wt: Wt::WTime Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
Wt::WTime Class Reference

A value class that defines a clock time. More...

`#include <Wt/WTime.h>`

## Public Member Functions
| WTime ()
| Construct a Null time.
| WTime (int h, int m, int s=0, int ms=0)
| Construct a time given hour, minutes, seconds, and milliseconds.
bool | setHMS (int h, int m, int s, int ms=0)
| Sets the time.
WTime | addSecs (int s) const
| Adds seconds.
WTime | addMSecs (int ms) const
| Adds milliseconds.
bool | isNull () const
| Returns if this time is Null.
bool | isValid () const
| Returns if this time is valid.
int | hour () const
| Returns the hour.
int | minute () const
| Returns the minutes (0-59).
int | second () const
| Returns the seconds (0-59).
int | msec () const
| Returns the milliseconds (0-999)
long | secsTo (const WTime &t) const
| Returns the difference between two time values (in seconds).
long | msecsTo (const WTime &t) const
| Returns the difference between two time values (in milliseconds).
bool | operator< (const WTime &other) const
| Compares two time values.
bool | operator<= (const WTime &other) const
| Compares two time values.
bool | operator> (const WTime &other) const
| Compares two time values.
bool | operator>= (const WTime &other) const
| Compares two time values.
bool | operator== (const WTime &other) const
| Compares two time values.
bool | operator!= (const WTime &other) const
| Compares two time values.
WString | toString () const
| Formats this time to a string using a default format.
WString | toString (const WString &format) const
| Formats this time to a string using a specified format.

## Static Public Member Functions
static WTime | fromString (const WString &s)
| Parses a string to a time using a default format.
static WTime | fromString (const WString &s, const WString &format)
| Parses a string to a time using a specified format.
static WTime | currentTime ()
| Reports the current client date.
static WTime | currentServerTime ()
| Reports the current server time.

## Detailed Description

A value class that defines a clock time.

A clock time represents the time of day (usually 0 to 24 hour), up to millisecond precision.

As of version %3.3.1, the time class itself will no longer limit times to the 24-hour range, but will allow any time (duration), including negative values.

See also
  WDate, WDateTime

## Constructor & Destructor Documentation

## ◆ WTime() [1/2]

Wt::WTime::WTime | ( |  | ) |

Construct a Null time.

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

See also
  isValid(), isNull()

## ◆ WTime() [2/2]

Wt::WTime::WTime | ( | int | h,
|  | int | m,
|  | int | s = 0,
|  | int | ms = 0 )

Construct a time given hour, minutes, seconds, and milliseconds.

`m` and `s` have range 0-59, and `ms` has range 0-999. A duration can be positive or negative depending on the sign of `h`.

When the time is invalid, isValid() is set to `false`.

## Member Function Documentation

## ◆ addMSecs()

WTime Wt::WTime::addMSecs | ( | int | ms | ) | const

Adds milliseconds.

Returns a time that is `ms` milliseconds later than this time. Negative values for `ms` will result in a time that is as many milliseconds earlier.

## ◆ addSecs()

WTime Wt::WTime::addSecs | ( | int | s | ) | const

Adds seconds.

Returns a time that is `s` seconds later than this time. Negative values for `s` will result in a time that is as many seconds earlier.

## ◆ currentServerTime()

static WTime Wt::WTime::currentServerTime | ( |  | ) |
| static

Reports the current server time.

This method returns the local time on the server.

See also
  WDateTime::currentDateTime(), WLocalDateTime::currentServerDateTime()

## ◆ currentTime()

static WTime Wt::WTime::currentTime | ( |  | ) |
| static

Reports the current client date.

This method uses browser information to retrieve the time that is configured in the client.

See also
  WLocalDateTime::currentDate()

## ◆ fromString() [1/2]

static WTime Wt::WTime::fromString | ( | const WString & | s | ) |
| static

Parses a string to a time using a default format.

The default format is "hh:mm:ss". For example, a time specified as:

"22:55:15"

will be parsed as a time that equals a time constructed as:

WTime d(22,55,15);
Wt::WTime
A value class that defines a clock time.
Definition WTime.h:30

When the time could not be parsed or is not valid, an invalid time is returned (for which isValid() returns false).

See also
  fromString(const WString& s, const WString& format), isValid()

## ◆ fromString() [2/2]

static WTime Wt::WTime::fromString | ( | const WString & | s,
|  | const WString & | format )
| static

Parses a string to a time using a specified format.

The `format` follows the same syntax as used by toString(const WString& format).

When the time could not be parsed or is not valid, an invalid time is returned (for which isValid() returns false).

See also
  toString(const WString&) const

## ◆ isNull()

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

Returns if this time is Null.

A null time is also invalid.

See also
  isValid(), WTime()

## ◆ msecsTo()

long Wt::WTime::msecsTo | ( | const WTime & | t | ) | const

Returns the difference between two time values (in milliseconds).

The result is negative if t is earlier than this.

## ◆ secsTo()

long Wt::WTime::secsTo | ( | const WTime & | t | ) | const

Returns the difference between two time values (in seconds).

The result is negative if t is earlier than this.

## ◆ setHMS()

bool Wt::WTime::setHMS | ( | int | h,
|  | int | m,
|  | int | s,
|  | int | ms = 0 )

Sets the time.

`m` and `s` have range 0-59, and `ms` has range 0-999.

When the time is invalid, isValid() is set to `false`.

## ◆ toString() [1/2]

WString Wt::WTime::toString | ( |  | ) | const

Formats this time to a string using a default format.

The default format is "hh:mm:ss".

## ◆ toString() [2/2]

WString Wt::WTime::toString | ( | const WString & | format | ) | const

Formats this time to a string using a specified format.

The `format` is a string in which the following contents has a special meaning.

Code | Meaning | Example (for 14:06:23.045)
h | The hour without leading zero (0-23 or 1-12 for AM/PM display) | 14 or 2
hh | The hour with leading zero (00-23 or 01-12 for AM/PM display) | 14 or 02
H | The hour without leading zero (0-23) | 14
HH | The hour with leading zero (00-23) | 14
+ followed by (h/hh/H/HH) | The sign of the hour (+/-) | +
m | The minutes without leading zero (0-59) | 6
mm | The minutes with leading zero (00-59) | 06
s | The seconds without leading zero (0-59) | 23
ss | The seconds with leading zero (00-59) | 23
z | The milliseconds without leading zero (0-999) | 45
zzz | The millisecons with leading zero (000-999) | 045
AP or A | use AM/PM display: affects h or hh display and is replaced itself by AM/PM | PM
ap or a | use am/pm display: affects h or hh display and is replaced itself by am/pm | pm
Z | the timezone in RFC 822 format (e.g. -0800) | +0000

Any other text is kept literally. String content between single quotes (') are not interpreted as special codes. LabelOption::Inside a string, a literal quote may be specifed using a double quote ('').

Examples of format and result:

Format | Result (for 22:53:13.078)
hh:mm:ss.zzz | 22:53:13.078
hh:mm:ss AP | 10:53:13 PM

See also
  fromString(const WString& value, const WString& format)