os/ossrv/genericopenlibs/cstdlib/TSTLIB/TTIME.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 * Time handling
    16 * 
    17 *
    18 */
    19 
    20 
    21 
    22 #include <stdlib.h>	/* definition of exit() */
    23 #include <stdio.h>
    24 #include <errno.h>
    25 #include <string.h>
    26 #include <time.h>
    27 #include <sys/time.h>	/* timeval, gettimeofday */
    28 
    29 void msdev_time_example(void);
    30 
    31 void NEXT_TEST(const char *title)
    32     {
    33     static int first = 1;
    34     int c;
    35     if (!first)
    36 	{
    37 	printf("\nContinue [Y/N]? ");
    38 	c = getchar();
    39 	if (c == 'q' || c == 'Q' || c == 'n' || c == 'N')
    40 	    {
    41 	    printf("\nAbandoned.\n");
    42 	    exit(-1);
    43 	    }
    44 	}
    45     first = 0;
    46     printf("\f\n%s\n\n", title);
    47     }
    48 
    49 /**
    50 @SYMTestCaseID          SYSLIB-STDLIB-CT-1080
    51 @SYMTestCaseDesc	    Time handling functions test
    52 @SYMTestPriority 	    High
    53 @SYMTestActions  	    Tests for time handling routines
    54 @SYMTestExpectedResults Test must not fail
    55 @SYMREQ                 REQ0000
    56 */		
    57 int main (int argc, char *argv[])
    58     {
    59     time_t now, then;
    60     struct tm *tmp, newtm;
    61     char tmpbuf[128];
    62     struct timeval tv, tv2;
    63     struct timezone tz;
    64     int err, duration, count;
    65    
    66     NEXT_TEST("What's the time, Mr Epoc32");
    67 
    68     then = 0;
    69     now = time(0);
    70     err = gettimeofday(&tv, &tz);
    71 
    72     printf("time_t 0     = %9ld = %s\n", then, ctime(&then));
    73     printf("time now     = %9ld = %s", now, ctime(&now));
    74 
    75     if (err < 0)
    76 	printf("gettimeofday failed\n");
    77     else 
    78 	{
    79 	printf("gettimeofday = %9ld + %d usecs\n\n", tv.tv_sec, tv.tv_usec);
    80 	printf("timezone     = %d mins west of GMT, DST is %s\n",
    81 	    tz.tz_minuteswest, tz.tz_dsttime? "ON":"OFF");
    82 	}
    83 
    84     
    85     NEXT_TEST("Cost of getttimeofday");
    86 
    87     count = 0;
    88     gettimeofday(&tv, &tz);
    89 
    90     /* Wait for first usec transition */
    91     err = gettimeofday(&tv2, &tz);
    92     while (err == 0 && tv2.tv_sec == tv.tv_sec && tv2.tv_usec == tv.tv_usec)
    93 		{
    94 		err = gettimeofday(&tv, &tz);
    95 		}
    96 
    97     /* Now wait for second transition */
    98     err = gettimeofday(&tv2, &tz);
    99     while (err == 0 && tv2.tv_sec == tv.tv_sec &&tv2.tv_usec == tv.tv_usec)
   100 	{
   101 	err = gettimeofday(&tv2, &tz);
   102 	count+=1;
   103 	}
   104 
   105     if (err < 0)
   106 	printf("gettimeofday failed\n");
   107     else
   108 	{
   109 	duration = 1000000 *(tv2.tv_sec - tv.tv_sec) + tv2.tv_usec - tv.tv_usec;
   110 	if (tv2.tv_usec < tv.tv_usec)
   111 	    duration += 1000000;
   112 	printf("smallest usec delta = %d usecs\n", duration);
   113 	printf("gettimeofday took %d usecs for %d calls = %d usecs\n",
   114 	    duration, count, duration/count);
   115 	}
   116 
   117 
   118     NEXT_TEST("localtime: Conversion to struct tm");
   119     
   120     tmp = localtime(&now);
   121     printf("time now = %9ld = %s", now, ctime(&now));
   122     printf("tm now   = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
   123 	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
   124 	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
   125 	tmp->tm_wday, tmp->tm_yday);
   126 
   127     
   128     NEXT_TEST("mktime: Conversion from struct tm");
   129     
   130     /* Example derived from MSDEV mktime documentation */
   131 
   132     newtm.tm_mday = 03;
   133     newtm.tm_mon  = 04;
   134     newtm.tm_year = 94;
   135     newtm.tm_hour = 12;
   136     newtm.tm_min  = 45;
   137     newtm.tm_sec  = 47;
   138     newtm.tm_wday = 9999;
   139     newtm.tm_yday = 9999;
   140 
   141     tmp = &newtm;
   142     printf("tm before = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
   143 	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
   144 	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
   145 	tmp->tm_wday, tmp->tm_yday);
   146     then = mktime(tmp);
   147     printf("tm after  = %02d/%02d/%02d %02d:%02d.%02d, wday=%d, dayno=%d\n",
   148 	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year,
   149 	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
   150 	tmp->tm_wday, tmp->tm_yday);
   151     printf("then      = %ld = %s", then, ctime(&then));
   152 
   153     printf("\nAdd 29 days...(Wed Jun 01)\n\n");
   154     newtm.tm_mday += 29;
   155     then = mktime(tmp);
   156     printf("tm after  = %02d/%02d/%04d %02d:%02d.%02d, wday=%d, dayno=%d\n",
   157 	tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year+1900,
   158 	tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
   159 	tmp->tm_wday, tmp->tm_yday);
   160     printf("then      = %ld = %s", then, ctime(&then));
   161 
   162 
   163     NEXT_TEST("strftime: customised string conversion");
   164     
   165     tmp = localtime( &now );
   166     strftime( tmpbuf, 128,
   167          "Today is %A, day %d of %B in the year %Y.\n", tmp );
   168     printf("time now = %9ld = %s", now, ctime(&now));
   169     printf( tmpbuf );
   170 
   171     
   172     NEXT_TEST("Example code from MSDEV documentation");
   173 
   174     msdev_time_example();
   175 
   176     return 0;
   177    }
   178 
   179 /* 
   180 Example code derived from MSDEV documentation on time() function
   181   
   182 Time in seconds since UTC 1/1/70:       768027063
   183 UNIX time and date:                     Tue May 03 21:51:03 1994
   184 Coordinated universal time:             Wed May 04 04:51:03 1994
   185 12-hour time:                           09:51:03 PM
   186 Christmas                               Sat Dec 25 12:00:00 1993
   187 
   188 Today is Tuesday, day 03 of May in the year 1994.
   189 */
   190 
   191 void msdev_time_example(void)
   192 {
   193     char tmpbuf[128], ampm[] = "AM";
   194     time_t ltime;
   195     struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
   196 
   197     /* Get UNIX-style time and display as number and string. 
   198      * time( &ltime );	Tue May 03 21:51:03 1994
   199      */
   200     struct tm example = { 3, 51, 21, 3, 4, 94 };
   201     ltime = mktime(&example);
   202 
   203     printf( "Time in seconds since UTC 1/1/70:   %ld\n", ltime );
   204     printf( "UNIX time and date:                 %s", ctime( &ltime ) );
   205 
   206     /* Display UTC. */
   207     gmt = gmtime( &ltime );
   208     printf( "Coordinated universal time:         %s", asctime( gmt ) );
   209 
   210     /* Convert to time structure and adjust for PM if necessary. */
   211     today = localtime( &ltime );
   212     if( today->tm_hour > 12 )
   213 	{
   214 	strcpy( ampm, "PM" );
   215 	today->tm_hour -= 12;
   216 	}
   217     if( today->tm_hour == 0 )  /* Adjust if midnight hour. */
   218 	today->tm_hour = 12;
   219 
   220     /* Note how pointer addition is used to skip the first 11 
   221      * characters and printf is used to trim off terminating 
   222      * characters.
   223      */
   224     printf( "12-hour time:                       %.8s %s\n",
   225        asctime( today ) + 11, ampm );
   226 
   227     /* Make time for noon on Christmas, 1993. */
   228     if( mktime( &xmas ) != (time_t)-1 )
   229 	printf( "Christmas                           %s\n", asctime( &xmas ) );
   230 
   231     /* Use time structure to build a customized time string. */
   232     today = localtime( &ltime );
   233 
   234     /* Use strftime to build a customized time string. */
   235     strftime( tmpbuf, 128,
   236 	"Today is %A, day %d of %B in the year %Y.\n", today );
   237     printf( tmpbuf );
   238 }
   239 
   240   
   241 
   242