sl@0: /*
sl@0: * Copyright (c) 1997-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: * Time handling
sl@0: * 
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: 
sl@0: #include <stdlib.h>	/* definition of exit() */
sl@0: #include <stdio.h>
sl@0: #include <errno.h>
sl@0: #include <string.h>
sl@0: #include <time.h>
sl@0: #include <sys/time.h>	/* timeval, gettimeofday */
sl@0: 
sl@0: void msdev_time_example(void);
sl@0: 
sl@0: void NEXT_TEST(const char *title)
sl@0:     {
sl@0:     static int first = 1;
sl@0:     int c;
sl@0:     if (!first)
sl@0: 	{
sl@0: 	printf("\nContinue [Y/N]? ");
sl@0: 	c = getchar();
sl@0: 	if (c == 'q' || c == 'Q' || c == 'n' || c == 'N')
sl@0: 	    {
sl@0: 	    printf("\nAbandoned.\n");
sl@0: 	    exit(-1);
sl@0: 	    }
sl@0: 	}
sl@0:     first = 0;
sl@0:     printf("\f\n%s\n\n", title);
sl@0:     }
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-STDLIB-CT-1080
sl@0: @SYMTestCaseDesc	    Time handling functions test
sl@0: @SYMTestPriority 	    High
sl@0: @SYMTestActions  	    Tests for time handling routines
sl@0: @SYMTestExpectedResults Test must not fail
sl@0: @SYMREQ                 REQ0000
sl@0: */		
sl@0: int main (int argc, char *argv[])
sl@0:     {
sl@0:     time_t now, then;
sl@0:     struct tm *tmp, newtm;
sl@0:     char tmpbuf[128];
sl@0:     struct timeval tv, tv2;
sl@0:     struct timezone tz;
sl@0:     int err, duration, count;
sl@0:    
sl@0:     NEXT_TEST("What's the time, Mr Epoc32");
sl@0: 
sl@0:     then = 0;
sl@0:     now = time(0);
sl@0:     err = gettimeofday(&tv, &tz);
sl@0: 
sl@0:     printf("time_t 0     = %9ld = %s\n", then, ctime(&then));
sl@0:     printf("time now     = %9ld = %s", now, ctime(&now));
sl@0: 
sl@0:     if (err < 0)
sl@0: 	printf("gettimeofday failed\n");
sl@0:     else 
sl@0: 	{
sl@0: 	printf("gettimeofday = %9ld + %d usecs\n\n", tv.tv_sec, tv.tv_usec);
sl@0: 	printf("timezone     = %d mins west of GMT, DST is %s\n",
sl@0: 	    tz.tz_minuteswest, tz.tz_dsttime? "ON":"OFF");
sl@0: 	}
sl@0: 
sl@0:     
sl@0:     NEXT_TEST("Cost of getttimeofday");
sl@0: 
sl@0:     count = 0;
sl@0:     gettimeofday(&tv, &tz);
sl@0: 
sl@0:     /* Wait for first usec transition */
sl@0:     err = gettimeofday(&tv2, &tz);
sl@0:     while (err == 0 && tv2.tv_sec == tv.tv_sec && tv2.tv_usec == tv.tv_usec)
sl@0: 		{
sl@0: 		err = gettimeofday(&tv, &tz);
sl@0: 		}
sl@0: 
sl@0:     /* Now wait for second transition */
sl@0:     err = gettimeofday(&tv2, &tz);
sl@0:     while (err == 0 && tv2.tv_sec == tv.tv_sec &&tv2.tv_usec == tv.tv_usec)
sl@0: 	{
sl@0: 	err = gettimeofday(&tv2, &tz);
sl@0: 	count+=1;
sl@0: 	}
sl@0: 
sl@0:     if (err < 0)
sl@0: 	printf("gettimeofday failed\n");
sl@0:     else
sl@0: 	{
sl@0: 	duration = 1000000 *(tv2.tv_sec - tv.tv_sec) + tv2.tv_usec - tv.tv_usec;
sl@0: 	if (tv2.tv_usec < tv.tv_usec)
sl@0: 	    duration += 1000000;
sl@0: 	printf("smallest usec delta = %d usecs\n", duration);
sl@0: 	printf("gettimeofday took %d usecs for %d calls = %d usecs\n",
sl@0: 	    duration, count, duration/count);
sl@0: 	}
sl@0: 
sl@0: 
sl@0:     NEXT_TEST("localtime: Conversion to struct tm");
sl@0:     
sl@0:     tmp = localtime(&now);
sl@0:     printf("time now = %9ld = %s", now, ctime(&now));
sl@0:     printf("tm now   = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
sl@0: 	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
sl@0: 	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
sl@0: 	tmp->tm_wday, tmp->tm_yday);
sl@0: 
sl@0:     
sl@0:     NEXT_TEST("mktime: Conversion from struct tm");
sl@0:     
sl@0:     /* Example derived from MSDEV mktime documentation */
sl@0: 
sl@0:     newtm.tm_mday = 03;
sl@0:     newtm.tm_mon  = 04;
sl@0:     newtm.tm_year = 94;
sl@0:     newtm.tm_hour = 12;
sl@0:     newtm.tm_min  = 45;
sl@0:     newtm.tm_sec  = 47;
sl@0:     newtm.tm_wday = 9999;
sl@0:     newtm.tm_yday = 9999;
sl@0: 
sl@0:     tmp = &newtm;
sl@0:     printf("tm before = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
sl@0: 	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
sl@0: 	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
sl@0: 	tmp->tm_wday, tmp->tm_yday);
sl@0:     then = mktime(tmp);
sl@0:     printf("tm after  = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
sl@0: 	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
sl@0: 	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
sl@0: 	tmp->tm_wday, tmp->tm_yday);
sl@0:     printf("then      = %ld = %s", then, ctime(&then));
sl@0: 
sl@0:     printf("\nAdd 29 days...(Wed Jun 01)\n\n");
sl@0:     newtm.tm_mday += 29;
sl@0:     then = mktime(tmp);
sl@0:     printf("tm after  = %02d/%02d/%04d %02d:%02d.%02d, wday=%d, dayno=%d\n",
sl@0: 	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year+1900,
sl@0: 	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
sl@0: 	tmp->tm_wday, tmp->tm_yday);
sl@0:     printf("then      = %ld = %s", then, ctime(&then));
sl@0: 
sl@0: 
sl@0:     NEXT_TEST("strftime: customised string conversion");
sl@0:     
sl@0:     tmp = localtime( &now );
sl@0:     strftime( tmpbuf, 128,
sl@0:          "Today is %A, day %d of %B in the year %Y.\n", tmp );
sl@0:     printf("time now = %9ld = %s", now, ctime(&now));
sl@0:     printf( tmpbuf );
sl@0: 
sl@0:     
sl@0:     NEXT_TEST("Example code from MSDEV documentation");
sl@0: 
sl@0:     msdev_time_example();
sl@0: 
sl@0:     return 0;
sl@0:    }
sl@0: 
sl@0: /* 
sl@0: Example code derived from MSDEV documentation on time() function
sl@0:   
sl@0: Time in seconds since UTC 1/1/70:       768027063
sl@0: UNIX time and date:                     Tue May 03 21:51:03 1994
sl@0: Coordinated universal time:             Wed May 04 04:51:03 1994
sl@0: 12-hour time:                           09:51:03 PM
sl@0: Christmas                               Sat Dec 25 12:00:00 1993
sl@0: 
sl@0: Today is Tuesday, day 03 of May in the year 1994.
sl@0: */
sl@0: 
sl@0: void msdev_time_example(void)
sl@0: {
sl@0:     char tmpbuf[128], ampm[] = "AM";
sl@0:     time_t ltime;
sl@0:     struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
sl@0: 
sl@0:     /* Get UNIX-style time and display as number and string. 
sl@0:      * time( &ltime );	Tue May 03 21:51:03 1994
sl@0:      */
sl@0:     struct tm example = { 3, 51, 21, 3, 4, 94 };
sl@0:     ltime = mktime(&example);
sl@0: 
sl@0:     printf( "Time in seconds since UTC 1/1/70:   %ld\n", ltime );
sl@0:     printf( "UNIX time and date:                 %s", ctime( &ltime ) );
sl@0: 
sl@0:     /* Display UTC. */
sl@0:     gmt = gmtime( &ltime );
sl@0:     printf( "Coordinated universal time:         %s", asctime( gmt ) );
sl@0: 
sl@0:     /* Convert to time structure and adjust for PM if necessary. */
sl@0:     today = localtime( &ltime );
sl@0:     if( today->tm_hour > 12 )
sl@0: 	{
sl@0: 	strcpy( ampm, "PM" );
sl@0: 	today->tm_hour -= 12;
sl@0: 	}
sl@0:     if( today->tm_hour == 0 )  /* Adjust if midnight hour. */
sl@0: 	today->tm_hour = 12;
sl@0: 
sl@0:     /* Note how pointer addition is used to skip the first 11 
sl@0:      * characters and printf is used to trim off terminating 
sl@0:      * characters.
sl@0:      */
sl@0:     printf( "12-hour time:                       %.8s %s\n",
sl@0:        asctime( today ) + 11, ampm );
sl@0: 
sl@0:     /* Make time for noon on Christmas, 1993. */
sl@0:     if( mktime( &xmas ) != (time_t)-1 )
sl@0: 	printf( "Christmas                           %s\n", asctime( &xmas ) );
sl@0: 
sl@0:     /* Use time structure to build a customized time string. */
sl@0:     today = localtime( &ltime );
sl@0: 
sl@0:     /* Use strftime to build a customized time string. */
sl@0:     strftime( tmpbuf, 128,
sl@0: 	"Today is %A, day %d of %B in the year %Y.\n", today );
sl@0:     printf( tmpbuf );
sl@0: }
sl@0: 
sl@0:   
sl@0: 
sl@0: