os/ossrv/genericopenlibs/cstdlib/TSTLIB/T_PR234_LTIME.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     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 	TInt r = KErrNone;
   113     RTz tz;
   114     r = tz.Connect();
   115     if (r != KErrNone)
   116         {
   117             User::Leave(r);
   118         }
   119 	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
   120 	CleanupStack::PushL(tzId);
   121 	tz.SetTimeZoneL(*tzId);
   122 	TTime t,unix;
   123 	unix.Set(priorUnixTime);
   124 	
   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)));
   128 	test(err==0);
   129 	t.UniversalTime(); 	
   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"));
   136 	
   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)));
   140 	test(err==0);
   141 	t.UniversalTime(); 	
   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"));
   148 	
   149 	//
   150 	CleanupStack::PopAndDestroy(tzId);
   151 	tz.Close();	
   152 	}
   153 
   154 /**
   155 @file
   156 @SYMTestCaseID		SYSLIB-STDLIB-CT-0145
   157 @SYMTestCaseDesc 	Check that toLocal() converts into correct localtime
   158 @SYMTestPriority 	low
   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.
   162 @SYMPREQ PREQ234
   163 */
   164 void TesttoLocal()
   165 	{
   166 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0145  \ntesting toLocal()...\n "));
   167 	
   168 	//test when dst is on...
   169 	test.Printf(_L("tests during summertime (dst on)...\t"));
   170 	
   171 	TInt rt = KErrNone;
   172 	TInt rz = KErrNone;
   173 	
   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)));
   176 	test(err==0);
   177 	RTz tz;
   178     rt = tz.Connect();
   179     if (rt != KErrNone)
   180         {
   181             User::Leave(rt);
   182         }
   183 	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
   184 	CleanupStack::PushL(tzId);
   185 	tz.SetTimeZoneL(*tzId);
   186 	CleanupStack::PopAndDestroy(tzId);
   187 	tz.Close();
   188 	
   189 	struct tm *ptr, *ptr2;
   190 	struct tm setup;
   191 	time_t seconds;
   192 	
   193 	setup.tm_hour = 8; //8 o'clock utc time
   194 	setup.tm_min = 55; 
   195 	setup.tm_sec = 0; 
   196 	setup.tm_mday = 1;
   197 	setup.tm_mon = 3;
   198 	setup.tm_year = 105;
   199 	seconds = mktime(&setup);
   200 	
   201 	ptr2 = gmtime(&seconds); //for a quick routine test
   202 	test(ptr2->tm_hour == 8);//
   203 	
   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"));
   212 	
   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)));
   216 	test(err==0);
   217 	
   218     rz = tz.Connect();
   219     if (rz != KErrNone)
   220         {
   221             User::Leave(rz);
   222         }
   223     
   224 	CTzId* tzId2 = CTzId::NewL(2592); //set the timezone to Europe/London
   225 	CleanupStack::PushL(tzId2);
   226 	tz.SetTimeZoneL(*tzId2);
   227 	CleanupStack::PopAndDestroy(tzId2);
   228 	tz.Close();
   229 	
   230 	ptr2 = gmtime(&seconds); //for a quick routine test
   231 	test(ptr2->tm_hour == 8);//
   232 	
   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"));
   241 	//
   242 	CloseSTDLIB();	
   243 	}
   244 
   245 //
   246 //
   247 
   248 LOCAL_C void DoTestsL()
   249 	{
   250 	TRAPD(err,Testgettimeofday());
   251 	test(err==KErrNone);
   252 	TRAP(err,Testtime());
   253 	test(err==KErrNone);
   254 	TRAP(err,TesttoLocal());
   255 	test(err==KErrNone);
   256 	}
   257 
   258 GLDEF_C TInt E32Main()
   259 	{
   260 	__UHEAP_MARK;
   261 
   262 	test.Title();
   263 	test.Start(_L("Time & Date Tests..."));
   264 
   265 	CTrapCleanup* trapCleanup=CTrapCleanup::New();
   266 	TRAPD(error, DoTestsL());
   267 	test(error==KErrNone);
   268 	delete trapCleanup;
   269 
   270 	test.End();
   271 	test.Close();
   272 
   273 	__UHEAP_MARKEND;
   274 	return error;
   275 	}
   276