cms/src/TCMSTimeUtil.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    "TCMSTimeUtil.h"
       
    21 #include 	<asn1dec.h>
       
    22 
       
    23 // CONSTANTS
       
    24 // UTC formatting string
       
    25 _LIT( KUTCFormatString, "%*Y%3%M%2%D%1%H%T%SZ" );
       
    26 const TUint8 KFormatLength = 13;
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // TCMSTimeUtil::ConvertToEncoderLC
       
    32 // Converts TTime to ASN encoder base
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CASN1EncBase* TCMSTimeUtil::ConvertToEncoderLC( const TTime& aTime )
       
    36     {
       
    37 	CASN1EncBase* time = NULL;
       
    38 	//    Dates between 1 January 1950 and 31 December 2049 (inclusive) must be
       
    39 	//    encoded as UTCTime.  Any dates with year values before 1950 or after
       
    40 	//    2049 must be encoded as GeneralizedTime.
       
    41 	if( aTime.DateTime().Year() >= 1950 &&
       
    42 		aTime.DateTime().Year() <= 2049 )
       
    43 		{
       
    44 		TBuf<KFormatLength> dateString;
       
    45 		// Formatting for UTC time
       
    46 		aTime.FormatL( dateString, KUTCFormatString );
       
    47 		TBuf8<KFormatLength> timeDes;
       
    48 		timeDes.Copy( dateString );
       
    49 		time = CASN1EncOctetString::NewLC( timeDes );
       
    50 		// change type to UTCTime
       
    51 		time->SetTag( EASN1UTCTime, EUniversal );
       
    52 		}
       
    53 	else
       
    54 		{
       
    55 		time = CASN1EncGeneralizedTime::NewLC( aTime );
       
    56 		}
       
    57 	return time;
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // TCMSTimeUtil::ConvertToTimeL
       
    62 // Converts DER encoded source to TTime
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 TTime TCMSTimeUtil::ConvertToTimeL( const TDesC8& aSource )
       
    66 	{
       
    67 	TASN1DecGeneric time( aSource );
       
    68 	time.InitL();
       
    69 	TTime returnValue = 0;
       
    70 	if( time.Tag() == EASN1GeneralizedTime )
       
    71 		{
       
    72 		TASN1DecGeneralizedTime genTime;
       
    73 		returnValue = genTime.DecodeDERL( time );
       
    74 		}
       
    75 	else if( time.Tag() == EASN1UTCTime )
       
    76 		{
       
    77 		TASN1DecUTCTime utcTime;
       
    78 		returnValue = utcTime.DecodeDERL( time );
       
    79 		}
       
    80 	else
       
    81 		{
       
    82 		User::Leave( KErrArgument );
       
    83 		}
       
    84 	return returnValue;
       
    85 	}
       
    86 //  End of File