Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 10:12, 6 June 2008.

CS001007 - Displaying date in different formats using TTime

From Forum Nokia Wiki


ID CS001007 Creation date June 6, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia N93
Category Symbian C++ Subcategory Files/Data


Keywords (APIs, classes, methods, functions): TTime, CEikonEnv, CCoeEnv, TTime::HomeTime(), TTime::FormatL(), CEikonEnv::InfoWinL(), CCoeEnv::AllocReadResourceLC()

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

CS001008 - Displaying time in different formats using TTime

Related Discussions
Thread Thread Starter Forum Replies Last Post
execution time mahendra_2010 Carbide.c++ and CodeWarrior Tools 2 2008-09-11 12:36
一个奇怪的问题! jintian2005 Symbian 6 2008-05-30 08:42
Convert From TBuf To TTime Hadad.Net General Symbian C++ 2 2007-10-16 00:25
Create a filename with timestamp as filename kambizs General Symbian C++ 6 2008-07-31 11:11
how to get time in Series 60? ayannurkenov Symbian Tools & SDKs 2 2004-03-31 11:11
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX