Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
22 #include <stdlib.h> /* definition of exit() */
27 #include <sys/time.h> /* timeval, gettimeofday */
29 void msdev_time_example(void);
31 void NEXT_TEST(const char *title)
37 printf("\nContinue [Y/N]? ");
39 if (c == 'q' || c == 'Q' || c == 'n' || c == 'N')
41 printf("\nAbandoned.\n");
46 printf("\f\n%s\n\n", title);
50 @SYMTestCaseID SYSLIB-STDLIB-CT-1080
51 @SYMTestCaseDesc Time handling functions test
53 @SYMTestActions Tests for time handling routines
54 @SYMTestExpectedResults Test must not fail
57 int main (int argc, char *argv[])
60 struct tm *tmp, newtm;
62 struct timeval tv, tv2;
64 int err, duration, count;
66 NEXT_TEST("What's the time, Mr Epoc32");
70 err = gettimeofday(&tv, &tz);
72 printf("time_t 0 = %9ld = %s\n", then, ctime(&then));
73 printf("time now = %9ld = %s", now, ctime(&now));
76 printf("gettimeofday failed\n");
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");
85 NEXT_TEST("Cost of getttimeofday");
88 gettimeofday(&tv, &tz);
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)
94 err = gettimeofday(&tv, &tz);
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)
101 err = gettimeofday(&tv2, &tz);
106 printf("gettimeofday failed\n");
109 duration = 1000000 *(tv2.tv_sec - tv.tv_sec) + tv2.tv_usec - tv.tv_usec;
110 if (tv2.tv_usec < tv.tv_usec)
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);
118 NEXT_TEST("localtime: Conversion to struct tm");
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);
128 NEXT_TEST("mktime: Conversion from struct tm");
130 /* Example derived from MSDEV mktime documentation */
138 newtm.tm_wday = 9999;
139 newtm.tm_yday = 9999;
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);
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));
153 printf("\nAdd 29 days...(Wed Jun 01)\n\n");
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));
163 NEXT_TEST("strftime: customised string conversion");
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));
172 NEXT_TEST("Example code from MSDEV documentation");
174 msdev_time_example();
180 Example code derived from MSDEV documentation on time() function
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
188 Today is Tuesday, day 03 of May in the year 1994.
191 void msdev_time_example(void)
193 char tmpbuf[128], ampm[] = "AM";
195 struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
197 /* Get UNIX-style time and display as number and string.
198 * time( <ime ); Tue May 03 21:51:03 1994
200 struct tm example = { 3, 51, 21, 3, 4, 94 };
201 ltime = mktime(&example);
203 printf( "Time in seconds since UTC 1/1/70: %ld\n", ltime );
204 printf( "UNIX time and date: %s", ctime( <ime ) );
207 gmt = gmtime( <ime );
208 printf( "Coordinated universal time: %s", asctime( gmt ) );
210 /* Convert to time structure and adjust for PM if necessary. */
211 today = localtime( <ime );
212 if( today->tm_hour > 12 )
214 strcpy( ampm, "PM" );
215 today->tm_hour -= 12;
217 if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
220 /* Note how pointer addition is used to skip the first 11
221 * characters and printf is used to trim off terminating
224 printf( "12-hour time: %.8s %s\n",
225 asctime( today ) + 11, ampm );
227 /* Make time for noon on Christmas, 1993. */
228 if( mktime( &xmas ) != (time_t)-1 )
229 printf( "Christmas %s\n", asctime( &xmas ) );
231 /* Use time structure to build a customized time string. */
232 today = localtime( <ime );
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 );