|
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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Simple STDLIB tests. |
|
15 // |
|
16 // |
|
17 |
|
18 #include <e32test.h> |
|
19 #include <e32svr.h> |
|
20 #include <sys/time.h> |
|
21 #include <time.h> |
|
22 #include <sys/reent.h> |
|
23 #include <tz.h> |
|
24 |
|
25 |
|
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. |
|
29 |
|
30 // |
|
31 // Globals |
|
32 |
|
33 LOCAL_D RTest test(_L("TTime")); |
|
34 _LIT16(priorUnixTime,"19700000:000000.000000"); //0 AD to the start of Unix Time |
|
35 |
|
36 // |
|
37 // Tests |
|
38 |
|
39 /** |
|
40 @file |
|
41 @SYMTestCaseID SYSLIB-STDLIB-CT-0143 |
|
42 @SYMTestCaseDesc Check that gettimeofday() returns universaltime, rather than the local time. |
|
43 @SYMTestPriority low |
|
44 @SYMTestActions retrieve return value of gettimeofday() and compare with preset universaltime |
|
45 @SYMTestExpectedResults The test must not fail. |
|
46 @SYMPREQ PREQ234 |
|
47 */ |
|
48 void Testgettimeofday() |
|
49 { |
|
50 test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0143 ")); |
|
51 test.Printf(_L("\ntesting gettimeofday()...\n")); |
|
52 RTz tz; |
|
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); |
|
58 |
|
59 struct timeval tv; |
|
60 struct timezone tzone; |
|
61 TTime t,unix; |
|
62 unix.Set(priorUnixTime); |
|
63 |
|
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))); |
|
67 test(err==0); |
|
68 t.UniversalTime(); |
|
69 err = gettimeofday(&tv, &tzone); |
|
70 test(err==0); |
|
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")); |
|
81 |
|
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))); |
|
85 test(err==0); |
|
86 t.UniversalTime(); |
|
87 err = gettimeofday(&tv, &tzone); |
|
88 test(err==0); |
|
89 // Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns |
|
90 // seconds from 1970 to now, |
|
91 sec = tv.tv_sec; |
|
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")); |
|
95 // |
|
96 CleanupStack::PopAndDestroy(tzId); |
|
97 tz.Close(); |
|
98 } |
|
99 |
|
100 /** |
|
101 @file |
|
102 @SYMTestCaseID SYSLIB-STDLIB-CT-0144 |
|
103 @SYMTestCaseDesc Check that time() returns universaltime, rather than the local time. |
|
104 @SYMTestPriority low |
|
105 @SYMTestActions retrieve return value of time() and compare with preset universaltime |
|
106 @SYMTestExpectedResults The test must not fail. |
|
107 @SYMPREQ PREQ234 |
|
108 */ |
|
109 void Testtime() |
|
110 { |
|
111 test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0144 \ntesting time()...\n ")); |
|
112 RTz tz; |
|
113 tz.Connect(); |
|
114 CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London |
|
115 CleanupStack::PushL(tzId); |
|
116 tz.SetTimeZoneL(*tzId); |
|
117 TTime t,unix; |
|
118 unix.Set(priorUnixTime); |
|
119 |
|
120 test.Printf(_L("tests during summertime (dst on)...\t")); |
|
121 //set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date |
|
122 TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0))); |
|
123 test(err==0); |
|
124 t.UniversalTime(); |
|
125 time_t res = time(0) * 1000000; // current time |
|
126 // As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now, |
|
127 // the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds |
|
128 TInt64 sec = t.Int64() - unix.Int64(); |
|
129 test((res-sec)<1000000);//allowing 1 sec delay in time |
|
130 test.Printf(_L("- OK!\n")); |
|
131 |
|
132 test.Printf(_L("tests during wintertime (dst off)...\t")); |
|
133 //set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date |
|
134 err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0))); |
|
135 test(err==0); |
|
136 t.UniversalTime(); |
|
137 res = time(0) * 1000000; // current time |
|
138 // As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now, |
|
139 // the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds |
|
140 sec = t.Int64() - unix.Int64(); |
|
141 test((res-sec)<1000000);//allowing 1 sec delay in time |
|
142 test.Printf(_L("- OK!\n")); |
|
143 |
|
144 // |
|
145 CleanupStack::PopAndDestroy(tzId); |
|
146 tz.Close(); |
|
147 } |
|
148 |
|
149 /** |
|
150 @file |
|
151 @SYMTestCaseID SYSLIB-STDLIB-CT-0145 |
|
152 @SYMTestCaseDesc Check that toLocal() converts into correct localtime |
|
153 @SYMTestPriority low |
|
154 @SYMTestActions With the Timezone set to Europe/London, a universaltime is passed to |
|
155 the function localtime (as toLocal cannot be accessed directly) which is expected to return a hometime, with DST on |
|
156 @SYMTestExpectedResults The test must not fail. |
|
157 @SYMPREQ PREQ234 |
|
158 */ |
|
159 void TesttoLocal() |
|
160 { |
|
161 test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0145 \ntesting toLocal()...\n ")); |
|
162 |
|
163 //test when dst is on... |
|
164 test.Printf(_L("tests during summertime (dst on)...\t")); |
|
165 //set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date |
|
166 TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0))); |
|
167 test(err==0); |
|
168 RTz tz; |
|
169 tz.Connect(); |
|
170 CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London |
|
171 CleanupStack::PushL(tzId); |
|
172 tz.SetTimeZoneL(*tzId); |
|
173 CleanupStack::PopAndDestroy(tzId); |
|
174 tz.Close(); |
|
175 |
|
176 struct tm *ptr, *ptr2; |
|
177 struct tm setup; |
|
178 time_t seconds; |
|
179 |
|
180 setup.tm_hour = 8; //8 o'clock utc time |
|
181 setup.tm_min = 55; |
|
182 setup.tm_sec = 0; |
|
183 setup.tm_mday = 1; |
|
184 setup.tm_mon = 3; |
|
185 setup.tm_year = 105; |
|
186 seconds = mktime(&setup); |
|
187 |
|
188 ptr2 = gmtime(&seconds); //for a quick routine test |
|
189 test(ptr2->tm_hour == 8);// |
|
190 |
|
191 ptr = localtime(&seconds); |
|
192 test(ptr->tm_hour == 9); //test against hometime hour: 9; |
|
193 test(ptr->tm_min == 55); |
|
194 test(ptr->tm_sec == 0); |
|
195 test(ptr->tm_mday == 1); |
|
196 test(ptr->tm_mon == 3); |
|
197 test(ptr->tm_year == 105); |
|
198 test.Printf(_L("Time:9:55 -correct!\n")); |
|
199 |
|
200 //test when DST is off |
|
201 test.Printf(_L("tests during wintertime (dst off)...\t")); |
|
202 err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0))); |
|
203 test(err==0); |
|
204 |
|
205 tz.Connect(); |
|
206 CTzId* tzId2 = CTzId::NewL(2592); //set the timezone to Europe/London |
|
207 CleanupStack::PushL(tzId2); |
|
208 tz.SetTimeZoneL(*tzId2); |
|
209 CleanupStack::PopAndDestroy(tzId2); |
|
210 tz.Close(); |
|
211 |
|
212 ptr2 = gmtime(&seconds); //for a quick routine test |
|
213 test(ptr2->tm_hour == 8);// |
|
214 |
|
215 ptr = localtime(&seconds); |
|
216 test(ptr->tm_hour == 8); //test against hometime hour: 8; |
|
217 test(ptr->tm_min == 55); |
|
218 test(ptr->tm_sec == 0); |
|
219 test(ptr->tm_mday == 1); |
|
220 test(ptr->tm_mon == 3); |
|
221 test(ptr->tm_year == 105); |
|
222 test.Printf(_L("Time:8:55 -correct!\n")); |
|
223 // |
|
224 CloseSTDLIB(); |
|
225 } |
|
226 |
|
227 // |
|
228 // |
|
229 |
|
230 LOCAL_C void DoTestsL() |
|
231 { |
|
232 TRAPD(err,Testgettimeofday()); |
|
233 test(err==KErrNone); |
|
234 TRAP(err,Testtime()); |
|
235 test(err==KErrNone); |
|
236 TRAP(err,TesttoLocal()); |
|
237 test(err==KErrNone); |
|
238 } |
|
239 |
|
240 GLDEF_C TInt E32Main() |
|
241 { |
|
242 __UHEAP_MARK; |
|
243 |
|
244 test.Title(); |
|
245 test.Start(_L("Time & Date Tests...")); |
|
246 |
|
247 CTrapCleanup* trapCleanup=CTrapCleanup::New(); |
|
248 TRAPD(error, DoTestsL()); |
|
249 test(error==KErrNone); |
|
250 delete trapCleanup; |
|
251 |
|
252 test.End(); |
|
253 test.Close(); |
|
254 |
|
255 __UHEAP_MARKEND; |
|
256 return error; |
|
257 } |
|
258 |