×
Namespaces

Variants
Actions

Archived:Displaying date in different formats using TTime

From Nokia Developer 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: lpvalente (11 Nov 2012)

Contents

Overview

This code snippet shows how to use the class TTime to format date 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.

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 TDateType
{
EDateShort=0,
EDateShortZero,
EDateUsual,
EDateUsualZero,
EDateWithoutYear,
EDateWithoutYearZero,
EDateTypeCount
};
 
/**
* From CEikAppUi, HandleCommandL.
* Takes care of command handling.
* @param aCommand Command to be handled.
*/

void HandleCommandL(TInt aCommand);
 
void showDateFormattingL(TTime aTime, TDateType 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 date formatting ***
case ECommand1:
{
TTime now;
now.HomeTime(); // get current date and time
 
TBuf<50> dateString;
 
// print date in numeric form with leading zeros, using default
// date ordering (European) and default date separator characters
// format: dd/mm/yyyy
_LIT(KOwnDateFormat,"%D%M%Y%/0%1%/1%2%/2%3%/3");
now.FormatL(dateString,KOwnDateFormat);
 
CEikonEnv::Static()->InfoWinL(_L("Own Date Format:"), dateString);
 
}
break;
 
//*** Shows how to use Avkon predefined date formatting ***
case ECommand2:
{
TTime now;
now.HomeTime(); // get current date and time
 
//loop through predefined Avkon date formats
for (TInt i=0; i<EDateTypeCount; i++)
{
showDateFormattingL(now, (TDateType)i);
}
 
}
break;
//...
 
default:
//Panic(EFormatExampleUi);
break;
}
}
 
 
/* This method shows the given date in different formats with InfoWin dialog.
Supported format types from avkon.rsg:
#define R_QTN_DATE_SHORT 0x8cc00db
#define R_QTN_DATE_SHORT_WITH_ZERO 0x8cc00dc
#define R_QTN_DATE_USUAL 0x8cc00dd
#define R_QTN_DATE_USUAL_WITH_ZERO 0x8cc00de
#define R_QTN_DATE_WITHOUT_YEAR 0x8cc00df
#define R_QTN_DATE_WITHOUT_YEAR_WITH_ZERO 0x8cc00e0
*/

 
void CFormatExampleAppUi::showDateFormattingL(TTime aTime, TDateType aFormatType)
{
HBufC* dateFormatString = NULL;
TBuf<50> dateString;
 
// read format string from Avkon resources by given format type
switch (aFormatType)
{
case EDateShort:
dateFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_DATE_SHORT);
break;
case EDateShortZero:
dateFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_DATE_SHORT_WITH_ZERO);
break;
case EDateUsual:
dateFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_DATE_USUAL);
break;
case EDateUsualZero:
dateFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_DATE_USUAL_WITH_ZERO);
break;
case EDateWithoutYear:
dateFormatString = CEikonEnv::Static()->
AllocReadResourceLC(R_QTN_DATE_WITHOUT_YEAR);
break;
case EDateWithoutYearZero:
dateFormatString = CEikonEnv::Static()->
AllocReadResourceLC (R_QTN_DATE_WITHOUT_YEAR_WITH_ZERO);
break;
default:
dateFormatString = CEikonEnv::Static()->
AllocReadResourceLC (R_QTN_DATE_SHORT);
}
 
// format the date string
aTime.FormatL(dateString, *dateFormatString);
CleanupStack::PopAndDestroy(); // dateFormatString
 
//show the date with a dialog
CEikonEnv::Static()->InfoWinL(_L("Date:"), dateString);
}


Postconditions

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

See also

Archived:Displaying time in different formats using TTime

This page was last modified on 11 November 2012, at 17:38.
82 page views in the last 30 days.
×