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