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:14, 6 June 2008.

CS001008 - Displaying time in different formats using TTime

From Forum Nokia Wiki


ID CS001008 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 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

CS001007 - Displaying date in different formats using TTime

Related Discussions
Thread Thread Starter Forum Replies Last Post
Regarding Data formats to be supported. srini890 General Messaging 1 2002-10-24 06:45
RAlarmServer is deprecated.. So, where is the new AlarmShared.lib?! michelasso Symbian Tools & SDKs 13 2007-01-22 11:36
Inserting date Time into table abrahamk General Symbian C++ 8 2006-04-04 08:29
为什么不能提出个别TTime的日期 alei9527 Symbian 9 2008-09-11 04:38
Date & Time Jeepy General Symbian C++ 4 2003-08-07 14:40
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX