Update contrib.
1 // Copyright (c) 2005-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Simple STDLIB tests.
22 #include <sys/reent.h>
26 //CPP file is used for C tests, because by default any console opened from a C file
27 //expects a key to be pressed when it is about to be closed. That makes impossible
28 //the creation of automated C tests.
33 LOCAL_D RTest test(_L("TTime"));
34 _LIT16(priorUnixTime,"19700000:000000.000000"); //0 AD to the start of Unix Time
41 @SYMTestCaseID SYSLIB-STDLIB-CT-0143
42 @SYMTestCaseDesc Check that gettimeofday() returns universaltime, rather than the local time.
44 @SYMTestActions retrieve return value of gettimeofday() and compare with preset universaltime
45 @SYMTestExpectedResults The test must not fail.
48 void Testgettimeofday()
50 test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0143 "));
51 test.Printf(_L("\ntesting gettimeofday()...\n"));
53 TInt error=tz.Connect();
54 test(error==KErrNone);
55 CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
56 CleanupStack::PushL(tzId);
57 tz.SetTimeZoneL(*tzId);
60 struct timezone tzone;
62 unix.Set(priorUnixTime);
64 test.Printf(_L("tests during summertime (dst on)...\t"));
65 //set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
66 TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0)));
69 err = gettimeofday(&tv, &tzone);
71 TTimeIntervalSeconds s = (User::UTCOffset().Int())/60;
72 test(tzone.tz_minuteswest==s.Int());
73 test(tzone.tz_dsttime == 0);
74 // Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns
75 // seconds from 1970 to now,
76 TInt64 sec = tv.tv_sec;
77 TUint64 microSec = (sec*1000000) + tv.tv_usec + unix.Int64();
78 test.Printf(_L("Expected Time: %ld\tReceived Time: %ld\n"),t.Int64(),microSec);
79 test((microSec-t.Int64())<1000000);//allowing a 1 sec delay in time
80 test.Printf(_L("-OK\n"));
82 test.Printf(_L("tests during wintertime (dst off)...\t"));
83 //set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date
84 err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0)));
87 err = gettimeofday(&tv, &tzone);
89 // Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns
90 // seconds from 1970 to now,
92 microSec = (sec*1000000) + tv.tv_usec + unix.Int64();
93 test((microSec-t.Int64())<1000000);//allowing a 1 sec delay in time
94 test.Printf(_L("-OK\n"));
96 CleanupStack::PopAndDestroy(tzId);
102 @SYMTestCaseID SYSLIB-STDLIB-CT-0144
103 @SYMTestCaseDesc Check that time() returns universaltime, rather than the local time.
105 @SYMTestActions retrieve return value of time() and compare with preset universaltime
106 @SYMTestExpectedResults The test must not fail.
111 test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0144 \ntesting time()...\n "));
119 CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
120 CleanupStack::PushL(tzId);
121 tz.SetTimeZoneL(*tzId);
123 unix.Set(priorUnixTime);
125 test.Printf(_L("tests during summertime (dst on)...\t"));
126 //set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
127 TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0)));
130 time_t res = time(0) * 1000000; // current time
131 // As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now,
132 // the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds
133 TInt64 sec = t.Int64() - unix.Int64();
134 test((res-sec)<1000000);//allowing 1 sec delay in time
135 test.Printf(_L("- OK!\n"));
137 test.Printf(_L("tests during wintertime (dst off)...\t"));
138 //set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date
139 err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0)));
142 res = time(0) * 1000000; // current time
143 // As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now,
144 // the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds
145 sec = t.Int64() - unix.Int64();
146 test((res-sec)<1000000);//allowing 1 sec delay in time
147 test.Printf(_L("- OK!\n"));
150 CleanupStack::PopAndDestroy(tzId);
156 @SYMTestCaseID SYSLIB-STDLIB-CT-0145
157 @SYMTestCaseDesc Check that toLocal() converts into correct localtime
159 @SYMTestActions With the Timezone set to Europe/London, a universaltime is passed to
160 the function localtime (as toLocal cannot be accessed directly) which is expected to return a hometime, with DST on
161 @SYMTestExpectedResults The test must not fail.
166 test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0145 \ntesting toLocal()...\n "));
168 //test when dst is on...
169 test.Printf(_L("tests during summertime (dst on)...\t"));
174 //set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
175 TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0)));
183 CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
184 CleanupStack::PushL(tzId);
185 tz.SetTimeZoneL(*tzId);
186 CleanupStack::PopAndDestroy(tzId);
189 struct tm *ptr, *ptr2;
193 setup.tm_hour = 8; //8 o'clock utc time
199 seconds = mktime(&setup);
201 ptr2 = gmtime(&seconds); //for a quick routine test
202 test(ptr2->tm_hour == 8);//
204 ptr = localtime(&seconds);
205 test(ptr->tm_hour == 9); //test against hometime hour: 9;
206 test(ptr->tm_min == 55);
207 test(ptr->tm_sec == 0);
208 test(ptr->tm_mday == 1);
209 test(ptr->tm_mon == 3);
210 test(ptr->tm_year == 105);
211 test.Printf(_L("Time:9:55 -correct!\n"));
213 //test when DST is off
214 test.Printf(_L("tests during wintertime (dst off)...\t"));
215 err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0)));
224 CTzId* tzId2 = CTzId::NewL(2592); //set the timezone to Europe/London
225 CleanupStack::PushL(tzId2);
226 tz.SetTimeZoneL(*tzId2);
227 CleanupStack::PopAndDestroy(tzId2);
230 ptr2 = gmtime(&seconds); //for a quick routine test
231 test(ptr2->tm_hour == 8);//
233 ptr = localtime(&seconds);
234 test(ptr->tm_hour == 8); //test against hometime hour: 8;
235 test(ptr->tm_min == 55);
236 test(ptr->tm_sec == 0);
237 test(ptr->tm_mday == 1);
238 test(ptr->tm_mon == 3);
239 test(ptr->tm_year == 105);
240 test.Printf(_L("Time:8:55 -correct!\n"));
248 LOCAL_C void DoTestsL()
250 TRAPD(err,Testgettimeofday());
252 TRAP(err,Testtime());
254 TRAP(err,TesttoLocal());
258 GLDEF_C TInt E32Main()
263 test.Start(_L("Time & Date Tests..."));
265 CTrapCleanup* trapCleanup=CTrapCleanup::New();
266 TRAPD(error, DoTestsL());
267 test(error==KErrNone);