os/ossrv/genericopenlibs/cstdlib/TSTLIB/T_PR234_LTIME.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/T_PR234_LTIME.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,276 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Simple STDLIB tests.
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <e32test.h>
    1.22 +#include <e32svr.h>
    1.23 +#include <sys/time.h>
    1.24 +#include <time.h>
    1.25 +#include <sys/reent.h>
    1.26 +#include <tz.h>
    1.27 +
    1.28 +
    1.29 +//CPP file is used for C tests, because by default any console opened from a C file
    1.30 +//expects a key to be pressed when it is about to be closed. That makes impossible
    1.31 +//the creation of automated C tests.
    1.32 +
    1.33 +//
    1.34 +// Globals
    1.35 +
    1.36 +LOCAL_D RTest test(_L("TTime"));
    1.37 +_LIT16(priorUnixTime,"19700000:000000.000000"); //0 AD to the start of Unix Time
    1.38 +
    1.39 +//
    1.40 +// Tests
    1.41 +
    1.42 +/**
    1.43 +@file
    1.44 +@SYMTestCaseID		SYSLIB-STDLIB-CT-0143
    1.45 +@SYMTestCaseDesc 	Check that gettimeofday() returns universaltime, rather than the local time. 
    1.46 +@SYMTestPriority 	low
    1.47 +@SYMTestActions  	retrieve return value of gettimeofday() and compare with preset universaltime
    1.48 +@SYMTestExpectedResults The test must not fail.
    1.49 +@SYMPREQ PREQ234
    1.50 +*/
    1.51 +void Testgettimeofday()
    1.52 +	{
    1.53 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0143 "));
    1.54 +	test.Printf(_L("\ntesting gettimeofday()...\n"));
    1.55 +	RTz tz;
    1.56 +	TInt error=tz.Connect();
    1.57 +	test(error==KErrNone);
    1.58 +	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
    1.59 +	CleanupStack::PushL(tzId);
    1.60 +	tz.SetTimeZoneL(*tzId);
    1.61 +	
    1.62 +	struct timeval tv;
    1.63 +	struct timezone tzone;
    1.64 +	TTime t,unix;
    1.65 +	unix.Set(priorUnixTime);
    1.66 +	
    1.67 +	test.Printf(_L("tests during summertime (dst on)...\t"));	
    1.68 +	//set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
    1.69 +	TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0))); 
    1.70 +	test(err==0);
    1.71 +	t.UniversalTime();
    1.72 +	err = gettimeofday(&tv, &tzone);
    1.73 +	test(err==0);
    1.74 +	TTimeIntervalSeconds s = (User::UTCOffset().Int())/60;
    1.75 +	test(tzone.tz_minuteswest==s.Int());
    1.76 +	test(tzone.tz_dsttime == 0);
    1.77 +	// Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns 
    1.78 +	// seconds from 1970 to now,
    1.79 +	TInt64 sec = tv.tv_sec;
    1.80 +	TUint64 microSec = (sec*1000000) + tv.tv_usec + unix.Int64();
    1.81 +    test.Printf(_L("Expected Time: %ld\tReceived Time: %ld\n"),t.Int64(),microSec);
    1.82 +	test((microSec-t.Int64())<1000000);//allowing a 1 sec delay in time
    1.83 +	test.Printf(_L("-OK\n"));
    1.84 +	
    1.85 +	test.Printf(_L("tests during wintertime (dst off)...\t"));	
    1.86 +	//set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date
    1.87 +	err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0))); 
    1.88 +	test(err==0);
    1.89 +	t.UniversalTime();
    1.90 +	err = gettimeofday(&tv, &tzone);
    1.91 +	test(err==0);
    1.92 +	// Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns 
    1.93 +	// seconds from 1970 to now,
    1.94 +	sec = tv.tv_sec;
    1.95 +	microSec = (sec*1000000) + tv.tv_usec + unix.Int64();
    1.96 +	test((microSec-t.Int64())<1000000);//allowing a 1 sec delay in time
    1.97 +	test.Printf(_L("-OK\n"));
    1.98 +	//
    1.99 +	CleanupStack::PopAndDestroy(tzId);
   1.100 +	tz.Close();
   1.101 +	}
   1.102 +
   1.103 +/**
   1.104 +@file
   1.105 +@SYMTestCaseID		SYSLIB-STDLIB-CT-0144
   1.106 +@SYMTestCaseDesc 	Check that time() returns universaltime, rather than the local time. 
   1.107 +@SYMTestPriority 	low
   1.108 +@SYMTestActions  	retrieve return value of time() and compare with preset universaltime
   1.109 +@SYMTestExpectedResults The test must not fail.
   1.110 +@SYMPREQ PREQ234
   1.111 +*/
   1.112 +void Testtime()
   1.113 +	{
   1.114 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0144 \ntesting time()...\n "));
   1.115 +	TInt r = KErrNone;
   1.116 +    RTz tz;
   1.117 +    r = tz.Connect();
   1.118 +    if (r != KErrNone)
   1.119 +        {
   1.120 +            User::Leave(r);
   1.121 +        }
   1.122 +	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
   1.123 +	CleanupStack::PushL(tzId);
   1.124 +	tz.SetTimeZoneL(*tzId);
   1.125 +	TTime t,unix;
   1.126 +	unix.Set(priorUnixTime);
   1.127 +	
   1.128 +	test.Printf(_L("tests during summertime (dst on)...\t"));
   1.129 +	//set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
   1.130 +	TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0)));
   1.131 +	test(err==0);
   1.132 +	t.UniversalTime(); 	
   1.133 +	time_t res = time(0) * 1000000; // current time
   1.134 +	// As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now,
   1.135 +	// the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds
   1.136 +	TInt64 sec = t.Int64() - unix.Int64(); 
   1.137 +	test((res-sec)<1000000);//allowing 1 sec delay in time
   1.138 +	test.Printf(_L("- OK!\n"));
   1.139 +	
   1.140 +	test.Printf(_L("tests during wintertime (dst off)...\t"));
   1.141 +	//set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date
   1.142 +	err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0)));
   1.143 +	test(err==0);
   1.144 +	t.UniversalTime(); 	
   1.145 +	res = time(0) * 1000000; // current time
   1.146 +	// As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now,
   1.147 +	// the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds
   1.148 +	sec = t.Int64() - unix.Int64(); 
   1.149 +	test((res-sec)<1000000);//allowing 1 sec delay in time
   1.150 +	test.Printf(_L("- OK!\n"));
   1.151 +	
   1.152 +	//
   1.153 +	CleanupStack::PopAndDestroy(tzId);
   1.154 +	tz.Close();	
   1.155 +	}
   1.156 +
   1.157 +/**
   1.158 +@file
   1.159 +@SYMTestCaseID		SYSLIB-STDLIB-CT-0145
   1.160 +@SYMTestCaseDesc 	Check that toLocal() converts into correct localtime
   1.161 +@SYMTestPriority 	low
   1.162 +@SYMTestActions  	With the Timezone set to Europe/London, a universaltime is passed to 
   1.163 +the function localtime (as toLocal cannot be accessed directly) which is expected to return a hometime, with DST on
   1.164 +@SYMTestExpectedResults The test must not fail.
   1.165 +@SYMPREQ PREQ234
   1.166 +*/
   1.167 +void TesttoLocal()
   1.168 +	{
   1.169 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0145  \ntesting toLocal()...\n "));
   1.170 +	
   1.171 +	//test when dst is on...
   1.172 +	test.Printf(_L("tests during summertime (dst on)...\t"));
   1.173 +	
   1.174 +	TInt rt = KErrNone;
   1.175 +	TInt rz = KErrNone;
   1.176 +	
   1.177 +	//set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
   1.178 +	TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0)));
   1.179 +	test(err==0);
   1.180 +	RTz tz;
   1.181 +    rt = tz.Connect();
   1.182 +    if (rt != KErrNone)
   1.183 +        {
   1.184 +            User::Leave(rt);
   1.185 +        }
   1.186 +	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
   1.187 +	CleanupStack::PushL(tzId);
   1.188 +	tz.SetTimeZoneL(*tzId);
   1.189 +	CleanupStack::PopAndDestroy(tzId);
   1.190 +	tz.Close();
   1.191 +	
   1.192 +	struct tm *ptr, *ptr2;
   1.193 +	struct tm setup;
   1.194 +	time_t seconds;
   1.195 +	
   1.196 +	setup.tm_hour = 8; //8 o'clock utc time
   1.197 +	setup.tm_min = 55; 
   1.198 +	setup.tm_sec = 0; 
   1.199 +	setup.tm_mday = 1;
   1.200 +	setup.tm_mon = 3;
   1.201 +	setup.tm_year = 105;
   1.202 +	seconds = mktime(&setup);
   1.203 +	
   1.204 +	ptr2 = gmtime(&seconds); //for a quick routine test
   1.205 +	test(ptr2->tm_hour == 8);//
   1.206 +	
   1.207 +	ptr = localtime(&seconds);	
   1.208 +	test(ptr->tm_hour == 9); //test against hometime hour: 9;
   1.209 +	test(ptr->tm_min == 55);
   1.210 +	test(ptr->tm_sec == 0);
   1.211 +	test(ptr->tm_mday == 1);
   1.212 +	test(ptr->tm_mon == 3);
   1.213 +	test(ptr->tm_year == 105);
   1.214 +	test.Printf(_L("Time:9:55 -correct!\n"));
   1.215 +	
   1.216 +	//test when DST is off
   1.217 +	test.Printf(_L("tests during wintertime (dst off)...\t"));
   1.218 +	err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0)));
   1.219 +	test(err==0);
   1.220 +	
   1.221 +    rz = tz.Connect();
   1.222 +    if (rz != KErrNone)
   1.223 +        {
   1.224 +            User::Leave(rz);
   1.225 +        }
   1.226 +    
   1.227 +	CTzId* tzId2 = CTzId::NewL(2592); //set the timezone to Europe/London
   1.228 +	CleanupStack::PushL(tzId2);
   1.229 +	tz.SetTimeZoneL(*tzId2);
   1.230 +	CleanupStack::PopAndDestroy(tzId2);
   1.231 +	tz.Close();
   1.232 +	
   1.233 +	ptr2 = gmtime(&seconds); //for a quick routine test
   1.234 +	test(ptr2->tm_hour == 8);//
   1.235 +	
   1.236 +	ptr = localtime(&seconds);	
   1.237 +	test(ptr->tm_hour == 8); //test against hometime hour: 8;
   1.238 +	test(ptr->tm_min == 55);
   1.239 +	test(ptr->tm_sec == 0);
   1.240 +	test(ptr->tm_mday == 1);
   1.241 +	test(ptr->tm_mon == 3);
   1.242 +	test(ptr->tm_year == 105);
   1.243 +	test.Printf(_L("Time:8:55 -correct!\n"));
   1.244 +	//
   1.245 +	CloseSTDLIB();	
   1.246 +	}
   1.247 +
   1.248 +//
   1.249 +//
   1.250 +
   1.251 +LOCAL_C void DoTestsL()
   1.252 +	{
   1.253 +	TRAPD(err,Testgettimeofday());
   1.254 +	test(err==KErrNone);
   1.255 +	TRAP(err,Testtime());
   1.256 +	test(err==KErrNone);
   1.257 +	TRAP(err,TesttoLocal());
   1.258 +	test(err==KErrNone);
   1.259 +	}
   1.260 +
   1.261 +GLDEF_C TInt E32Main()
   1.262 +	{
   1.263 +	__UHEAP_MARK;
   1.264 +
   1.265 +	test.Title();
   1.266 +	test.Start(_L("Time & Date Tests..."));
   1.267 +
   1.268 +	CTrapCleanup* trapCleanup=CTrapCleanup::New();
   1.269 +	TRAPD(error, DoTestsL());
   1.270 +	test(error==KErrNone);
   1.271 +	delete trapCleanup;
   1.272 +
   1.273 +	test.End();
   1.274 +	test.Close();
   1.275 +
   1.276 +	__UHEAP_MARKEND;
   1.277 +	return error;
   1.278 +	}
   1.279 +