sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Simple STDLIB tests. sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: //CPP file is used for C tests, because by default any console opened from a C file sl@0: //expects a key to be pressed when it is about to be closed. That makes impossible sl@0: //the creation of automated C tests. sl@0: sl@0: // sl@0: // Globals sl@0: sl@0: LOCAL_D RTest test(_L("TTime")); sl@0: _LIT16(priorUnixTime,"19700000:000000.000000"); //0 AD to the start of Unix Time sl@0: sl@0: // sl@0: // Tests sl@0: sl@0: /** sl@0: @file sl@0: @SYMTestCaseID SYSLIB-STDLIB-CT-0143 sl@0: @SYMTestCaseDesc Check that gettimeofday() returns universaltime, rather than the local time. sl@0: @SYMTestPriority low sl@0: @SYMTestActions retrieve return value of gettimeofday() and compare with preset universaltime sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMPREQ PREQ234 sl@0: */ sl@0: void Testgettimeofday() sl@0: { sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0143 ")); sl@0: test.Printf(_L("\ntesting gettimeofday()...\n")); sl@0: RTz tz; sl@0: TInt error=tz.Connect(); sl@0: test(error==KErrNone); sl@0: CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London sl@0: CleanupStack::PushL(tzId); sl@0: tz.SetTimeZoneL(*tzId); sl@0: sl@0: struct timeval tv; sl@0: struct timezone tzone; sl@0: TTime t,unix; sl@0: unix.Set(priorUnixTime); sl@0: sl@0: test.Printf(_L("tests during summertime (dst on)...\t")); sl@0: //set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date sl@0: TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0))); sl@0: test(err==0); sl@0: t.UniversalTime(); sl@0: err = gettimeofday(&tv, &tzone); sl@0: test(err==0); sl@0: TTimeIntervalSeconds s = (User::UTCOffset().Int())/60; sl@0: test(tzone.tz_minuteswest==s.Int()); sl@0: test(tzone.tz_dsttime == 0); sl@0: // Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns sl@0: // seconds from 1970 to now, sl@0: TInt64 sec = tv.tv_sec; sl@0: TUint64 microSec = (sec*1000000) + tv.tv_usec + unix.Int64(); sl@0: test.Printf(_L("Expected Time: %ld\tReceived Time: %ld\n"),t.Int64(),microSec); sl@0: test((microSec-t.Int64())<1000000);//allowing a 1 sec delay in time sl@0: test.Printf(_L("-OK\n")); sl@0: sl@0: test.Printf(_L("tests during wintertime (dst off)...\t")); sl@0: //set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date sl@0: err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0))); sl@0: test(err==0); sl@0: t.UniversalTime(); sl@0: err = gettimeofday(&tv, &tzone); sl@0: test(err==0); sl@0: // Conversion needed as TTime returns micro seconds from 0AD to now, and gettimeofday() returns sl@0: // seconds from 1970 to now, sl@0: sec = tv.tv_sec; sl@0: microSec = (sec*1000000) + tv.tv_usec + unix.Int64(); sl@0: test((microSec-t.Int64())<1000000);//allowing a 1 sec delay in time sl@0: test.Printf(_L("-OK\n")); sl@0: // sl@0: CleanupStack::PopAndDestroy(tzId); sl@0: tz.Close(); sl@0: } sl@0: sl@0: /** sl@0: @file sl@0: @SYMTestCaseID SYSLIB-STDLIB-CT-0144 sl@0: @SYMTestCaseDesc Check that time() returns universaltime, rather than the local time. sl@0: @SYMTestPriority low sl@0: @SYMTestActions retrieve return value of time() and compare with preset universaltime sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMPREQ PREQ234 sl@0: */ sl@0: void Testtime() sl@0: { sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0144 \ntesting time()...\n ")); sl@0: TInt r = KErrNone; sl@0: RTz tz; sl@0: r = tz.Connect(); sl@0: if (r != KErrNone) sl@0: { sl@0: User::Leave(r); sl@0: } sl@0: CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London sl@0: CleanupStack::PushL(tzId); sl@0: tz.SetTimeZoneL(*tzId); sl@0: TTime t,unix; sl@0: unix.Set(priorUnixTime); sl@0: sl@0: test.Printf(_L("tests during summertime (dst on)...\t")); sl@0: //set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date sl@0: TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0))); sl@0: test(err==0); sl@0: t.UniversalTime(); sl@0: time_t res = time(0) * 1000000; // current time sl@0: // As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now, sl@0: // the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds sl@0: TInt64 sec = t.Int64() - unix.Int64(); sl@0: test((res-sec)<1000000);//allowing 1 sec delay in time sl@0: test.Printf(_L("- OK!\n")); sl@0: sl@0: test.Printf(_L("tests during wintertime (dst off)...\t")); sl@0: //set the utc time to 8.55am, 15 January 2005 -Daylight savings DON'T apply on this date sl@0: err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0))); sl@0: test(err==0); sl@0: t.UniversalTime(); sl@0: res = time(0) * 1000000; // current time sl@0: // As TTime returns micro seconds from 0AD to now, and time() returns seconds from 1970 to now, sl@0: // the start date of t needs to be changed to 1 Jan 1970 midnight and converted into seconds sl@0: sec = t.Int64() - unix.Int64(); sl@0: test((res-sec)<1000000);//allowing 1 sec delay in time sl@0: test.Printf(_L("- OK!\n")); sl@0: sl@0: // sl@0: CleanupStack::PopAndDestroy(tzId); sl@0: tz.Close(); sl@0: } sl@0: sl@0: /** sl@0: @file sl@0: @SYMTestCaseID SYSLIB-STDLIB-CT-0145 sl@0: @SYMTestCaseDesc Check that toLocal() converts into correct localtime sl@0: @SYMTestPriority low sl@0: @SYMTestActions With the Timezone set to Europe/London, a universaltime is passed to sl@0: the function localtime (as toLocal cannot be accessed directly) which is expected to return a hometime, with DST on sl@0: @SYMTestExpectedResults The test must not fail. sl@0: @SYMPREQ PREQ234 sl@0: */ sl@0: void TesttoLocal() sl@0: { sl@0: test.Next(_L(" @SYMTestCaseID:SYSLIB-STDLIB-CT-0145 \ntesting toLocal()...\n ")); sl@0: sl@0: //test when dst is on... sl@0: test.Printf(_L("tests during summertime (dst on)...\t")); sl@0: sl@0: TInt rt = KErrNone; sl@0: TInt rz = KErrNone; sl@0: sl@0: //set the utc time to 8.55am, 15 May 2005 -Daylight savings apply on this date sl@0: TInt err=User::SetUTCTime(TTime(TDateTime(2005, EMay, 15, 8, 55, 0, 0))); sl@0: test(err==0); sl@0: RTz tz; sl@0: rt = tz.Connect(); sl@0: if (rt != KErrNone) sl@0: { sl@0: User::Leave(rt); sl@0: } sl@0: CTzId* tzId = CTzId::NewL(2592); //set the timezone to Europe/London sl@0: CleanupStack::PushL(tzId); sl@0: tz.SetTimeZoneL(*tzId); sl@0: CleanupStack::PopAndDestroy(tzId); sl@0: tz.Close(); sl@0: sl@0: struct tm *ptr, *ptr2; sl@0: struct tm setup; sl@0: time_t seconds; sl@0: sl@0: setup.tm_hour = 8; //8 o'clock utc time sl@0: setup.tm_min = 55; sl@0: setup.tm_sec = 0; sl@0: setup.tm_mday = 1; sl@0: setup.tm_mon = 3; sl@0: setup.tm_year = 105; sl@0: seconds = mktime(&setup); sl@0: sl@0: ptr2 = gmtime(&seconds); //for a quick routine test sl@0: test(ptr2->tm_hour == 8);// sl@0: sl@0: ptr = localtime(&seconds); sl@0: test(ptr->tm_hour == 9); //test against hometime hour: 9; sl@0: test(ptr->tm_min == 55); sl@0: test(ptr->tm_sec == 0); sl@0: test(ptr->tm_mday == 1); sl@0: test(ptr->tm_mon == 3); sl@0: test(ptr->tm_year == 105); sl@0: test.Printf(_L("Time:9:55 -correct!\n")); sl@0: sl@0: //test when DST is off sl@0: test.Printf(_L("tests during wintertime (dst off)...\t")); sl@0: err=User::SetUTCTime(TTime(TDateTime(2005, EJanuary, 15, 8, 55, 0, 0))); sl@0: test(err==0); sl@0: sl@0: rz = tz.Connect(); sl@0: if (rz != KErrNone) sl@0: { sl@0: User::Leave(rz); sl@0: } sl@0: sl@0: CTzId* tzId2 = CTzId::NewL(2592); //set the timezone to Europe/London sl@0: CleanupStack::PushL(tzId2); sl@0: tz.SetTimeZoneL(*tzId2); sl@0: CleanupStack::PopAndDestroy(tzId2); sl@0: tz.Close(); sl@0: sl@0: ptr2 = gmtime(&seconds); //for a quick routine test sl@0: test(ptr2->tm_hour == 8);// sl@0: sl@0: ptr = localtime(&seconds); sl@0: test(ptr->tm_hour == 8); //test against hometime hour: 8; sl@0: test(ptr->tm_min == 55); sl@0: test(ptr->tm_sec == 0); sl@0: test(ptr->tm_mday == 1); sl@0: test(ptr->tm_mon == 3); sl@0: test(ptr->tm_year == 105); sl@0: test.Printf(_L("Time:8:55 -correct!\n")); sl@0: // sl@0: CloseSTDLIB(); sl@0: } sl@0: sl@0: // sl@0: // sl@0: sl@0: LOCAL_C void DoTestsL() sl@0: { sl@0: TRAPD(err,Testgettimeofday()); sl@0: test(err==KErrNone); sl@0: TRAP(err,Testtime()); sl@0: test(err==KErrNone); sl@0: TRAP(err,TesttoLocal()); sl@0: test(err==KErrNone); sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: test.Title(); sl@0: test.Start(_L("Time & Date Tests...")); sl@0: sl@0: CTrapCleanup* trapCleanup=CTrapCleanup::New(); sl@0: TRAPD(error, DoTestsL()); sl@0: test(error==KErrNone); sl@0: delete trapCleanup; sl@0: sl@0: test.End(); sl@0: test.Close(); sl@0: sl@0: __UHEAP_MARKEND; sl@0: return error; sl@0: } sl@0: