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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/*
* cook - file construction tool
* Copyright (C) 1994, 1996, 1997, 2001 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: impliment missing functions from <time.h>
*/
#include <ac/time.h>
/*
* NAME
* strftime - string from time
*
* SYNOPSIS
* size_t strftime(char *s, size_t maxsize, const char *format,
* const struct tm *timeptr);
*
* DESCRIPTION
* The strftime function places characters into the array pointed to
* by s as controlled by the string pointed to by format. The format
* string consists of zero or more directives and ordinary characters.
* A directive consists of a % character followed by a character that
* determined the dirrective's behaviour. All ordinary characters
* (including the terminating null character) are copied unchanged
* into the array. No more thaqn maxsize characters are placed into
* the array. Each directive is replaced by appropriate characters as
* described in the following list. The appropriate characters are
* determined by the program's locale and by the values contained in
* the structure pointed to by timeptr.
*
* %a is replaced by the locale's abbreviated weekday name
* %A is replaced by the locale's full weekday name
* %b is replaced by the locale's abbreviated month name
* %B is replaced by the locale's full month name
* %c is replaced by the locale's appropriae date and time representation
* %d is replaced by the day of the month as decimal number (01-31)
* %H is replaced by the hour (24-hour clock) as decimal number (00-23)
* %I is replaced by the hour (12-hour clock) as decimal number (01-12)
* %j is replaced by the day of te year as decimal number (001-366)
* %m is replaced by the month as decomal number (01-12)
* %M is replaced by the minute as decimal number (00-59)
* %p is replaced by the locale's equivalent of either AM or PM
* %S is replaced by the second as decimal number (00-59)
* %U is replaced by the week number of they year (Sunday as the first
* day of the week) as decimal number (00-52)
* %w is replaced by the weekday as decimal number (0=Sunday to 6=Saturday)
* %W is replaced by the week number of the year (Monday as the first
* day of the week) as decimal number (00-52)
* %x is replaced by the locale's appropriate date representation
* %X is replaced by the locale's appropriate time representation
* %y is replaced by the year without century as decimal number (00-99)
* %Y is replaced by the year with century as decimal number
* %Z is replaced by the time zone name, or no characters in no time
* zone name is available
* %% is replaced by %
*
* RETURNS
* If the total number of resulting characters including the terminating
* null character is not more than maxsize, the strftime function returns
* the number of characters placed into the array pointed to by s not
* including the terminating null character. Otherwise, zero is returned
* and the contents of the array are indeterminate.
*
* CAVEAT
* This suffers from a serious design flaw: there is no way to
* distinguish between a result which is the empty string, and a result
* which is more than maxsize characters.
*
* The behaviour for unknow directivbes is not only undefined,
* it is unmentioned! (Normally the standard specifically allows
* implementation defined behaviour on weird boundary conditions.)
* This implementation will echo unknown directives into the output.
*/
#ifndef HAVE_STRFTIME
#ifndef HAVE_tm_zone
extern char *tzname[2];
#endif
size_t
strftime(buf, max, fmt, tm)
char *buf;
size_t max;
char *fmt;
struct tm *tm;
{
char *cp;
char *end;
char output[1000];
int n;
size_t len;
static char *weekday[] =
{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
};
static char *month[] =
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
};
end = buf + max - 1;
cp = buf;
while (*fmt)
{
if (*fmt++ != '%')
{
if (cp >= end)
return 0;
*cp++ = fmt[-1];
continue;
}
switch (*fmt++)
{
case 0:
--fmt;
output[0] = '%';
output[1] = 0;
break;
default:
output[0] = '%';
output[1] = fmt[-1];
output[2] = 0;
break;
case '%':
output[0] = '%';
output[1] = 0;
break;
case 'a':
/*
* the abbreviated weekday name
*/
sprintf(output, "%3.3s", weekday[tm->tm_wday]);
break;
case 'A':
/*
* the full weekday name
*/
strcpy(output, weekday[tm->tm_wday]);
break;
case 'b':
case 'h':
/*
* the abbreviated month name
*/
sprintf(output, "%3.3s", month[tm->tm_mon]);
break;
case 'B':
/*
* the full month name
*/
strcpy(output, month[tm->tm_mon]);
break;
case 'c':
/*
* the date and time
*/
len =
strftime
(
output,
sizeof(output),
"%b %e %X %Y",
tm
);
if (!len)
output[0] = 0;
break;
case 'C':
/*
* This looks like a Sun extra.
* Local date.
*/
len =
strftime
(
output,
sizeof(output),
"%A, %B %e, %Y",
tm
);
if (!len)
output[0] = 0;
break;
case 'd':
/*
* the day of the month,
* zero padded.
*/
sprintf(output, "%2.2d", tm->tm_mday);
break;
case 'D':
/*
* This looks like a Sun extra.
* Local date.
*/
len = strftime(output, sizeof(output), "%m/%d/%y", tm);
if (!len)
output[0] = 0;
break;
case 'e':
/*
* This looks like a Sun extra.
* the day of the month,
* blank padded.
*/
sprintf(output, "%2d", tm->tm_mday);
break;
case 'H':
/*
* the hour of a 24-hour day
* zero padded
*/
sprintf(output, "%2.2d", tm->tm_hour);
break;
case 'I':
/*
* the hour of a 12-hour day,
* zero padded
*/
n = tm->tm_hour % 12;
sprintf(output, "%2.2d", n ? n : 12);
break;
case 'j':
/*
* the day of the year,
* zero padded, one based
*/
sprintf(output, "%3.3d", tm->tm_yday + 1);
break;
case 'k':
/*
* This looks like a Sun extra.
* the hour of the 24-hour day,
* blank padded.
*/
sprintf(output, "%2d", tm->tm_hour);
break;
case 'l':
/*
* This looks like a Sun extra.
* the hour of the 12-hour day,
* blank padded.
*/
n = tm->tm_hour % 12;
sprintf(output, "%2d", n ? n : 12);
break;
case 'm':
/*
* the month of the year,
* zero padded, one based.
*/
sprintf(output, "%2.2d", tm->tm_mon + 1);
break;
case 'M':
/*
* the minute of the hour,
* zero padded
*/
sprintf(output, "%2.2d", tm->tm_min);
break;
case 'n':
/*
* This looks like a Sun extra.
* like \n
*/
output[0] = '\n';
output[1] = 0;
break;
case 'p':
/*
* meridian indicator
*/
if (tm->tm_hour >= 12)
strcpy(output, "PM");
else
strcpy(output, "AM");
break;
case 'r':
/*
* this looks like a Sun extra.
* like %X, but 12-hour clock with meridian.
*/
len =
strftime
(
output,
sizeof(output),
"%I:%M:%S %p",
tm
);
if (!len)
output[0] = 0;
break;
case 'R':
/*
* this looks like a Sun extra.
* the 24-hour time as HH:MM
*/
len = strftime(output, sizeof(output), "%H:%M", tm);
if (!len)
output[0] = 0;
break;
case 'S':
/*
* seconds of the minute
*/
sprintf(output, "%2.2d", tm->tm_sec);
break;
case 't':
/*
* this looks like a Sun extra.
* like \t
*/
output[0] = '\t';
output[1] = 0;
break;
case 'T':
/*
* This looks like a Sun extra.
* the 24-hour time as HH:MM:SS
*/
len = strftime(output, sizeof(output), "%H:%M:%S", tm);
if (!len)
output[0] = 0;
break;
case 'U':
/*
* the Sunday week of the year
*/
n = (tm->tm_yday - tm->tm_wday + 5) / 7;
sprintf(output, "%2.2d", n);
break;
case 'w':
/*
* the day of the week,
* Sunday = 0
*/
sprintf(output, "%d", tm->tm_wday);
break;
case 'W':
/*
* the Monday week of the year
*/
n = (tm->tm_yday - ((tm->tm_wday + 6) % 7) + 5) / 7;
sprintf(output, "%2.2d", n);
break;
case 'x':
/*
* the date, as mmm dd yyyy
*/
len = strftime(output, sizeof(output), "%b %d %Y", tm);
if (!len)
output[0] = 0;
break;
case 'X':
/*
* the time as hh:mm:ss
*/
len = strftime(output, sizeof(output), "%H:%M:%S", tm);
if (!len)
output[0] = 0;
break;
case 'y':
/*
* the year of the century
*/
sprintf(output, "%2.2d", tm->tm_year % 100);
break;
case 'Y':
/*
* the year including century
*/
sprintf(output, "%4.4d", tm->tm_year + 1900);
break;
case 'Z':
/*
* the timezone name, if any
*/
#ifndef HAVE_tm_zone
if (tm->tm_isdst >= 0 && tm->tm_isdst <= 1)
strcpy(output, tzname[tm->tm_isdst]);
else
output[0] = 0;
#else
/* Berkeley derivatives have extra tm field */
strcpy(output, tm->tm_zone);
#endif
break;
}
/*
* make sure it fits in the buffer
*/
len = strlen(output);
if (cp + len > end)
return -1;
memcpy(cp, output, len);
cp += len;
}
*cp = 0;
return (cp - buf);
}
#endif /* !HAVE_STRFTIME */
|