2 * strftime - convert a structure to a string, controlled by an argument
9 /* The width can be -1 in both s_prnt() as in u_prnt(). This
10 * indicates that as many characters as needed should be printed.
13 s_prnt(char *s
, size_t maxsize
, const char *str
, int width
)
15 while (width
> 0 || (width
< 0 && *str
)) {
25 u_prnt(char *s
, size_t maxsize
, unsigned val
, int width
)
31 if (--width
> 0 || (width
< 0 && val
!= 0))
32 s
= u_prnt(s
, (maxsize
? maxsize
- 1 : 0), val
, width
);
33 if (maxsize
) *s
++ = c
+ '0';
38 strftime(char *s
, size_t maxsize
,
39 const char *format
, const struct tm
*timeptr
)
44 if (!format
) return 0;
46 _tzset(); /* for %Z conversion */
48 while (maxsize
&& *format
) {
49 while (maxsize
&& *format
&& *format
!= '%') {
53 if (!maxsize
|| !*format
) break;
59 s
= s_prnt(s
, maxsize
,
60 _days
[timeptr
->tm_wday
], ABB_LEN
);
64 s
= s_prnt(s
, maxsize
, _days
[timeptr
->tm_wday
], -1);
68 s
= s_prnt(s
, maxsize
,
69 _months
[timeptr
->tm_mon
], ABB_LEN
);
73 s
= s_prnt(s
, maxsize
, _months
[timeptr
->tm_mon
], -1);
77 n
= strftime(s
, maxsize
,
78 "%a %b %d %H:%M:%S %Y", timeptr
);
84 s
= u_prnt(s
, maxsize
, timeptr
->tm_mday
, 2);
88 s
= u_prnt(s
, maxsize
, timeptr
->tm_hour
, 2);
92 s
= u_prnt(s
, maxsize
,
93 (timeptr
->tm_hour
+ 11) % 12 + 1, 2);
97 s
= u_prnt(s
, maxsize
, timeptr
->tm_yday
+ 1, 3);
101 s
= u_prnt(s
, maxsize
, timeptr
->tm_mon
+ 1, 2);
105 s
= u_prnt(s
, maxsize
, timeptr
->tm_min
, 2);
109 s
= s_prnt(s
, maxsize
,
110 (timeptr
->tm_hour
< 12) ? "AM" : "PM", 2);
114 s
= u_prnt(s
, maxsize
, timeptr
->tm_sec
, 2);
118 s
= u_prnt(s
, maxsize
, /* ??? */
119 (timeptr
->tm_yday
+ 7 - timeptr
->tm_wday
) / 7, 2);
123 s
= u_prnt(s
, maxsize
, timeptr
->tm_wday
, 1);
127 s
= u_prnt(s
, maxsize
, /* ??? */
128 (timeptr
->tm_yday
+7-(timeptr
->tm_wday
+6)%7)/7,2);
132 n
= strftime(s
, maxsize
, "%a %b %d %Y", timeptr
);
138 n
= strftime(s
, maxsize
, "%H:%M:%S", timeptr
);
144 s
= u_prnt(s
, maxsize
, timeptr
->tm_year
% 100, 2);
148 s
= u_prnt(s
, maxsize
, timeptr
->tm_year
+ YEAR0
, -1);
152 s
= s_prnt(s
, maxsize
,
153 _tzname
[(timeptr
->tm_isdst
> 0)], -1);
161 /* A conversion error. Leave the loop. */
162 while (*format
) format
++;
171 return 0; /* The buffer is full */