First public contribution.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * This file contains the implementation of the General Time ASN1 class.
16 * Fully support three forms of ASN.1 generalizedTime
17 * Fraction of second accepts 1,2 or 3 digits input. This is to maximize the tolerance of client input
24 EXPORT_C TASN1DecGeneralizedTime::TASN1DecGeneralizedTime()
28 TTime TASN1DecGeneralizedTime::GetTimeL(const TDesC8& aSource)
33 // __OFFSET__ Extract corrected time to include offset too.
35 // Did this checking ought to be done on creation rather than when we attempt to get the result?
37 // I guess we ought to check that the contents we've got are long enough to contain a time!
38 if (aSource.Length()<13)
40 User::Leave(KErrArgument);
43 // Check all digits the main bit of time are valid, YYYY:MM:DD HH:MM
48 j=(TUint8)(aSource[i]-'0');
51 User::Leave(KErrArgument);
54 //check seconds are valid if present
56 if(aSource.Length() > 13)
58 if ((aSource[i] < '0') || (aSource[i] > '9')
59 || (aSource[i+1] < '0') || (aSource[i+1] > '9'))
60 User::Leave(KErrArgument);
64 //if the time string is longer
65 if(aSource.Length() > 14)
68 TInt countOperator = 0;
71 //check if the extra bytes are valid
72 while(i < aSource.Length())
76 case '.': //'.' can't appear more than once and at least one digit should follow
79 User::Leave(KErrArgument);
81 if(aSource.Length()-1 == i) //error, '.' is last char in time string
82 User::Leave(KErrArgument);
85 if(aSource[i+1] - '0' < 0 || aSource[i+1] - '0' > 9)
86 User::Leave(KErrArgument);
91 case '-': // '+' and '-' can't appear more than once and at least four digits should follow
92 // After that, we reach either EOF or 'Z', no more char could follow
95 User::Leave(KErrArgument);
97 if(aSource.Length()-1 < i + 4) //error, not enough digits following operator
98 User::Leave(KErrArgument);
101 for(TInt j = i+1; j <= i+4; j++)
102 if(aSource[j] - '0' < 0 || aSource[j] -'0' > 9)
103 User::Leave(KErrArgument);
106 if(aSource.Length() > i + 6)//error, unexpected rubbish
107 User::Leave(KErrArgument);
108 if(aSource.Length() == i + 6 && aSource[i+5] != 'Z')
109 User::Leave(KErrArgument);
114 case 'Z': //'Z' can't appear more than once and must be the last char in time string
117 User::Leave(KErrArgument);
119 if(aSource.Length()-1 > i) //error, something following 'Z'
120 User::Leave(KErrArgument);
124 default: //so is this a digit?
125 if(aSource[i] -'0' < 0 || aSource[i] -'0' > 9)
126 User::Leave(KErrArgument);
133 // Uh-oh, looks like we're going to have to pull each bit manually and pop it in a TDateTime thing
134 TInt Year,Month,Day,Hour,Minute,Second=0; // Only set seconds to zero 'cos they are optional, everything else is going to be set
135 TInt MicroSecond = 0;
136 TInt offsetIntHours = 0, offsetIntMinutes = 0;
137 TTimeIntervalHours offsetHours = 0;
138 TTimeIntervalMinutes offsetMinutes = 0;
139 Year=(aSource[0]-'0')*1000+(aSource[1]-'0')*100+(aSource[2]-'0')*10+(aSource[3]-'0');
140 Month=(aSource[4]-'0')*10+aSource[5]-'0';
141 Month--; // Because the enum for months starts at 0 not 1
142 Day=(aSource[6]-'0')*10+aSource[7]-'0';
143 Day--; // Because the enum for days starts at 0 not 1
144 Hour=(aSource[8]-'0')*10+aSource[9]-'0';
145 Minute=(aSource[10]-'0')*10+aSource[11]-'0';
149 if (aSource.Length()>13)
152 Second = (aSource[Pos++]-'0')*10;
153 Second += aSource[Pos++]-'0';
157 // check range for various components
158 if (Month<EJanuary || Month>EDecember)
160 User::Leave(KErrArgument);
163 TMonth month = (TMonth)Month;
165 if ( (Day<0 || Day>=Time::DaysInMonth(Year,month)) ||
166 (Hour<0 || Hour>=24) ||
167 (Minute<0 || Minute>=60) ||
168 (Second<0 || Second>=60) )
170 User::Leave(KErrArgument);
173 //optional 3 bytes fraction of seconds
174 //Converts the fraction of seconds to microsecond
175 if(aSource.Length() > Pos && aSource[Pos] == '.')
179 TInt multiplier = 100000;
181 // parse fraction of second
182 while(Pos <= 17 && Pos < aSource.Length())
184 if(!((aSource[Pos] >= '0') && (aSource[Pos] <= '9')))
185 break; //fraction ended
187 MicroSecond += (aSource[Pos++]-'0') * multiplier;
194 if (aSource.Length()>Pos)
196 TBool aheadUTC = EFalse;
197 switch (aSource[Pos])
200 // Zulu - nothing more to do
203 //'+' indicates local time is ahead of UTC, need to substract the offset later
205 // The case fall-through is intentional, both cases have the same functionality, with a different
208 {//brackets are required to prevent armv5 warning and gccxml errors
211 //'-' indicates local time is behind of UTC, need to add the offset later
212 //if we are in here with '-', aheadUTC has already been set to the correct value
218 TInt multiplier = 10;
219 for(TInt i = 0; i <= 3; i++)
222 offsetIntHours += (aSource[Pos + i]-'0') * multiplier;
224 offsetIntMinutes += (aSource[Pos + i]-'0') * (TInt)multiplier;
226 multiplier = (multiplier == 10)? 1 : 10;
233 //offset hour range: 0-23
234 //offset minute range: 0-59
235 if(offsetIntHours < 0 || offsetIntHours > 23 ||
236 offsetIntMinutes < 0 || offsetIntMinutes > 59)
237 User::Leave(KErrArgument);
239 offsetHours = aheadUTC ? -offsetIntHours : offsetIntHours ;
240 offsetMinutes = aheadUTC ? -offsetIntMinutes : offsetIntMinutes;
246 User::Leave(KErrArgument);
253 TDateTime D(Year, month,Day,Hour,Minute,Second,MicroSecond);
257 T = T + offsetMinutes;
261 EXPORT_C TTime TASN1DecGeneralizedTime::DecodeDERL(const TDesC8& aSource,TInt& aPos)
264 TPtrC8 Source=aSource.Mid(aPos);
265 TASN1DecGeneric gen(Source);
267 TTime t = GetTimeL(gen.GetContentDER());
268 aPos+=gen.LengthDER();
272 EXPORT_C TTime TASN1DecGeneralizedTime::DecodeDERL(const TASN1DecGeneric& aGen)
275 return GetTimeL(aGen.GetContentDER());