Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:Displaying time in different formats using TTime

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): Nokia N93
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition FP1
Article
Keywords: TTime, CEikonEnv, CCoeEnv, TTime::HomeTime(), TTime::FormatL(), CEikonEnv::InfoWinL(), CCoeEnv::AllocReadResourceLC()
Created: aknyman (30 May 2008)
Last edited: hamishwillee (13 Jun 2012)

Contents

Overview

This code snippet shows how to use the class TTime to format time strings. The class TTime is used to store date and time and has the method FormatL() to format date and time into a user-defined format. The Avkon UI library also offers some predefined formatting strings that can be used in the application by reading strings from the Avkon resource file.

Note: The Avkon library contains predefined format strings to display time durations and they can be used the same way than the time resource strings (R_QTN_TIME_DURAT_SHORT, R_QTN_TIME_DURAT_SHORT_WITH_ZERO, R_QTN_TIME_DURAT_LONG, R_QTN_TIME_DURAT_LONG_WITH_ZERO, R_QTN_TIME_DURAT_MIN_SEC, R_QTN_TIME_DURAT_MIN_SEC_WITH_ZERO)

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY avkon.lib   //Avkon resources
LIBRARY euser.lib //TTime, HBufC
LIBRARY eikcore.lib //InfoWinL, CEikonEnv
LIBRARY cone.lib //CCoeEnv


Header file

#ifndef __FORMATEXAMPLEAPPUI_H__
#define __FORMATEXAMPLEAPPUI_H__
 
//...
 
class CFormatExampleAppUi : public CAknAppUi
{
//...
private:
enum TTimeType
{
ETimeUsual=0,
ETimeUsualZero,
ETimeLong,
ETimeLongZero,
ETimeTypeCount
};
 
/**
* From CEikAppUi, HandleCommandL.
* Takes care of command handling.
* @param aCommand Command to be handled.
*/

void HandleCommandL(TInt aCommand);
 
void showTimeFormattingL(TTime aTime, TTimeType aFormatType);
 
//...
};
 
#endif // __FORMATEXAMPLEAPPUI_H__


Source file

#include <avkon.rsg>
 
void CFormatExampleAppUi::HandleCommandL(TInt aCommand)
{
switch (aCommand)
{
case EEikCmdExit:
case EAknSoftkeyExit:
Exit();
break;
 
//*** Shows how to use user-defined time formatting ***
case ECommand1:
{
TTime now;
now.HomeTime(); // get current date and time
 
TBuf<50> timeString;
 
// print time using default time separator characters in 24 hour clock format
// format: hour:minute:second.microsecond
_LIT(KOwnTimeFormat,"%:0%H%:1%T%:2%S.%*C3%:3");
now.FormatL(timeString,KOwnTimeFormat);
 
CEikonEnv::Static()->InfoWinL(_L("Own Time Format:"), timeString);
 
}
break;
 
//*** Shows how to use Avkon predefined time formatting ***
case ECommand2:
{
TTime now;
now.HomeTime(); // get current date and time
 
//loop through predefined Avkon time formats
for (TInt i=0; i<ETimeTypeCount; i++)
{
showTimeFormattingL(now, (TTimeType)i);
}
 
}
break;
//...
 
default:
//Panic(EFormatExampleUi);
break;
}
}
 
 
/* This method shows the given time in different formats with InfoWin dialog.
Supported format types from avkon.rsg:
#define R_QTN_TIME_USUAL 0x8cc00d7
#define R_QTN_TIME_USUAL_WITH_ZERO 0x8cc00d8
#define R_QTN_TIME_LONG 0x8cc00d9
#define R_QTN_TIME_LONG_WITH_ZERO 0x8cc00da
*/

void CFormatExampleAppUi::showTimeFormattingL(TTime aTime, TTimeType aFormatType)
{
HBufC* timeFormatString = NULL;
TBuf<50> timeString;
 
// read format string from Avkon resources by given format type
switch (aFormatType)
{
case ETimeUsual:
timeFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_TIME_USUAL);
break;
case ETimeUsualZero:
timeFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_TIME_USUAL_WITH_ZERO);
break;
case ETimeLong:
timeFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_TIME_LONG);
break;
case ETimeLongZero:
timeFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_TIME_LONG_WITH_ZERO);
break;
 
default:
timeFormatString = CEikonEnv::Static()->
AllocReadResourceLC (R_QTN_TIME_USUAL);
}
 
// format the time string
aTime.FormatL(timeString, *timeFormatString);
CleanupStack::PopAndDestroy(); // timeFormatString
 
//show the time with a dialog
CEikonEnv::Static()->InfoWinL(_L("Time:"), timeString);
}


Postconditions

The current time is displayed with dialogs using the user-defined time format and predefined Avkon library time formats.

See also

Archived:Displaying date in different formats using TTime

This page was last modified on 13 June 2012, at 06:14.
48 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×