File: strftime.c

package info (click to toggle)
trn4 4.0-test76-3
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 2,988 kB
  • ctags: 2,965
  • sloc: ansic: 48,337; sh: 6,770; tcl: 1,696; yacc: 660; perl: 108; makefile: 72
file content (225 lines) | stat: -rw-r--r-- 4,851 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * strftime: print formatted information about a given time.
 * Adapted from the routine by Eric R. Smith, Michal Jaegermann,
 * Arnold Robins, and Paul Close.
 */
/* The authors make no claims as to the fitness or correctness of this software
 * for any use whatsoever, and it is provided as is. Any use of this software
 * is at the user's own risk. 
 */

/* Configuration choices for %x and %X */

#undef LOCAL_DDMMYY	/* choose DD/MM/YY instead of MM/DD/YY */
#undef LOCAL_DOTTIME	/* choose HH.MM.SS instead of HH:MM:SS */

#include "EXTERN.h"
#include "common.h"

static char* mth_name[] = {
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

static char* day_name[] = {
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
    "Saturday"
};

#ifdef HAS_FTIME
# ifndef TM_ZONE
char tznm[16] = "";
# endif
#else
extern char* tzname[];
#endif

size_t
strftime(str, maxsize, fmt, ts)
char* str;
size_t maxsize;
CONST char* fmt;
CONST struct tm* ts;
{
    size_t num = 0, len;
    char ch;
    char* putstr;
    char tmpbuf[80];

    if (maxsize-- <= 0)
	return 0;

    for (;;) {
	if (!(ch = *fmt++))
	    break;
	if (num == maxsize) {
	    num = 0;
	    break;
	}
	if (ch != '%') {
	    *str++ = ch;
	    num++;
	    continue;
	}
	/* assume the finished product will be sprintf'ed into tmpbuf */
	putstr = tmpbuf;

	switch (ch = *fmt++) {
	case 'A':
	case 'a':
	    if (ts->tm_wday < 0 || ts->tm_wday > 6)
		putstr = "?";
	    else
		if (ch == 'A')
		    putstr = day_name[ts->tm_wday];
		else
		    sprintf(tmpbuf, "%-.3s", day_name[ts->tm_wday]);
	    break;
	case 'B':
	case 'b':
	case 'h':
	    if (ts->tm_mon < 0 || ts->tm_mon > 11)
		putstr = "?";
	    else if (ch == 'B')
		putstr = mth_name[ts->tm_mon];
	    else
		sprintf(tmpbuf, "%-.3s", mth_name[ts->tm_mon]);
	    break;
	case 'C':
	    strftime(tmpbuf, sizeof tmpbuf, "%A, %B %e, %Y", ts);
	    break;
	case 'c':
	    strftime(tmpbuf, sizeof tmpbuf, "%x %X", ts);
	    break;
	case 'D':
#ifndef LOCAL_DDMMYY
	case 'x':
#endif
	    strftime(tmpbuf, sizeof tmpbuf, "%m/%d/%y", ts);
	    break;
	case 'd':
	    sprintf(tmpbuf, "%02d", ts->tm_mday);
	    break;
	case 'e':	/* day of month, blank padded */
	    sprintf(tmpbuf, "%2d", ts->tm_mday);
	    break;
	case 'H':
	    sprintf(tmpbuf, "%02d", ts->tm_hour);
	    break;
	case 'I':
	{
	    int n;

	    n = ts->tm_hour;
	    if (n == 0)
		n = 12;
	    else if (n > 12)
		n -= 12;
	    sprintf(tmpbuf, "%02d", n);
	    break;
	}
	case 'j':
	    sprintf(tmpbuf, "%03d", ts->tm_yday + 1);
	    break;
	case 'm':
	    sprintf(tmpbuf, "%02d", ts->tm_mon + 1);
	    break;
	case 'M':
	    sprintf(tmpbuf, "%02d", ts->tm_min);
	    break;
	case 'p':
	    putstr = (ts->tm_hour < 12) ? "AM" : "PM";
	    break;
	case 'r':
	    strftime(tmpbuf, sizeof tmpbuf, "%I:%M:%S %p", ts);
	    break;
	case 'R':
	    strftime(tmpbuf, sizeof tmpbuf, "%H:%M", ts);
	    break;
	case 'S':
	    sprintf(tmpbuf, "%02d", ts->tm_sec);
	    break;
	case 'T':
#ifndef LOCAL_DOTTIME
	case 'X':
#endif
	    strftime(tmpbuf, sizeof tmpbuf, "%H:%M:%S", ts);
	    break;
	case 'U':	/* week of year - starting Sunday */
	    sprintf(tmpbuf, "%02d", (ts->tm_yday - ts->tm_wday + 10) / 7);
	    break;
	case 'W':	/* week of year - starting Monday */
	    sprintf(tmpbuf, "%02d", (ts->tm_yday - ((ts->tm_wday + 6) % 7)
			+ 10) / 7);
	    break;
	case 'w':
	    sprintf(tmpbuf, "%d", ts->tm_wday);
	    break;
	case 'y':
	    sprintf(tmpbuf, "%02d", ts->tm_year % 100);
	    break;
#ifdef LOCAL_DOTTIME
	case 'X':
	    strftime(tmpbuf, sizeof tmpbuf, "%H.%M.%S", ts);
	    break;
#endif
#ifdef LOCAL_DDMMYY
	case 'x':
	    strftime(tmpbuf, sizeof tmpbuf, "%d/%m/%y", ts);
	    break;
#endif
	case 'Y':
	    sprintf(tmpbuf, "%d", ts->tm_year + 1900);
	    break;
	case 'Z':
#if defined(HAS_GETTIMEOFDAY) || defined(HAS_FTIME)
# ifdef TM_ZONE
	    strcpy(tmpbuf, ts->tm_zone);
# else
#  ifdef HAS_GETTIMEOFDAY
	    if (*tznm == '\0') {
		char* timezone();
		struct timeval tv;
		struct timezone tz;

		(void) gettimeofday(&tv, &tz);
		strcpy(tznm, timezone(tz.tz_minuteswest, ts->tm_isdst));
	    }
	    strcpy(tmpbuf, tznm);
#  else
	    strcpy(tmpbuf, ts->tm_name);
#  endif
# endif /* !HAS_GETTIMEOFDAY */
#else /* !HAS_GETTIMEOFDAY && !HAS_FTIME */
	    strcpy(tmpbuf, tzname[ts->tm_isdst]);
#endif
	    break;
	case '%':
	case '\0':
	    putstr = "%";
	    break;
	case 'n':	/* same as \n */
	    putstr = "\n";
	    break;
	case 't':	/* same as \t */
	    putstr = "\t";
	    break;
	default:
	    sprintf(tmpbuf, "%%%c", ch);
	    break;
	}
	len = strlen(putstr);
	num += len;
	if (num > maxsize) {
	    len -= num - maxsize;
	    num = 0;
	    ch = '\0';
	}
	strncpy(str, putstr, len);
	str += len;
	if (!ch)
	    break;
    }
    *str = '\0';
    return num;
}