Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



Location: e32std.h
Link against: euser.lib

Class TTime

TTime

Support

Supported from 5.0

Description

Stores and manipulates the date and time. It represents a date and time as a number of microseconds since midnight, January 1st, 0 AD nominal Gregorian. BC dates are represented by negative TTime values. A TTime object may be constructed from a TInt64, a TDateTime a string literal, or by default, which initialises the time to an arbitrary value. To access human-readable time information, the TTime may be converted from a TInt64 into a TDateTime, which represents the date and time as seven numeric fields and provides functions to extract these fields. Alternatively, to display the time as text, the time may be formatted and placed into a descriptor using a variety of formatting commands and which may or may not honour the system's locale settings. The conversion between time and text may be performed the other way around, so that a descriptor can be parsed and converted into a TTime value.

In addition to setting and getting the date and time and converting between text and time, TTime provides functions to get intervals between times and standard comparison and arithmetic operators which enable time intervals to be added or subtracted to or from the time.

Defined in TTime:
DateTime(), DayNoInMonth(), DayNoInWeek(), DayNoInYear(), DaysFrom(), DaysInMonth(), FormatL(), HomeTime(), HoursFrom(), Int64(), MicroSecondsFrom(), MinutesFrom(), MonthsFrom(), Parse(), RoundUpToNextMinute(), SecondsFrom(), Set(), TTime(), UniversalTime(), WeekNoInYear(), YearsFrom(), operator!=(), operator+(), operator+=(), operator-(), operator-=(), operator<(), operator<=(), operator=(), operator==(), operator>(), operator>=()

See also:


Construction and destruction


TTime()

TTime();

Description

Constructs a TTime object. It is initialised to an arbitrary value.


TTime()

TTime(const TInt64& aTime);

Description

Constructs a TTime object from a 64-bit microsecond value.

Parameters

const TInt64& aTime

Microsecond value to which to initialise the TTime object.


TTime()

TTime(const TDesC& aString);

Description

Constructs a TTime object with a text string. The string consists of up to three components, any or all of which may be omitted: —

When all three components are present, the string should take the form:

YYYYMMDD:HHMMSS.MMMMMM

The conversion from text to time is carried out in the same manner as that used in TTime::Set(). For a more complete description of how the string may be composed, see the description of TTime::Set().

Note:

Parameters

const TDesC& aString

Date and time string to which to initialize the TTime object. The year cannot be negative, or a panic will occur.


TTime()

TTime(const TDateTime& aDateTime);

Description

Constructs a TTime object with the seven fields which comprise a date and time.

Parameters

const TDateTime& aDateTime

Date and time to which to initialise the TTime object.

[Top]


Assignment operations


operator=()

TTime& operator=(const TInt64& aTime);

Description

Assigns a value contained in a 64-bit integer to this TTime.

Parameters

const TInt64& aTime

Microsecond value which to assign to the TTime object.

Return value

TTime&

This TTime.


operator=()

TTime& operator=(const TDateTime& aDateTime);

Description

Assigns a TDateTime object to this TTime.

Parameters

const TDateTime& aDateTime

The date and time to assign to this TTime.

Return value

TTime&

This TTime.

[Top]


Setting the time


Set()

TInt Set(const TDesC& aString);

Description

Assigns a date and time contained in a descriptor to this TTime. The string consists of up to three components, any or all of which may be omitted: —

When all three components are present, the string should take the form:

YYYYMMDD:HHMMSS.MMMMMM

If omitted, the first component is set to January 1st 0 AD nominal Gregorian. If either the second or third components are omitted, they are set to zero.

Notes:

The month and day values are offset from zero.

The only situations in which either the colon or dot may be omitted are as follows: —

Parameters

const TDesC& aString

The date and time string to assign to this TTime. The year cannot be negative, or the function will return an error.

Return value

TInt

KErrNone if successful, KErrGeneral if the string is syntactically incorrect, for example if neither a colon nor a dot is present; or if any component of the date or time is given an invalid value. For a list of valid values for components of the date and time, see TDateTime::Set(). If an error is returned, the date and time will remain unchanged.

See also:


HomeTime()

void HomeTime();

Description

Sets the date and time of this TTime to the home time.


UniversalTime()

void UniversalTime();

Description

Sets the date and time of this TTime to the universal time.

[Top]


Conversions to fields


DateTime()

TDateTime DateTime() const;

Description

Converts a TTime object into a TDateTime object. This conversion should be carried out before the seven fields which comprise a date and time can be accessed.

Return value

TDateTime

The components of the time, indicating year, month, day, hour, minute, second, microsecond.

[Top]


Interval calculations


MicroSecondsFrom()

TTimeIntervalMicroSeconds MicroSecondsFrom(TTime aTime) const;

Description

Calculates the number of microseconds difference between a TTime object and this TTime.

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TTimeIntervalMicroSeconds

Difference in microseconds between the two times. If the time specified in the argument is later than this TTime, this value is negative.


SecondsFrom()

TInt SecondsFrom(TTime aTime,TTimeIntervalSeconds& aInterval) const;

Description

Calculates the number of seconds difference between a TTime object and this TTime. The difference may be positive or negative.

Parameters

TTime aTime

The time to compare with this TTime.

TTimeIntervalSeconds& aInterval

On return contains the difference in seconds between the two times. If the time specified in the first argument is later than this TTime, this argument returns a negative value.

Return value

TInt

Error code. If not zero, indicates that the calculated interval is too large for a 32-bit integer.


MinutesFrom()

TInt MinutesFrom(TTime aTime,TTimeIntervalMinutes& aInterval) const;

Description

Calculates the number of minutes difference between a TTime object and this TTime. The difference may be positive or negative.

Parameters

TTime aTime

The time to compare with this TTime.

TTimeIntervalMinutes& aInterval

On return contains the difference in minutes between the two times. If the time specified in the first argument is later than this TTime, this argument returns a negative value.

Return value

TInt

Error code. If not zero, indicates that the calculated interval is too large to be contained in a 32 bit integer.


HoursFrom()

TInt HoursFrom(TTime aTime,TTimeIntervalHours& aInterval) const;

Description

Calculates the number of hours difference between a TTime object and this TTime. The difference may be positive or negative.

Parameters

TTime aTime

The time to compare with this TTime.

TTimeIntervalHours& aInterval

On return contains the difference in hours between the two times. If the time specified in the first argument is later than this TTime, this argument returns a negative value.

Return value

TInt

Error code. If not zero, indicates that the calculated interval is too large to be contained in a 32 bit integer.


DaysFrom()

TTimeIntervalDays DaysFrom(TTime aTime) const;

Description

Calculates the number of days difference between a TTime object and this TTime. The difference may be positive or negative.

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TTimeIntervalDays

Difference in days between the two times. If the time specified in aTime is later than this TTime, this value will be negative.


MonthsFrom()

TTimeIntervalMonths MonthsFrom(TTime aTime) const;

Description

Calculates the number of months between a TTime and this TTime. The result may be positive or negative.

Notes:

The interval in months between two TTimes is calculated by incrementing it by one each time the same day number and time in the previous or following month has been reached. Exceptions to this rule occur when this TTime is on the last day of the month. In this case, the following rules apply: —

When comparing this TTime with a later time,

When comparing this TTime with an earlier time,

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TTimeIntervalMonths

Difference in months between the two times. If the time specified in the argument is later than this TTime, the interval is negative.

See also:


YearsFrom()

TTimeIntervalYears YearsFrom(TTime aTime) const;

Description

Calculates the number of years between a TTime and this TTime. The result may be positive or negative.

Notes:

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TTimeIntervalYears

Difference in years between the two times. If the time specified in the argument is later than this TTime, the interval is negative.

See also:

[Top]


Extracting date information


WeekNoInYear()

TInt WeekNoInYear() const;
TInt WeekNoInYear(TTime aStartDate) const;
TInt WeekNoInYear(TFirstWeekRule aRule) const;
TInt WeekNoInYear(TTime aStartDate,TFirstWeekRule aRule) const;

Description

Finds the number of the current week in the year. Variants are provided which allow the user to specify the date which is to be considered as the start of the year and to determine the rule governing which week is the first week in the year.

Parameters

TTime aStartDate

If specified, indicates the date which is to be considered the start of the year. Default is 1st January.

TFirstWeekRule aRule

Determines how the first week in the year is to be calculated. By default EFirstFourDayWeek.

Return value

TInt

Week number in the year.

See also:


DayNoInYear()

TInt DayNoInYear() const;
TInt DayNoInYear(TTime aStartDate) const;

Description

Gets the day number in the year. A variant exists allowing the user to set the date which is to be considered as the start of the year. If no start date is specified, the default is January 1st.

Parameters

TTime aStartDate

If specified, indicates the date which is to be considered the start of the year. Default is 1st January.

Return value

TInt

The day number in the year. The first day in the year is day one.


DayNoInMonth()

TInt DayNoInMonth() const;

Description

Gets the day number in the month.

Return value

TInt

The day number in the month. The first day in the month is numbered zero.


DayNoInWeek()

TDay DayNoInWeek() const;

Description

Gets the day number within the current week, from zero to six, honouring the setting specified in TLocale::SetStartOfWeek(). By default the first day in the week is Monday.

Return value

TDay

The number of the day within the week. The range is EMonday to ESunday.


DaysInMonth()

TInt DaysInMonth() const;

Description

Gets the number of days in the current month.

Return value

TInt

The number of days in the month

[Top]


Formatting the date and time


FormatL()

void FormatL(TDes& aDes,const TDesC& aFormat) const;

Description

Puts this TTime into a descriptor and formats it according to the format string specified in the second argument. Many of the formatting commands use the system's locale settings for the date and time, for instance the characters used to separate components of the date and time and the ordering of day, month and year. The list of formatting commands below is divided into two sections, the first of which lists the commands which operate without reference to the locale's date and time settings (see class TLocale) and the second table lists the commands which do use these settings.

The following formatting commands do not honour the locale-specific system settings:

The following formatting commands do honour the locale-specific system settings:

Notes:

The %1, %2, %3, %4 and %5 formatting commands are used in conjunction with %D, %M and %Y to format the date locale-dependently. When formatting the date locale-dependently, the order of the day, month and year components within the string is determined by the order of the %1 to %5 formatting commands, not that of %D, %M, %Y.

When formatting the date locale-independently (that is, %F has been specified in the format string), the %1 to %5 formatting commands are not required, and should be omitted. In this case, the order of the date components is determined by the order of the %D, %M, %Y format commands within aFormat.

Up to four date separators and up to four time separators can be used to separate the components of a date or time. When formatting a numerical date consisting of the day, month and year or a time containing hours, minutes and seconds, all four separators should always be specified in the format command string. Usually, the leading and trailing separators should not be displayed. In this case, the first and fourth separators should still be specified, but should be represented by a null character.

The date format follows the pattern: —

DateSeparator[0] DateComponent1 DateSeparator[1] DateComponent2 DateSeparator[2] DateComponent3 DateSeparator[3]

where the ordering of date components is determined by the locale's date format setting.

The time format follows the pattern: —

TimeSeparator[0] Hours TimeSeparator[1] Minutes TimeSeparator[2] Seconds TimeSeparator[3]

If the time includes a microseconds component, the third separator should occur after the microseconds, and the seconds and microseconds should be separated by the decimal separator. When formatting a two component time, the following rules apply:

A set of examples demonstrating how to format the date locale-dependently and independently, is given in the “Using” document for this class.

Parameters

TDes& aDes

Descriptor, which on return contains the formatted date/time string.

const TDesC& aFormat

Format string which determines the format of the date and time.

Leave codes

KErrOverflow

The date/time string is too long for the descriptor aDes.

KErrGeneral

A formatting error has occurred.

See also:

[Top]


Rounding the time


RoundUpToNextMinute()

void RoundUpToNextMinute();

Description

Rounds this TTime up to the next minute. Both the seconds and microseconds components are set to zero.

[Top]


Conversion from text to date/time


Parse()

TInt Parse(const TDesC& aDes,TInt aCenturyOffset=0);

Description

Parses a descriptor containing either or both a date and time, setting this TTime to the value of the parsed descriptor.

The descriptor may contain the date only, the time only, the date followed by the time, or the time followed by the date. When both the date and time are specified in the descriptor, they should be separated using one or more space characters.

Leading zeros and spaces preceding any time or date components are discarded.

Dates may be specified either with all three components (day, month and year), or with just two components; for example month and day. The date suffix ("st" "nd" "rd" or "th") may not be included in the descriptor.

The date and its components may take different forms: —

If a colon or a dot has been specified in TLocale as the date separator character, neither may be used as date separators in this function.

If specified, the time must include the hour, but both minutes and seconds, or seconds alone may be omitted.

The time and its components may take different forms: —

When a character can be interpreted as either a date or time separator character, this function will interpret it as a date separator.

Look out for cases in which wrongly interpreting the contents of a descriptor, based on the interpretation of separator characters, causes an error. For example, trying to interpret the following descriptor _L("5.6.1996") as a time is invalid and will return an error of –2 because 1,996 seconds is out of range.

Notes:

Parameters

const TDesC& aDes

Descriptor containing any combination of date and time as text.

TInt aCenturyOffset=0

Offset between zero (the default) and 99. Allows a flexible interpretation of the century for two digit year values. If less than zero, or greater than 99, a panic occurs.

Return value

TInt

If equal to or greater than zero, the function completed successfully. EDatePresent and/or ETimePresent indicate whether either or both of the date or time are present. If less than zero, an error code. KErrGeneral indicates that the time or date value is out of range, for example if the hour is greater than 23 or if the minute is greater than 59. KErrNotSupported indicates that a two field date has been entered. KErrArgument indicates that the descriptor was syntactically incorrect. If the function fails, this TTime will remain unchanged.

See also:

[Top]


Arithmetic operators


operator+()

TTime operator+(TTimeIntervalYears aYear) const;
TTime operator+(TTimeIntervalMonths aMonth) const;
TTime operator+(TTimeIntervalDays aDay) const;
TTime operator+(TTimeIntervalHours aHour) const;
TTime operator+(TTimeIntervalMinutes aMinute) const;
TTime operator+(TTimeIntervalSeconds aSecond) const;
TTime operator+(TTimeIntervalMicroSeconds aMicroSecond) const;
TTime operator+(TTimeIntervalMicroSeconds32 aMicroSecond) const;

Description

Adds a time interval in any time units to a TTime, returning the result as a TTime.

Notes:

When adding one month to the last day in the month, if the following month is shorter, the result is the last day in the following month. For example, when adding one month to 31st August, the result is the 30th September.

In a leap year, when adding one year to the 29th February, the result is 28th February in the following year.

All the above arguments except TTimeIntervalMicroSeconds are stored as a 32 bit signed integer. The maximum value which they can represent is 2147483647. An attempt to add more than this amount will produce incorrect results.

Return value

TTime

The new time in microseconds


operator+=()

TTime& operator+=(TTimeIntervalYears aYear);
TTime& operator+=(TTimeIntervalMonths aMonth);
TTime& operator+=(TTimeIntervalDays aDay);
TTime& operator+=(TTimeIntervalHours aHour);
TTime& operator+=(TTimeIntervalMinutes aMinute);
TTime& operator+=(TTimeIntervalSeconds aSecond);
TTime& operator+=(TTimeIntervalMicroSeconds aMicroSecond);
TTime& operator+=(TTimeIntervalMicroSeconds32 aMicroSecond);

Description

Adds an interval of time in any time units to a TTime, assigning the result to the TTime.

Return value

TTime&

On return contains the new date and time in microseconds.


operator-()

TTime operator-(TTimeIntervalYears aYear) const;
TTime operator-(TTimeIntervalMonths aMonth) const;
TTime operator-(TTimeIntervalDays aDay) const;
TTime operator-(TTimeIntervalHours aHour) const;
TTime operator-(TTimeIntervalMinutes aMinute) const;
TTime operator-(TTimeIntervalSeconds aSecond) const;
TTime operator-(TTimeIntervalMicroSeconds aMicroSecond) const;
TTime operator-(TTimeIntervalMicroSeconds32 aMicroSecond) const;

Description

Subtracts an interval of time in any units from a TTime, returning the result as a TTime.

Notes:

Return value

TTime

The new time following the subtraction of a time interval from a TTime.


operator-=()

TTime& operator-=(TTimeIntervalYears aYear);
TTime& operator-=(TTimeIntervalMonths aMonth);
TTime& operator-=(TTimeIntervalDays aDay);
TTime& operator-=(TTimeIntervalHours aHour);
TTime& operator-=(TTimeIntervalMinutes aMinute);
TTime& operator-=(TTimeIntervalSeconds aSecond);
TTime& operator-=(TTimeIntervalMicroSeconds aMicroSecond);
TTime& operator-=(TTimeIntervalMicroSeconds32 aMicroSecond);

Description

Subtracts an interval of time in any units from a TTime, assigning the result to the TTime.

Return value

TTime&

On return contains the new date and time in microseconds

[Top]


Comparison operators


operator==()

TBool operator==(TTime aTime) const;

Description

Tests whether two date/times are equal.

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TBool

True if the two TTimes are equal. False if not


operator!=()

TBool operator!=(TTime aTime) const;

Description

Tests whether two date/times are not equal.

Parameters

TTime aTime

The date/time to compare with this TTime.

Return value

TBool

True if the two TTimes are different. False if the same.


operator>=()

TBool operator>=(TTime aTime) const;

Description

Tests whether a date/time is after or is the same as another.

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TBool

True if the first TTime is after than or the same as the second. Otherwise false.


operator<=()

TBool operator<=(TTime aTime) const;

Description

Tests whether a date/time precedes or is the same as another.

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TBool

True if the first TTime precedes or is the same as the second. Otherwise false.


operator>()

TBool operator>(TTime aTime) const;

Description

Tests whether a date/time is after another.

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TBool

True if the first TTime is after the second. Otherwise false.


operator<()

TBool operator<(TTime aTime) const;

Description

Tests whether a date/time precedes another.

Parameters

TTime aTime

The time to compare with this TTime.

Return value

TBool

True if the first TTime precedes the second. Otherwise false.

[Top]


Integer representation


Int64()

const TInt64& Int64() const;

Description

Gets the 64 bit integer representation of a TTime.

Return value

TInt64&

The 64 bit integer representation of a TTime.