examples/AppServices/timezonelocalization/TZLocExample.cpp

00001 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // This examples demonstrates using the TimeZone Localization API 
00015 // (CTzLocalizer and associated classes) to retrieve a list of time zones 
00016 // and to get information about a particular time zone. It adds a city 
00017 // to a time zone/city group.
00018 //
00019 
00020 #include <e32base.h>
00021 #include <e32cons.h>
00022 #include <tzlocalizer.h> 
00023 #include <tzlocalizationdatatypes.h>
00024 
00025 // local definitions 
00026 static CConsoleBase* console;
00027 static CTzLocalizer* localizer;
00028 static TInt timeZoneId;
00029 static TInt cityGroupId;
00030 
00031 // strings for display
00032 _LIT(KConsoleNewLine,"\n");
00033 _LIT(KMsgPressAnyKey,"\n\nPress any key to continue\n");
00034 
00035 // Sort and print out all time zones
00036 static void GetAllTimeZonesL()
00037         {
00038         // Use standard names in alphabetical order
00039         CTzLocalizedTimeZoneArray* timeZones = localizer->GetAllTimeZonesL(CTzLocalizer::ETzAlphaStandardNameAscending);
00040 
00041         TInt count= timeZones->Count();
00042         _LIT(KConsoleMessage,"\nAll timezones:");       
00043         console->Printf(KConsoleMessage);       
00044         for(TInt i=0; i<count; ++i)
00045                 {
00046                 TPtrC temp(timeZones->At(i).StandardName());
00047                 // if the time zone has a standard name, print it and the short name
00048                 if ((&temp)->Length()!=0)
00049                         {
00050                         console->Printf(KConsoleNewLine);
00051                         console->Printf(temp);
00052                         _LIT(KConsoleTab,"\t");
00053                         console->Printf(KConsoleTab);
00054                         console->Printf(timeZones->At(i).ShortStandardName());
00055                         }
00056                 } 
00057                 
00058         delete timeZones;
00059         console->Printf(KMsgPressAnyKey);
00060         console->Getch();
00061         }
00062 
00063 // Add a city to a specific time zone and city group
00064 static void AddCityL()
00065         {
00066         // Add the new city to the same time zone and city group as London
00067         _LIT(KCityName,"London");
00068         // First, find the time zone and city group that London is in 
00069         CTzLocalizedCity* city=localizer->FindCityByNameL(KCityName);
00070         if (!city)
00071         // city doesn't exist
00072                 User::Leave(KErrNotFound);
00073         
00074         // Get the city's time zone ID and city group ID
00075         timeZoneId = city->TimeZoneId();
00076         cityGroupId = city->GroupId();
00077         delete city;
00078 
00079         // Now add the new city to that time zone/city group.
00080         // First check it is not already present
00081         CTzLocalizedCityArray* cities=localizer->GetCitiesL(timeZoneId);
00082         CleanupStack::PushL(cities);
00083         _LIT(KNewCityName,"Cambridge");
00084         CTzLocalizedCity* temp=NULL;
00085         // (AddCityL() leaves if the city has already been added, so trap the leave)
00086         TRAPD(err,temp=localizer->AddCityL(timeZoneId,KNewCityName,cityGroupId));
00087         // Ignore return value
00088         if (err==KErrNone)
00089                         delete temp;
00090         CleanupStack::PopAndDestroy(cities);
00091         }
00092         
00093 // print out all the cities in the time zone
00094 static void GetCitiesInTimeZoneL()
00095         {
00096         CTzLocalizedCityArray* tzCities=localizer->GetCitiesL(timeZoneId);
00097         CleanupStack::PushL(tzCities);
00098         // get the standard name of the time zone and print it
00099         CTzLocalizedTimeZone* tz = localizer->GetLocalizedTimeZoneL(timeZoneId);
00100         _LIT(KConsoleMessage,"\nThe cities in the %S time zone are :");
00101         TPtrC tzName = tz->StandardName();
00102         console->Printf(KConsoleMessage,&tzName);
00103         delete tz;
00104         
00105         TInt tzCount=tzCities->Count();
00106         for(TInt i=0; i<tzCount; ++i)
00107                 {
00108                 console->Printf(KConsoleNewLine);
00109                 console->Printf(tzCities->At(i).Name());
00110                 }
00111 
00112         CleanupStack::PopAndDestroy(tzCities);
00113         console->Printf(KMsgPressAnyKey);
00114         console->Getch();
00115         }
00116         
00117 // print out all the cities in the city group
00118 static void GetCitiesInGroupL()
00119         {
00120         CTzLocalizedCityArray* groupCities = localizer->GetCitiesInGroupL(cityGroupId);
00121         CleanupStack::PushL(groupCities);
00122         
00123         // get the city group name
00124         CTzLocalizedCityGroup* cityGroup=localizer->GetCityGroupL(cityGroupId); 
00125         _LIT(KConsoleMessage,"\nThe cities in the %S city group are :");        
00126         TPtrC groupName = cityGroup->Name();
00127         console->Printf(KConsoleMessage,&groupName);
00128         delete cityGroup;
00129         
00130         TInt groupCount=groupCities->Count();
00131         for(TInt i=0; i<groupCount; ++i)
00132                         {
00133                         console->Printf(KConsoleNewLine);
00134                         console->Printf(groupCities->At(i).Name());
00135                         }
00136         CleanupStack::PopAndDestroy(groupCities); 
00137         }
00138         
00139 
00140 static void LocalizeTimeZonesL()
00141         {
00142         // Create an instance of CTzLocalizer
00143         localizer = CTzLocalizer::NewL();
00144         CleanupStack::PushL(localizer);
00145         GetAllTimeZonesL(); 
00146         AddCityL();
00147         GetCitiesInTimeZoneL();
00148         GetCitiesInGroupL();
00149         CleanupStack::PopAndDestroy(localizer);
00150         }
00151         
00152 
00153 static void DoExampleL()
00154     {
00155         // Create the console to print the messages to. 
00156         _LIT(KConsoleMessageDisplay, "Time zone localisation example");
00157         _LIT(KConsoleStars,"\n*************************\n");
00158         console = Console::NewL(KConsoleMessageDisplay,TSize(KConsFullScreen,KConsFullScreen));
00159         CleanupStack::PushL(console);
00160         console->Printf(KConsoleMessageDisplay);
00161         console->Printf(KConsoleStars);
00162 
00163         TRAPD(err,LocalizeTimeZonesL());
00164         if (err)
00165                 {
00166                 _LIT(KFailed,"\n\nExample failed: leave code=%d");
00167                 console->Printf(KFailed, err);
00168                 }
00169         // wait for user to press a key before destroying console
00170         console->Printf(KMsgPressAnyKey);
00171         console->Getch();
00172     CleanupStack::PopAndDestroy(console);
00173     }
00174 
00175 // Standard entry point function
00176  TInt E32Main()
00177         {
00178         __UHEAP_MARK;
00179         // Active scheduler required as this is a console app
00180         CActiveScheduler* scheduler=new CActiveScheduler;
00181         // If active scheduler has been created, install it.
00182         if (scheduler)
00183                 {
00184                 CActiveScheduler::Install(scheduler); 
00185                 // Cleanup stack needed
00186                 CTrapCleanup* cleanup=CTrapCleanup::New();
00187                 if (cleanup)
00188                         {
00189                     TRAP_IGNORE(DoExampleL());
00190                         delete cleanup;
00191                         }
00192                 delete scheduler;
00193                 }
00194         __UHEAP_MARKEND;
00195         return KErrNone;
00196     }
00197   
00198 
00199 

Generated on Thu Jan 21 10:32:55 2010 for TB10.1 Example Applications by  doxygen 1.5.3