tzservices/tzserver/Client/Source/tzid.cpp
changeset 0 2e3d3ce01487
child 81 676b6116ca93
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include <s32mem.h>
       
    18 #include <tzid.h>
       
    19 #include "tzidinternal.h"
       
    20 #include <tz.h>
       
    21 
       
    22 
       
    23 /**
       
    24 The maximum number of digits for a user-defined time zone (when it is 0x20000 -
       
    25 1).
       
    26 
       
    27 @internalComponent
       
    28 */
       
    29 const TInt KMaxTzIdDigits = 6;
       
    30 
       
    31 
       
    32 CTzId::CTzId()
       
    33 	{
       
    34 	}
       
    35 
       
    36 
       
    37 CTzId::CTzId(TUint aNumericId) :
       
    38 	iReferenceId(aNumericId)
       
    39 	{
       
    40 	}
       
    41 
       
    42 
       
    43 /**
       
    44 Time zone ID data externaliser.
       
    45 
       
    46 @param aStream A stream that will contain the serialised time zone ID.
       
    47 
       
    48 @internalComponent
       
    49 */
       
    50 EXPORT_C void CTzId::ExternalizeL(RWriteStream& aStream) const
       
    51 	{
       
    52 	aStream << iReferenceId;
       
    53 	TInt32 size	= 0;
       
    54 	if (iZoneId)
       
    55 		{
       
    56 		size = iZoneId->Length();
       
    57 		aStream << size;
       
    58 		if (size)
       
    59 			{
       
    60 			aStream.WriteL(iZoneId->Des(), size);
       
    61 			}
       
    62 		}
       
    63 	else
       
    64 		{
       
    65 		aStream << size;
       
    66 		}
       
    67 	}
       
    68 
       
    69 
       
    70 /**
       
    71 Serialised CTzId data internaliser.
       
    72 
       
    73 @param aStream a stream containing the serialised time zone Id
       
    74 
       
    75 @internalComponent
       
    76 */
       
    77 EXPORT_C void CTzId::InternalizeL(RReadStream& aStream)
       
    78 	{
       
    79 
       
    80 	aStream >> iReferenceId;
       
    81 
       
    82 	delete iZoneId;
       
    83 	iZoneId = NULL;
       
    84 
       
    85 	TInt32 size;
       
    86 	aStream >> size;
       
    87 
       
    88 	const TInt KMaxSize = KMaxTInt / 2;
       
    89 	if (size < 0 || size >= KMaxSize)
       
    90 	    {
       
    91 	    User::Leave( KErrArgument );
       
    92 	    }
       
    93 
       
    94     // Don't create the tz id if the size is 0 - it hasn't been set.
       
    95     if (size)
       
    96         {
       
    97     	iZoneId = HBufC8::NewL(size);
       
    98     	TPtr8 bufferPtr(iZoneId->Des() );
       
    99     	aStream.ReadL(bufferPtr, size);
       
   100         }
       
   101 	}
       
   102 
       
   103 
       
   104 /**
       
   105 Destructor.
       
   106 */
       
   107 EXPORT_C CTzId::~CTzId()
       
   108 	{
       
   109 	delete iZoneId;
       
   110 	}
       
   111 
       
   112 
       
   113 /**
       
   114 Time zone ID factory method.
       
   115 
       
   116 @param aStream a stream containing the serialised time zone Id
       
   117 
       
   118 @leave KErrNoMemory or some other system error code
       
   119 
       
   120 @return Pointer to CTzId; clients own the object.
       
   121 
       
   122 @internalComponent
       
   123 */
       
   124 EXPORT_C CTzId* CTzId::NewL(RReadStream& aStream)
       
   125 	{
       
   126 	CTzId* self = new(ELeave) CTzId();
       
   127 	CleanupStack::PushL(self);
       
   128 
       
   129 	self->InternalizeL(aStream);
       
   130 
       
   131 	CleanupStack::Pop(self);
       
   132 	return self;
       
   133 	}
       
   134 
       
   135 
       
   136 /**
       
   137 Time zone ID factory method.
       
   138 
       
   139 @param aNameIdentity A name recognised by the time zone database.
       
   140 
       
   141 @return A pointer to the time zone ID. Clients take ownership.
       
   142 */
       
   143 EXPORT_C CTzId* CTzId::NewL(const TDesC8& aNameIdentity)
       
   144 	{
       
   145 	CTzId* self = new(ELeave) CTzId();
       
   146 	CleanupStack::PushL(self);
       
   147 
       
   148 	self->iZoneId = aNameIdentity.AllocL();
       
   149 
       
   150 	CleanupStack::Pop(self);
       
   151 	return self;
       
   152 	}
       
   153 
       
   154 
       
   155 /**
       
   156 Time zone ID factory method.
       
   157 
       
   158 @param aNumericId A numeric reference ID, generated by the time zone compiler.
       
   159 
       
   160 @return A pointer to the time zone ID. Clients take ownership.
       
   161 
       
   162 @panic TzServer 8 aNumericId must not be zero.
       
   163 */
       
   164 EXPORT_C CTzId* CTzId::NewL(TUint aNumericId)
       
   165 	{
       
   166 	const TUint KUnacceptableTzId = 0;
       
   167 
       
   168 	// aNumericId must not be zero 
       
   169 	__ASSERT_ALWAYS((aNumericId != KUnacceptableTzId), RTz::Panic(RTz::EPanicUnsupportedTimeZoneNoId));
       
   170 	CTzId* self = new(ELeave) CTzId(aNumericId);
       
   171 	CleanupStack::PushL(self);
       
   172 	self->ConstructL();	
       
   173 	CleanupStack::Pop(self);
       
   174 	return self;
       
   175 	}
       
   176 
       
   177 	
       
   178 void CTzId::ConstructL()
       
   179 	{
       
   180 	if (IsUserTzId())
       
   181 		{
       
   182 		TBuf8<KMaxTzIdDigits> name;
       
   183    		name.Num(iReferenceId); 
       
   184 		SetIdL(name);
       
   185 		}
       
   186 	}
       
   187 
       
   188 
       
   189 /**
       
   190 Replicates a CTzId.
       
   191 
       
   192 @return A pointer to CTzId; clients own the object.
       
   193 
       
   194 @leave KErrNoMemory or some other system error code.
       
   195 
       
   196 @internalComponent
       
   197 */
       
   198 EXPORT_C CTzId* CTzId::CloneL() const
       
   199 	{
       
   200 	CTzId* clone = NULL;
       
   201 	if (iZoneId)
       
   202 		{
       
   203 		clone = NewL(*iZoneId);
       
   204 		clone->iReferenceId = iReferenceId;
       
   205 		}
       
   206 	else
       
   207 		{
       
   208 		clone = new(ELeave) CTzId(iReferenceId);
       
   209 		}
       
   210 	return clone;
       
   211 	}
       
   212 
       
   213 
       
   214 /**
       
   215 Retrieves the time zone's name.
       
   216 
       
   217 This can be used after calling RTz::GetTimeZoneIdL().
       
   218 
       
   219 @return The time zone's name, if one has been set. A NULL descriptor if not.
       
   220 */
       
   221 EXPORT_C const TDesC8& CTzId::TimeZoneNameID() const
       
   222 	{
       
   223 	if (NULL == iZoneId)
       
   224 		{
       
   225 		return KNullDesC8();
       
   226 		}
       
   227 
       
   228 	return *iZoneId;
       
   229 	}
       
   230 
       
   231 
       
   232 /**
       
   233 Retrieves the time zone's numeric ID.
       
   234 
       
   235 This can be used after calling RTz::GetTimeZoneIdL().
       
   236 
       
   237 @return The time zone's numeric ID, generated by the time zone compiler.
       
   238 */
       
   239 EXPORT_C TUint CTzId::TimeZoneNumericID() const
       
   240 	{
       
   241 	return iReferenceId;
       
   242 	}
       
   243 
       
   244 
       
   245 /**
       
   246 Sets the numeric time zone ID.
       
   247 
       
   248 @param aNumericId The new numeric ID, generated by the time zone compiler.
       
   249 
       
   250 @internalComponent
       
   251 */
       
   252 EXPORT_C  void CTzId::SetId(TUint aNumericId)
       
   253 	{
       
   254 	iReferenceId = aNumericId;
       
   255 	}
       
   256 
       
   257 
       
   258 /**
       
   259 Sets the the time zone's name.
       
   260 
       
   261 @param aNameIdentity string TZID as used in the Olson's TZ Database
       
   262 
       
   263 @internalComponent
       
   264 */
       
   265 EXPORT_C void CTzId::SetIdL(const TDesC8& aNameIdentity)
       
   266 	{
       
   267 	delete iZoneId;
       
   268 	iZoneId = NULL;
       
   269 
       
   270 	iZoneId = aNameIdentity.AllocL();
       
   271 	}
       
   272 
       
   273 
       
   274 /** 
       
   275 Equality operator.
       
   276 
       
   277 @param aTZId The ID of another time zone.
       
   278 
       
   279 @return ETrue if the specified time zone ID is the same as this one, otherwise
       
   280 EFalse.
       
   281 */
       
   282 EXPORT_C TBool CTzId::operator==(const CTzId& aTZId) const
       
   283 	{
       
   284 	if ((aTZId.iReferenceId != 0) && (iReferenceId != 0))
       
   285 		{
       
   286 		return (aTZId.iReferenceId == iReferenceId);
       
   287 		}
       
   288 	else
       
   289 		{
       
   290 		if (iZoneId == NULL)
       
   291 			{
       
   292 			return EFalse;
       
   293 			}
       
   294 
       
   295 		if (aTZId.iZoneId == NULL)
       
   296 			{
       
   297 			return EFalse;
       
   298 			}
       
   299 
       
   300 		return (iZoneId->Compare(*(aTZId.iZoneId)) == 0);
       
   301 		}
       
   302 	}
       
   303 
       
   304 
       
   305 /**
       
   306 Determines if this time zone identifier has a numeric identifier value that
       
   307 belongs to the range of values used to identify user-defined time zones.
       
   308 
       
   309 @return Returns ETrue if this time zone identifier has a numeric identifier
       
   310 value that belongs to the range of values used to identify user-defined time
       
   311 zones and EFalse otherwise.
       
   312 */
       
   313 EXPORT_C TBool CTzId::IsUserTzId() const
       
   314 	{
       
   315 	// Check if just the string identifier has been set.  If so, we will need
       
   316 	// to attempt to extract a number from the string identifier and use this
       
   317 	// as the numeric identifier.
       
   318 	if (iReferenceId == 0 && iZoneId && *iZoneId != KNullDesC8)
       
   319 		{
       
   320 		TUint intId;
       
   321 		TLex8 lex(*iZoneId );
       
   322 		if(lex.Val(intId) == KErrNone)
       
   323 			{
       
   324 			CTzId* self = const_cast<CTzId*>(this);
       
   325 			self->SetId(intId);
       
   326 			}
       
   327 		}
       
   328 
       
   329 	return IsUserTzId(iReferenceId);
       
   330 	}
       
   331 
       
   332 
       
   333 /**
       
   334 Determines if the given numeric time zone identifier value belongs to the range
       
   335 of values used to identify user-defined time zones.
       
   336 
       
   337 @param aTzId Numeric time zone identifier value.
       
   338 
       
   339 @return Returns ETrue if the given numeric time zone identifier value belongs to
       
   340 the range of values used to identify user-defined time zones and EFalse
       
   341 otherwise.
       
   342 */
       
   343 EXPORT_C TBool CTzId::IsUserTzId(TUint aTzId)
       
   344 	{
       
   345 	return (aTzId >= KUserTzIdMin && aTzId <= KUserTzIdMax);
       
   346 	}