sl@0
|
1 |
// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32test\datetime\t_dparse.cpp
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#include <e32test.h>
|
sl@0
|
19 |
|
sl@0
|
20 |
RTest test(_L("T_DPARSE"));
|
sl@0
|
21 |
TInt offset=0;
|
sl@0
|
22 |
|
sl@0
|
23 |
LOCAL_C TInt DateTimeParse(TDateTime& aDateTime,const TDesC& aDes,TInt aCenturyOffset=0)
|
sl@0
|
24 |
{
|
sl@0
|
25 |
TTime time;
|
sl@0
|
26 |
TInt r=time.Parse(aDes,aCenturyOffset);
|
sl@0
|
27 |
if (r>=0)
|
sl@0
|
28 |
aDateTime=time.DateTime();
|
sl@0
|
29 |
return r;
|
sl@0
|
30 |
}
|
sl@0
|
31 |
|
sl@0
|
32 |
LOCAL_D TInt ConvertDesToDateTimeInteractively()
|
sl@0
|
33 |
{
|
sl@0
|
34 |
TKeyCode code;
|
sl@0
|
35 |
TBuf<32> buf;
|
sl@0
|
36 |
buf.SetLength(0);
|
sl@0
|
37 |
FOREVER
|
sl@0
|
38 |
{
|
sl@0
|
39 |
code = test.Console()->Getch();
|
sl@0
|
40 |
if (code==EKeyEnter || code==EKeyLineFeed)
|
sl@0
|
41 |
break;
|
sl@0
|
42 |
if (code==EKeyEscape)
|
sl@0
|
43 |
return KErrGeneral;
|
sl@0
|
44 |
if ((code==EKeyDelete || code==EKeyBackspace) && buf.Length()>0)
|
sl@0
|
45 |
{
|
sl@0
|
46 |
TChar del(code);
|
sl@0
|
47 |
TBuf<1> delBuf;
|
sl@0
|
48 |
delBuf.Append(del);
|
sl@0
|
49 |
test.Printf(delBuf);
|
sl@0
|
50 |
buf.SetLength(buf.Length()-1);
|
sl@0
|
51 |
continue;
|
sl@0
|
52 |
}
|
sl@0
|
53 |
if (buf.Length()==0 && code==EKeyF1)
|
sl@0
|
54 |
{
|
sl@0
|
55 |
test.Printf(_L("Enter a two digit value. "));
|
sl@0
|
56 |
TKeyCode code1 = test.Console()->Getch();
|
sl@0
|
57 |
TKeyCode code2 = test.Console()->Getch();
|
sl@0
|
58 |
TChar ch1(code1);
|
sl@0
|
59 |
TChar ch2(code2);
|
sl@0
|
60 |
TBuf<2> centuryOffset;
|
sl@0
|
61 |
centuryOffset.Append(ch1);
|
sl@0
|
62 |
centuryOffset.Append(ch2);
|
sl@0
|
63 |
TLex lex(centuryOffset);
|
sl@0
|
64 |
if (lex.Val(offset)!=KErrNone)
|
sl@0
|
65 |
return KErrGeneral;
|
sl@0
|
66 |
test.Printf(_L("The century offset is set to %d\n"),offset);
|
sl@0
|
67 |
if (ConvertDesToDateTimeInteractively()==KErrGeneral)
|
sl@0
|
68 |
return KErrGeneral;
|
sl@0
|
69 |
}
|
sl@0
|
70 |
TChar ch(code);
|
sl@0
|
71 |
buf.Append(ch);
|
sl@0
|
72 |
TBuf<1> charBuf;
|
sl@0
|
73 |
charBuf.Append(ch);
|
sl@0
|
74 |
test.Printf(charBuf);
|
sl@0
|
75 |
}
|
sl@0
|
76 |
test.Printf(_L(" = "));
|
sl@0
|
77 |
TDateTime dateTime;
|
sl@0
|
78 |
TInt error = DateTimeParse(dateTime,buf,offset);
|
sl@0
|
79 |
TInt year = dateTime.Year();
|
sl@0
|
80 |
TInt month = dateTime.Month()+1;
|
sl@0
|
81 |
TInt day = dateTime.Day()+1;
|
sl@0
|
82 |
TInt hour = dateTime.Hour();
|
sl@0
|
83 |
TInt min = dateTime.Minute();
|
sl@0
|
84 |
TInt sec = dateTime.Second();
|
sl@0
|
85 |
TInt msec = dateTime.MicroSecond();
|
sl@0
|
86 |
switch(error)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
case EParseTimePresent:
|
sl@0
|
89 |
test.Printf(_L("%d:%d:%d.%d "),hour,min,sec,msec);
|
sl@0
|
90 |
test.Printf(_L("Time\n"));
|
sl@0
|
91 |
break;
|
sl@0
|
92 |
case EParseDatePresent:
|
sl@0
|
93 |
test.Printf(_L("%d/%d/%d "),day,month,year);
|
sl@0
|
94 |
test.Printf(_L("Date\n"));
|
sl@0
|
95 |
break;
|
sl@0
|
96 |
case EParseTimePresent|EParseDatePresent:
|
sl@0
|
97 |
test.Printf(_L("%d:%d:%d.%d %d/%d/%d "),hour,min,sec,msec,day,month,year);
|
sl@0
|
98 |
test.Printf(_L("DateAndTime\n"));
|
sl@0
|
99 |
break;
|
sl@0
|
100 |
default:
|
sl@0
|
101 |
test.Printf(_L("error\n"));
|
sl@0
|
102 |
break;
|
sl@0
|
103 |
}
|
sl@0
|
104 |
if (ConvertDesToDateTimeInteractively()==KErrGeneral)
|
sl@0
|
105 |
return KErrGeneral;
|
sl@0
|
106 |
return KErrNone;
|
sl@0
|
107 |
}
|
sl@0
|
108 |
|
sl@0
|
109 |
|
sl@0
|
110 |
TInt E32Main()
|
sl@0
|
111 |
{
|
sl@0
|
112 |
test.Start(_L("Begin tests"));
|
sl@0
|
113 |
test.Console()->Printf(_L("Convert a Des To a DateTime.\n"));
|
sl@0
|
114 |
test.Printf(_L("Enter a Date, a Time or a Date & Time.\n"));
|
sl@0
|
115 |
test.Printf(_L("Press Escape to exit tests.\n"));
|
sl@0
|
116 |
test.Printf(_L("Press the F1 key to set the century offset.\n"));
|
sl@0
|
117 |
ConvertDesToDateTimeInteractively();
|
sl@0
|
118 |
test.End();
|
sl@0
|
119 |
return KErrNone;
|
sl@0
|
120 |
}
|
sl@0
|
121 |
|
sl@0
|
122 |
|