os/ossrv/genericopenlibs/cstdlib/TSTLIB/TTIME.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TTIME.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,242 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* Time handling
    1.19 +* 
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +
    1.25 +#include <stdlib.h>	/* definition of exit() */
    1.26 +#include <stdio.h>
    1.27 +#include <errno.h>
    1.28 +#include <string.h>
    1.29 +#include <time.h>
    1.30 +#include <sys/time.h>	/* timeval, gettimeofday */
    1.31 +
    1.32 +void msdev_time_example(void);
    1.33 +
    1.34 +void NEXT_TEST(const char *title)
    1.35 +    {
    1.36 +    static int first = 1;
    1.37 +    int c;
    1.38 +    if (!first)
    1.39 +	{
    1.40 +	printf("\nContinue [Y/N]? ");
    1.41 +	c = getchar();
    1.42 +	if (c == 'q' || c == 'Q' || c == 'n' || c == 'N')
    1.43 +	    {
    1.44 +	    printf("\nAbandoned.\n");
    1.45 +	    exit(-1);
    1.46 +	    }
    1.47 +	}
    1.48 +    first = 0;
    1.49 +    printf("\f\n%s\n\n", title);
    1.50 +    }
    1.51 +
    1.52 +/**
    1.53 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1080
    1.54 +@SYMTestCaseDesc	    Time handling functions test
    1.55 +@SYMTestPriority 	    High
    1.56 +@SYMTestActions  	    Tests for time handling routines
    1.57 +@SYMTestExpectedResults Test must not fail
    1.58 +@SYMREQ                 REQ0000
    1.59 +*/		
    1.60 +int main (int argc, char *argv[])
    1.61 +    {
    1.62 +    time_t now, then;
    1.63 +    struct tm *tmp, newtm;
    1.64 +    char tmpbuf[128];
    1.65 +    struct timeval tv, tv2;
    1.66 +    struct timezone tz;
    1.67 +    int err, duration, count;
    1.68 +   
    1.69 +    NEXT_TEST("What's the time, Mr Epoc32");
    1.70 +
    1.71 +    then = 0;
    1.72 +    now = time(0);
    1.73 +    err = gettimeofday(&tv, &tz);
    1.74 +
    1.75 +    printf("time_t 0     = %9ld = %s\n", then, ctime(&then));
    1.76 +    printf("time now     = %9ld = %s", now, ctime(&now));
    1.77 +
    1.78 +    if (err < 0)
    1.79 +	printf("gettimeofday failed\n");
    1.80 +    else 
    1.81 +	{
    1.82 +	printf("gettimeofday = %9ld + %d usecs\n\n", tv.tv_sec, tv.tv_usec);
    1.83 +	printf("timezone     = %d mins west of GMT, DST is %s\n",
    1.84 +	    tz.tz_minuteswest, tz.tz_dsttime? "ON":"OFF");
    1.85 +	}
    1.86 +
    1.87 +    
    1.88 +    NEXT_TEST("Cost of getttimeofday");
    1.89 +
    1.90 +    count = 0;
    1.91 +    gettimeofday(&tv, &tz);
    1.92 +
    1.93 +    /* Wait for first usec transition */
    1.94 +    err = gettimeofday(&tv2, &tz);
    1.95 +    while (err == 0 && tv2.tv_sec == tv.tv_sec && tv2.tv_usec == tv.tv_usec)
    1.96 +		{
    1.97 +		err = gettimeofday(&tv, &tz);
    1.98 +		}
    1.99 +
   1.100 +    /* Now wait for second transition */
   1.101 +    err = gettimeofday(&tv2, &tz);
   1.102 +    while (err == 0 && tv2.tv_sec == tv.tv_sec &&tv2.tv_usec == tv.tv_usec)
   1.103 +	{
   1.104 +	err = gettimeofday(&tv2, &tz);
   1.105 +	count+=1;
   1.106 +	}
   1.107 +
   1.108 +    if (err < 0)
   1.109 +	printf("gettimeofday failed\n");
   1.110 +    else
   1.111 +	{
   1.112 +	duration = 1000000 *(tv2.tv_sec - tv.tv_sec) + tv2.tv_usec - tv.tv_usec;
   1.113 +	if (tv2.tv_usec < tv.tv_usec)
   1.114 +	    duration += 1000000;
   1.115 +	printf("smallest usec delta = %d usecs\n", duration);
   1.116 +	printf("gettimeofday took %d usecs for %d calls = %d usecs\n",
   1.117 +	    duration, count, duration/count);
   1.118 +	}
   1.119 +
   1.120 +
   1.121 +    NEXT_TEST("localtime: Conversion to struct tm");
   1.122 +    
   1.123 +    tmp = localtime(&now);
   1.124 +    printf("time now = %9ld = %s", now, ctime(&now));
   1.125 +    printf("tm now   = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
   1.126 +	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
   1.127 +	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
   1.128 +	tmp->tm_wday, tmp->tm_yday);
   1.129 +
   1.130 +    
   1.131 +    NEXT_TEST("mktime: Conversion from struct tm");
   1.132 +    
   1.133 +    /* Example derived from MSDEV mktime documentation */
   1.134 +
   1.135 +    newtm.tm_mday = 03;
   1.136 +    newtm.tm_mon  = 04;
   1.137 +    newtm.tm_year = 94;
   1.138 +    newtm.tm_hour = 12;
   1.139 +    newtm.tm_min  = 45;
   1.140 +    newtm.tm_sec  = 47;
   1.141 +    newtm.tm_wday = 9999;
   1.142 +    newtm.tm_yday = 9999;
   1.143 +
   1.144 +    tmp = &newtm;
   1.145 +    printf("tm before = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
   1.146 +	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
   1.147 +	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
   1.148 +	tmp->tm_wday, tmp->tm_yday);
   1.149 +    then = mktime(tmp);
   1.150 +    printf("tm after  = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
   1.151 +	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
   1.152 +	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
   1.153 +	tmp->tm_wday, tmp->tm_yday);
   1.154 +    printf("then      = %ld = %s", then, ctime(&then));
   1.155 +
   1.156 +    printf("\nAdd 29 days...(Wed Jun 01)\n\n");
   1.157 +    newtm.tm_mday += 29;
   1.158 +    then = mktime(tmp);
   1.159 +    printf("tm after  = %02d/%02d/%04d %02d:%02d.%02d, wday=%d, dayno=%d\n",
   1.160 +	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year+1900,
   1.161 +	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
   1.162 +	tmp->tm_wday, tmp->tm_yday);
   1.163 +    printf("then      = %ld = %s", then, ctime(&then));
   1.164 +
   1.165 +
   1.166 +    NEXT_TEST("strftime: customised string conversion");
   1.167 +    
   1.168 +    tmp = localtime( &now );
   1.169 +    strftime( tmpbuf, 128,
   1.170 +         "Today is %A, day %d of %B in the year %Y.\n", tmp );
   1.171 +    printf("time now = %9ld = %s", now, ctime(&now));
   1.172 +    printf( tmpbuf );
   1.173 +
   1.174 +    
   1.175 +    NEXT_TEST("Example code from MSDEV documentation");
   1.176 +
   1.177 +    msdev_time_example();
   1.178 +
   1.179 +    return 0;
   1.180 +   }
   1.181 +
   1.182 +/* 
   1.183 +Example code derived from MSDEV documentation on time() function
   1.184 +  
   1.185 +Time in seconds since UTC 1/1/70:       768027063
   1.186 +UNIX time and date:                     Tue May 03 21:51:03 1994
   1.187 +Coordinated universal time:             Wed May 04 04:51:03 1994
   1.188 +12-hour time:                           09:51:03 PM
   1.189 +Christmas                               Sat Dec 25 12:00:00 1993
   1.190 +
   1.191 +Today is Tuesday, day 03 of May in the year 1994.
   1.192 +*/
   1.193 +
   1.194 +void msdev_time_example(void)
   1.195 +{
   1.196 +    char tmpbuf[128], ampm[] = "AM";
   1.197 +    time_t ltime;
   1.198 +    struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
   1.199 +
   1.200 +    /* Get UNIX-style time and display as number and string. 
   1.201 +     * time( &ltime );	Tue May 03 21:51:03 1994
   1.202 +     */
   1.203 +    struct tm example = { 3, 51, 21, 3, 4, 94 };
   1.204 +    ltime = mktime(&example);
   1.205 +
   1.206 +    printf( "Time in seconds since UTC 1/1/70:   %ld\n", ltime );
   1.207 +    printf( "UNIX time and date:                 %s", ctime( &ltime ) );
   1.208 +
   1.209 +    /* Display UTC. */
   1.210 +    gmt = gmtime( &ltime );
   1.211 +    printf( "Coordinated universal time:         %s", asctime( gmt ) );
   1.212 +
   1.213 +    /* Convert to time structure and adjust for PM if necessary. */
   1.214 +    today = localtime( &ltime );
   1.215 +    if( today->tm_hour > 12 )
   1.216 +	{
   1.217 +	strcpy( ampm, "PM" );
   1.218 +	today->tm_hour -= 12;
   1.219 +	}
   1.220 +    if( today->tm_hour == 0 )  /* Adjust if midnight hour. */
   1.221 +	today->tm_hour = 12;
   1.222 +
   1.223 +    /* Note how pointer addition is used to skip the first 11 
   1.224 +     * characters and printf is used to trim off terminating 
   1.225 +     * characters.
   1.226 +     */
   1.227 +    printf( "12-hour time:                       %.8s %s\n",
   1.228 +       asctime( today ) + 11, ampm );
   1.229 +
   1.230 +    /* Make time for noon on Christmas, 1993. */
   1.231 +    if( mktime( &xmas ) != (time_t)-1 )
   1.232 +	printf( "Christmas                           %s\n", asctime( &xmas ) );
   1.233 +
   1.234 +    /* Use time structure to build a customized time string. */
   1.235 +    today = localtime( &ltime );
   1.236 +
   1.237 +    /* Use strftime to build a customized time string. */
   1.238 +    strftime( tmpbuf, 128,
   1.239 +	"Today is %A, day %d of %B in the year %Y.\n", today );
   1.240 +    printf( tmpbuf );
   1.241 +}
   1.242 +
   1.243 +  
   1.244 +
   1.245 +