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.
sl@0
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Simple STDLIB tests.
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32test.h>
sl@0
    19
#include <e32svr.h>
sl@0
    20
#include <sys/time.h>
sl@0
    21
#include <time.h>
sl@0
    22
#include <sys/reent.h>
sl@0
    23
#include <tz.h>
sl@0
    24
sl@0
    25
sl@0
    26
//CPP file is used for C tests, because by default any console opened from a C file
sl@0
    27
//expects a key to be pressed when it is about to be closed. That makes impossible
sl@0
    28
//the creation of automated C tests.
sl@0
    29
sl@0
    30
//
sl@0
    31
// Globals
sl@0
    32
sl@0
    33
LOCAL_D RTest test(_L("TTime"));
sl@0
    34
_LIT16(priorUnixTime,"19700000:000000.000000"); //0 AD to the start of Unix Time
sl@0
    35
sl@0
    36
//
sl@0
    37
// Tests
sl@0
    38
sl@0
    39
/**
sl@0
    40
@file
sl@0
    41
@SYMTestCaseID		SYSLIB-STDLIB-CT-0143
sl@0
    42
@SYMTestCaseDesc 	Check that gettimeofday() returns universaltime, rather than the local time. 
sl@0
    43
@SYMTestPriority 	low
sl@0
    44
@SYMTestActions  	retrieve return value of gettimeofday() and compare with preset universaltime
sl@0
    45
@SYMTestExpectedResults The test must not fail.
sl@0
    46
@SYMPREQ PREQ234
sl@0
    47
*/
sl@0
    48
void Testgettimeofday()
sl@0
    49
	{
sl@0
    50
	test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0143 "));
sl@0
    51
	test.Printf(_L("\ntesting gettimeofday()...\n"));
sl@0
    52
	RTz tz;
sl@0
    53
	TInt error=tz.Connect();
sl@0
    54
	test(error==KErrNone);
sl@0
    55
	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
sl@0
    56
	CleanupStack::PushL(tzId);
sl@0
    57
	tz.SetTimeZoneL(*tzId);
sl@0
    58
	
sl@0
    59
	struct timeval tv;
sl@0
    60
	struct timezone tzone;
sl@0
    61
	TTime t,unix;
sl@0
    62
	unix.Set(priorUnixTime);
sl@0
    63
	
sl@0
    64
	test.Printf(_L("tests during summertime (dst on)...\t"));	
sl@0
    65
	//set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
sl@0
    66
	TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0))); 
sl@0
    67
	test(err==0);
sl@0
    68
	t.UniversalTime();
sl@0
    69
	err = gettimeofday(&tv, &tzone);
sl@0
    70
	test(err==0);
sl@0
    71
	TTimeIntervalSeconds s = (User::UTCOffset().Int())/60;
sl@0
    72
	test(tzone.tz_minuteswest==s.Int());
sl@0
    73
	test(tzone.tz_dsttime == 0);
sl@0
    74
	// Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns 
sl@0
    75
	// seconds from 1970 to now,
sl@0
    76
	TInt64 sec = tv.tv_sec;
sl@0
    77
	TUint64 microSec = (sec*1000000) + tv.tv_usec + unix.Int64();
sl@0
    78
    test.Printf(_L("Expected Time: %ld\tReceived Time: %ld\n"),t.Int64(),microSec);
sl@0
    79
	test((microSec-t.Int64())<1000000);//allowing a 1 sec delay in time
sl@0
    80
	test.Printf(_L("-OK\n"));
sl@0
    81
	
sl@0
    82
	test.Printf(_L("tests during wintertime (dst off)...\t"));	
sl@0
    83
	//set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date
sl@0
    84
	err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0))); 
sl@0
    85
	test(err==0);
sl@0
    86
	t.UniversalTime();
sl@0
    87
	err = gettimeofday(&tv, &tzone);
sl@0
    88
	test(err==0);
sl@0
    89
	// Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns 
sl@0
    90
	// seconds from 1970 to now,
sl@0
    91
	sec = tv.tv_sec;
sl@0
    92
	microSec = (sec*1000000) + tv.tv_usec + unix.Int64();
sl@0
    93
	test((microSec-t.Int64())<1000000);//allowing a 1 sec delay in time
sl@0
    94
	test.Printf(_L("-OK\n"));
sl@0
    95
	//
sl@0
    96
	CleanupStack::PopAndDestroy(tzId);
sl@0
    97
	tz.Close();
sl@0
    98
	}
sl@0
    99
sl@0
   100
/**
sl@0
   101
@file
sl@0
   102
@SYMTestCaseID		SYSLIB-STDLIB-CT-0144
sl@0
   103
@SYMTestCaseDesc 	Check that time() returns universaltime, rather than the local time. 
sl@0
   104
@SYMTestPriority 	low
sl@0
   105
@SYMTestActions  	retrieve return value of time() and compare with preset universaltime
sl@0
   106
@SYMTestExpectedResults The test must not fail.
sl@0
   107
@SYMPREQ PREQ234
sl@0
   108
*/
sl@0
   109
void Testtime()
sl@0
   110
	{
sl@0
   111
	test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0144 \ntesting time()...\n "));
sl@0
   112
	TInt r = KErrNone;
sl@0
   113
    RTz tz;
sl@0
   114
    r = tz.Connect();
sl@0
   115
    if (r != KErrNone)
sl@0
   116
        {
sl@0
   117
            User::Leave(r);
sl@0
   118
        }
sl@0
   119
	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
sl@0
   120
	CleanupStack::PushL(tzId);
sl@0
   121
	tz.SetTimeZoneL(*tzId);
sl@0
   122
	TTime t,unix;
sl@0
   123
	unix.Set(priorUnixTime);
sl@0
   124
	
sl@0
   125
	test.Printf(_L("tests during summertime (dst on)...\t"));
sl@0
   126
	//set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
sl@0
   127
	TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0)));
sl@0
   128
	test(err==0);
sl@0
   129
	t.UniversalTime(); 	
sl@0
   130
	time_t res = time(0) * 1000000; // current time
sl@0
   131
	// As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now,
sl@0
   132
	// the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds
sl@0
   133
	TInt64 sec = t.Int64() - unix.Int64(); 
sl@0
   134
	test((res-sec)<1000000);//allowing 1 sec delay in time
sl@0
   135
	test.Printf(_L("- OK!\n"));
sl@0
   136
	
sl@0
   137
	test.Printf(_L("tests during wintertime (dst off)...\t"));
sl@0
   138
	//set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date
sl@0
   139
	err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0)));
sl@0
   140
	test(err==0);
sl@0
   141
	t.UniversalTime(); 	
sl@0
   142
	res = time(0) * 1000000; // current time
sl@0
   143
	// As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now,
sl@0
   144
	// the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds
sl@0
   145
	sec = t.Int64() - unix.Int64(); 
sl@0
   146
	test((res-sec)<1000000);//allowing 1 sec delay in time
sl@0
   147
	test.Printf(_L("- OK!\n"));
sl@0
   148
	
sl@0
   149
	//
sl@0
   150
	CleanupStack::PopAndDestroy(tzId);
sl@0
   151
	tz.Close();	
sl@0
   152
	}
sl@0
   153
sl@0
   154
/**
sl@0
   155
@file
sl@0
   156
@SYMTestCaseID		SYSLIB-STDLIB-CT-0145
sl@0
   157
@SYMTestCaseDesc 	Check that toLocal() converts into correct localtime
sl@0
   158
@SYMTestPriority 	low
sl@0
   159
@SYMTestActions  	With the Timezone set to Europe/London, a universaltime is passed to 
sl@0
   160
the function localtime (as toLocal cannot be accessed directly) which is expected to return a hometime, with DST on
sl@0
   161
@SYMTestExpectedResults The test must not fail.
sl@0
   162
@SYMPREQ PREQ234
sl@0
   163
*/
sl@0
   164
void TesttoLocal()
sl@0
   165
	{
sl@0
   166
	test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0145  \ntesting toLocal()...\n "));
sl@0
   167
	
sl@0
   168
	//test when dst is on...
sl@0
   169
	test.Printf(_L("tests during summertime (dst on)...\t"));
sl@0
   170
	
sl@0
   171
	TInt rt = KErrNone;
sl@0
   172
	TInt rz = KErrNone;
sl@0
   173
	
sl@0
   174
	//set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date
sl@0
   175
	TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0)));
sl@0
   176
	test(err==0);
sl@0
   177
	RTz tz;
sl@0
   178
    rt = tz.Connect();
sl@0
   179
    if (rt != KErrNone)
sl@0
   180
        {
sl@0
   181
            User::Leave(rt);
sl@0
   182
        }
sl@0
   183
	CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London
sl@0
   184
	CleanupStack::PushL(tzId);
sl@0
   185
	tz.SetTimeZoneL(*tzId);
sl@0
   186
	CleanupStack::PopAndDestroy(tzId);
sl@0
   187
	tz.Close();
sl@0
   188
	
sl@0
   189
	struct tm *ptr, *ptr2;
sl@0
   190
	struct tm setup;
sl@0
   191
	time_t seconds;
sl@0
   192
	
sl@0
   193
	setup.tm_hour = 8; //8 o'clock utc time
sl@0
   194
	setup.tm_min = 55; 
sl@0
   195
	setup.tm_sec = 0; 
sl@0
   196
	setup.tm_mday = 1;
sl@0
   197
	setup.tm_mon = 3;
sl@0
   198
	setup.tm_year = 105;
sl@0
   199
	seconds = mktime(&setup);
sl@0
   200
	
sl@0
   201
	ptr2 = gmtime(&seconds); //for a quick routine test
sl@0
   202
	test(ptr2->tm_hour == 8);//
sl@0
   203
	
sl@0
   204
	ptr = localtime(&seconds);	
sl@0
   205
	test(ptr->tm_hour == 9); //test against hometime hour: 9;
sl@0
   206
	test(ptr->tm_min == 55);
sl@0
   207
	test(ptr->tm_sec == 0);
sl@0
   208
	test(ptr->tm_mday == 1);
sl@0
   209
	test(ptr->tm_mon == 3);
sl@0
   210
	test(ptr->tm_year == 105);
sl@0
   211
	test.Printf(_L("Time:9:55 -correct!\n"));
sl@0
   212
	
sl@0
   213
	//test when DST is off
sl@0
   214
	test.Printf(_L("tests during wintertime (dst off)...\t"));
sl@0
   215
	err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0)));
sl@0
   216
	test(err==0);
sl@0
   217
	
sl@0
   218
    rz = tz.Connect();
sl@0
   219
    if (rz != KErrNone)
sl@0
   220
        {
sl@0
   221
            User::Leave(rz);
sl@0
   222
        }
sl@0
   223
    
sl@0
   224
	CTzId* tzId2 = CTzId::NewL(2592); //set the timezone to Europe/London
sl@0
   225
	CleanupStack::PushL(tzId2);
sl@0
   226
	tz.SetTimeZoneL(*tzId2);
sl@0
   227
	CleanupStack::PopAndDestroy(tzId2);
sl@0
   228
	tz.Close();
sl@0
   229
	
sl@0
   230
	ptr2 = gmtime(&seconds); //for a quick routine test
sl@0
   231
	test(ptr2->tm_hour == 8);//
sl@0
   232
	
sl@0
   233
	ptr = localtime(&seconds);	
sl@0
   234
	test(ptr->tm_hour == 8); //test against hometime hour: 8;
sl@0
   235
	test(ptr->tm_min == 55);
sl@0
   236
	test(ptr->tm_sec == 0);
sl@0
   237
	test(ptr->tm_mday == 1);
sl@0
   238
	test(ptr->tm_mon == 3);
sl@0
   239
	test(ptr->tm_year == 105);
sl@0
   240
	test.Printf(_L("Time:8:55 -correct!\n"));
sl@0
   241
	//
sl@0
   242
	CloseSTDLIB();	
sl@0
   243
	}
sl@0
   244
sl@0
   245
//
sl@0
   246
//
sl@0
   247
sl@0
   248
LOCAL_C void DoTestsL()
sl@0
   249
	{
sl@0
   250
	TRAPD(err,Testgettimeofday());
sl@0
   251
	test(err==KErrNone);
sl@0
   252
	TRAP(err,Testtime());
sl@0
   253
	test(err==KErrNone);
sl@0
   254
	TRAP(err,TesttoLocal());
sl@0
   255
	test(err==KErrNone);
sl@0
   256
	}
sl@0
   257
sl@0
   258
GLDEF_C TInt E32Main()
sl@0
   259
	{
sl@0
   260
	__UHEAP_MARK;
sl@0
   261
sl@0
   262
	test.Title();
sl@0
   263
	test.Start(_L("Time & Date Tests..."));
sl@0
   264
sl@0
   265
	CTrapCleanup* trapCleanup=CTrapCleanup::New();
sl@0
   266
	TRAPD(error, DoTestsL());
sl@0
   267
	test(error==KErrNone);
sl@0
   268
	delete trapCleanup;
sl@0
   269
sl@0
   270
	test.End();
sl@0
   271
	test.Close();
sl@0
   272
sl@0
   273
	__UHEAP_MARKEND;
sl@0
   274
	return error;
sl@0
   275
	}
sl@0
   276