|
|
1.1 root 1: /* The Plum Hall Validation Suite for C
2: * Unpublished copyright (c) 1986-1991, Chiron Systems Inc and Plum Hall Inc.
3: * VERSION: 4
4: * DATE: 1993-01-01
5: * The "ANSI" mode of this suite corresponds to official ANSI C, X3.159-1989.
6: * As per your license agreement, your distribution is not to be moved or copied outside the Designated Site
7: * without specific permission from Plum Hall Inc.
8: */
9:
10: #define LIB_TEST 1
11: #include "defs.h"
12: #if !ANSI
13: #define SKIP412 1 /* This file is almost irrelevant for non-ANSI */
14: #endif
15:
16: #include "flags.h"
17: #ifndef SKIP412
18: /*
19: * 4.12 - Date and time
20: */
21: #include <time.h>
22: #include <limits.h>
23: #include <string.h>
24: struct tm tm1, tm2, *ptm;
25: static time_t time_t1, time_t2, time_t3;
26: static void d4_12_1();
27: static void d4_12_2();
28: static void d4_12_3();
29: void d4_12()
30: {
31: Filename = "d412.c";
32: d4_12_1();
33: d4_12_2();
34: d4_12_3();
35: }
36:
37: /*
38: * 4.12.1 Components of time
39: */
40: static void d4_12_1()
41: {
42: /* d4_12_1 is required by both d4_12_2 and d4_12_3 */
43: /* make sure that all of the required fields are there */
44: tm1.tm_sec = 0;
45: tm1.tm_min = 49;
46: tm1.tm_hour = 20;
47: tm1.tm_mday = 12;
48: tm1.tm_mon = 8;
49: tm1.tm_year = 76;
50: tm1.tm_wday = -99;
51: tm1.tm_yday = -99;
52: tm1.tm_isdst = 1;
53: }
54:
55: /*
56: * 4.12.2 Time manipulation functions.
57: */
58: static void d4_12_2()
59: {
60: #ifndef SKIP4_122
61: int sec;
62: clock_t c;
63:
64: /* 4.12.2.1 clock
65: * system time. Also check existence of CLOCKS_PER_SEC macro
66: */
67: sec = (c = clock()) / CLOCKS_PER_SEC;
68:
69: /* is the processor time available via "clock" ? */
70: if (c == (clock_t)-1)
71: complain(- __LINE__);
72:
73: /* 4.12.2.2 difftime
74: * compute the difference between two times
75: */
76: time_t1 = mktime(&tm1);
77: tm1.tm_sec = 5;
78: time_t2 = mktime(&tm1);
79: dequals(__LINE__, difftime(time_t1, time_t2), -5.0);
80:
81: /* 4.12.2.3 mktime
82: * convert a "struct tm" to a "time_t". Return
83: * (time_t)-1 if it is unrepresentable
84: */
85: tm2.tm_sec = tm2.tm_min = tm2.tm_hour = tm2.tm_mday =
86: tm2.tm_mon = tm2.tm_year = INT_MIN;
87: checkthat( - __LINE__, mktime(&tm2) == (time_t)-1);
88:
89: /* now check that the day of the week for tm1 is Sunday. */
90: iequals(__LINE__, tm1.tm_wday, 0);
91:
92: /* 4.12.2.4 time
93: * present time of day
94: */
95: time_t1 = time_t2;
96: time_t3 = time(&time_t1);
97:
98: /* check whether the time is available */
99: if (time_t3 == (time_t)-1)
100: complain(- __LINE__);
101: dequals(__LINE__, difftime(time_t3, time_t1), 0.0);
102: checkthat(__LINE__, difftime(time_t1, time_t2) > 0.0);
103: #endif /* SKIP4_122 */
104: }
105:
106: /*
107: * 4.12.3 Time conversion functions
108: */
109: static void d4_12_3()
110: {
111: #ifndef SKIP4_123
112: char s[64];
113: char *p;
114:
115: /* 4.12.3.1 asctime
116: * convert a "struct tm" to a string
117: */
118: p = asctime(&tm1);
119:
120: /* 4.12.3.2 ctime
121: * convert a "time_t" to a string
122: */
123: time_t1 = mktime(&tm1);
124: stequals(__LINE__, p, ctime(&time_t1));
125: iequals(__LINE__, tm1.tm_wday, 0); /* Sunday */
126: iequals(__LINE__, tm1.tm_yday, 255);
127:
128: /* 4.12.3.3 gmtime
129: * convert a "time_t" to a Greenwich Mean Time string
130: */
131: ptm = gmtime(&time_t1);
132:
133: /* 4.12.3.4 localtime
134: * convert a "time_t" to a "struct tm"
135: */
136: ptm = localtime(&time_t1);
137: time_t2 = mktime(ptm);
138: /* this should be an isomorphic change */
139: dequals(__LINE__, difftime(time_t1, time_t2), 0.0);
140:
141: /* 4.12.3.5 strftime
142: * encode time information in a locale dependant way.
143: * This test will make sure that the "C" locale works right.
144: */
145: iequals(__LINE__, strftime(s, 64, "XXX", &tm1), 3);
146: iequals(__LINE__, strftime(s, 2, "XXX", &tm1), 0);
147:
148: #define ST(in, out) strftime(s, 64, in, &tm1); stequals(__LINE__, s, out);
149: #define ST2(in, out) strftime(s, 64, in, &tm1); stequals( - __LINE__, s, out);
150:
151: /* check each of the formatting directives */
152: ST("%a", "Sun");
153: ST("%A", "Sunday");
154: ST("%b", "Sep");
155: ST("%B", "September");
156: {
157: static char a[31] = {0};
158:
159: strftime(a, 30, "%c", &tm1);
160: if (strchr(a, '\n') == 0)
161: *strchr(a, '\0') = '\n';
162: stequals(- __LINE__, a, asctime(&tm1));
163: }
164: ST("%d", "12");
165: ST("%H", "20");
166: ST("%I", "08");
167: ST("%j", "256");
168: ST("%m", "09");
169: ST("%M", "49");
170: ST("%p", "PM");
171: ST("%S", "05");
172: ST("%U", "37");
173: ST("%w", "0");
174: ST("%W", "36");
175: ST2("%x", "Sun Sep 12, 1976");
176: ST2("%X", "20:49:05");
177: ST("%y", "76");
178: ST("%Y", "1976");
179: ST("%%", "%");
180: #endif /* SKIP4_123 */
181: }
182:
183: #else /* if SKIP412 */
184: void d4_12() { pr_skip("d4_12: SKIPPED ENTIRELY\n"); }
185: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.