kerneltest/e32test/datetime/t_dparse.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1996-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 the License "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 // e32test\datetime\t_dparse.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 
       
    20 RTest test(_L("T_DPARSE"));
       
    21 TInt offset=0;
       
    22 
       
    23 LOCAL_C TInt DateTimeParse(TDateTime& aDateTime,const TDesC& aDes,TInt aCenturyOffset=0)
       
    24 	{
       
    25 	TTime time;
       
    26 	TInt r=time.Parse(aDes,aCenturyOffset);
       
    27 	if (r>=0)
       
    28 		aDateTime=time.DateTime();
       
    29 	return r;
       
    30 	}
       
    31 
       
    32 LOCAL_D TInt ConvertDesToDateTimeInteractively()
       
    33 	{
       
    34 	TKeyCode code;
       
    35 	TBuf<32> buf;
       
    36 	buf.SetLength(0);
       
    37 	FOREVER
       
    38 		{
       
    39 		code = test.Console()->Getch();
       
    40 		if (code==EKeyEnter || code==EKeyLineFeed)
       
    41 			break;
       
    42 		if (code==EKeyEscape)
       
    43 			return KErrGeneral;
       
    44 		if ((code==EKeyDelete || code==EKeyBackspace) && buf.Length()>0)
       
    45 			{
       
    46 			TChar del(code);
       
    47 			TBuf<1> delBuf;
       
    48 			delBuf.Append(del);
       
    49 			test.Printf(delBuf);
       
    50 			buf.SetLength(buf.Length()-1);
       
    51 			continue;
       
    52 			}
       
    53 		if (buf.Length()==0 && code==EKeyF1)
       
    54 			{
       
    55 			test.Printf(_L("Enter a two digit value. "));	
       
    56 			TKeyCode code1 = test.Console()->Getch();
       
    57 			TKeyCode code2 = test.Console()->Getch();
       
    58 			TChar ch1(code1);
       
    59 			TChar ch2(code2);
       
    60 			TBuf<2> centuryOffset;
       
    61 			centuryOffset.Append(ch1);
       
    62 			centuryOffset.Append(ch2);
       
    63 			TLex lex(centuryOffset);
       
    64 			if (lex.Val(offset)!=KErrNone)
       
    65 				return KErrGeneral;
       
    66 			test.Printf(_L("The century offset is set to %d\n"),offset);
       
    67 			if (ConvertDesToDateTimeInteractively()==KErrGeneral)
       
    68 				return KErrGeneral;
       
    69 			}
       
    70 		TChar ch(code);
       
    71 		buf.Append(ch);
       
    72 		TBuf<1> charBuf;
       
    73 		charBuf.Append(ch);
       
    74 		test.Printf(charBuf);
       
    75 		} 
       
    76 	test.Printf(_L(" = "));
       
    77 	TDateTime dateTime;
       
    78 	TInt error = DateTimeParse(dateTime,buf,offset);
       
    79 	TInt year = dateTime.Year();
       
    80 	TInt month = dateTime.Month()+1;
       
    81 	TInt day = dateTime.Day()+1;
       
    82 	TInt hour = dateTime.Hour(); 
       
    83 	TInt min = dateTime.Minute();
       
    84 	TInt sec = dateTime.Second();
       
    85 	TInt msec = dateTime.MicroSecond();
       
    86 	switch(error)
       
    87 		{
       
    88 		case EParseTimePresent:
       
    89 			test.Printf(_L("%d:%d:%d.%d  "),hour,min,sec,msec);
       
    90 			test.Printf(_L("Time\n"));
       
    91 			break;
       
    92 		case EParseDatePresent:
       
    93 			test.Printf(_L("%d/%d/%d  "),day,month,year);
       
    94 			test.Printf(_L("Date\n"));
       
    95 			break;
       
    96 		case EParseTimePresent|EParseDatePresent:
       
    97 			test.Printf(_L("%d:%d:%d.%d %d/%d/%d  "),hour,min,sec,msec,day,month,year);
       
    98 			test.Printf(_L("DateAndTime\n"));
       
    99 			break;
       
   100 		default:
       
   101 			test.Printf(_L("error\n"));
       
   102 			break;
       
   103 		}
       
   104 	if (ConvertDesToDateTimeInteractively()==KErrGeneral)
       
   105 		return KErrGeneral;
       
   106 	return KErrNone;
       
   107 	}
       
   108 
       
   109 
       
   110 TInt E32Main()
       
   111 	{
       
   112 	test.Start(_L("Begin tests"));
       
   113 	test.Console()->Printf(_L("Convert a Des To a DateTime.\n"));
       
   114 	test.Printf(_L("Enter a Date, a Time or a Date & Time.\n"));
       
   115 	test.Printf(_L("Press Escape to exit tests.\n"));
       
   116 	test.Printf(_L("Press the F1 key to set the century offset.\n"));
       
   117 	ConvertDesToDateTimeInteractively();
       
   118 	test.End();
       
   119 	return KErrNone;
       
   120 	}
       
   121 
       
   122