137103Sbostic /* 237103Sbostic * Copyright (c) 1989 The Regents of the University of California. 337103Sbostic * All rights reserved. 437103Sbostic * 537103Sbostic * Redistribution and use in source and binary forms are permitted 637103Sbostic * provided that the above copyright notice and this paragraph are 737103Sbostic * duplicated in all such forms and that any documentation, 837103Sbostic * advertising materials, and other materials related to such 937103Sbostic * distribution and use acknowledge that the software was developed 1037103Sbostic * by the University of California, Berkeley. The name of the 1137103Sbostic * University may not be used to endorse or promote products derived 1237103Sbostic * from this software without specific prior written permission. 1337103Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437103Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537103Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637103Sbostic */ 1737103Sbostic 1837103Sbostic #if defined(LIBC_SCCS) && !defined(lint) 19*37137Sbostic static char sccsid[] = "@(#)strftime.c 5.2 (Berkeley) 03/12/89"; 2037103Sbostic #endif /* LIBC_SCCS and not lint */ 2137103Sbostic 2237103Sbostic #include <sys/types.h> 2337103Sbostic #include <sys/time.h> 2437103Sbostic #include <tzfile.h> 2537103Sbostic 2637103Sbostic static char *afmt[] = { 2737103Sbostic "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 2837103Sbostic }; 2937103Sbostic static char *Afmt[] = { 3037103Sbostic "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 3137103Sbostic "Saturday", 3237103Sbostic }; 3337103Sbostic static char *bfmt[] = { 3437103Sbostic "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 3537103Sbostic "Oct", "Nov", "Dec", 3637103Sbostic }; 3737103Sbostic static char *Bfmt[] = { 3837103Sbostic "January", "February", "March", "April", "May", "June", "July", 3937103Sbostic "August", "September", "October", "November", "December", 4037103Sbostic }; 4137103Sbostic 4237103Sbostic static size_t gsize; 4337103Sbostic static char *pt; 4437103Sbostic 4537103Sbostic size_t 4637103Sbostic strftime(s, maxsize, format, t) 4737103Sbostic char *s; 4837103Sbostic char *format; 4937103Sbostic size_t maxsize; 5037103Sbostic struct tm *t; 5137103Sbostic { 5237103Sbostic size_t _fmt(); 5337103Sbostic 5437103Sbostic pt = s; 5537103Sbostic if ((gsize = maxsize) < 1) 5637103Sbostic return(0); 5737103Sbostic if (_fmt(format, t)) { 5837103Sbostic *pt = '\0'; 5937103Sbostic return(maxsize - gsize); 6037103Sbostic } 6137103Sbostic return(0); 6237103Sbostic } 6337103Sbostic 6437103Sbostic static size_t 6537103Sbostic _fmt(format, t) 6637103Sbostic register char *format; 6737103Sbostic struct tm *t; 6837103Sbostic { 6937103Sbostic char *timezone(); 7037103Sbostic 7137103Sbostic for (; *format; ++format) { 7237103Sbostic if (*format == '%') 7337103Sbostic switch(*++format) { 7437103Sbostic case 'A': 7537103Sbostic if (!_add(Afmt[t->tm_mon])) 7637103Sbostic return(0); 7737103Sbostic continue; 7837103Sbostic case 'a': 7937103Sbostic if (!_add(afmt[t->tm_mon])) 8037103Sbostic return(0); 8137103Sbostic continue; 8237103Sbostic case 'B': 8337103Sbostic if (!_add(Bfmt[t->tm_mon])) 8437103Sbostic return(0); 8537103Sbostic continue; 8637103Sbostic case 'b': 87*37137Sbostic case 'h': 8837103Sbostic if (!_add(bfmt[t->tm_mon])) 8937103Sbostic return(0); 9037103Sbostic continue; 9137103Sbostic case 'c': 9237103Sbostic if (!_fmt("%x %X %Z %Y", t)) 9337103Sbostic return(0); 9437103Sbostic continue; 95*37137Sbostic case 'D': 96*37137Sbostic if (!_fmt("%m/%d/%y", t)) 97*37137Sbostic return(0); 98*37137Sbostic continue; 9937103Sbostic case 'd': 10037103Sbostic if (!_conv(t->tm_mday, 2)) 10137103Sbostic return(0); 10237103Sbostic continue; 10337103Sbostic case 'H': 10437103Sbostic if (!_conv(t->tm_hour, 2)) 10537103Sbostic return(0); 10637103Sbostic continue; 10737103Sbostic case 'I': 10837103Sbostic if (!_conv((t->tm_hour - 1) % 12 + 1, 2)) 10937103Sbostic return(0); 11037103Sbostic continue; 11137103Sbostic case 'j': 11237103Sbostic if (!_conv(t->tm_yday + 1, 3)) 11337103Sbostic return(0); 11437103Sbostic continue; 11537103Sbostic case 'M': 11637103Sbostic if (!_conv(t->tm_min, 2)) 11737103Sbostic return(0); 11837103Sbostic continue; 11937103Sbostic case 'm': 12037103Sbostic if (!_conv(t->tm_mon + 1, 2)) 12137103Sbostic return(0); 12237103Sbostic continue; 123*37137Sbostic case 'n': 124*37137Sbostic if (!_add("\n")) 125*37137Sbostic return(0); 126*37137Sbostic continue; 12737103Sbostic case 'p': 12837103Sbostic if (!_add(t->tm_hour >= 12 ? "AM" : "PM")) 12937103Sbostic return(0); 13037103Sbostic continue; 131*37137Sbostic case 'R': 132*37137Sbostic if (!_fmt("%H:%M", t)) 133*37137Sbostic return(0); 134*37137Sbostic continue; 135*37137Sbostic case 'r': 136*37137Sbostic if (!_fmt("%I:%M:%S %p", t)) 137*37137Sbostic return(0); 138*37137Sbostic continue; 13937103Sbostic case 'S': 14037103Sbostic if (!_conv(t->tm_sec, 2)) 14137103Sbostic return(0); 14237103Sbostic continue; 143*37137Sbostic case 'T': 144*37137Sbostic case 'X': 145*37137Sbostic if (!_fmt("%H:%M:%S", t)) 146*37137Sbostic return(0); 147*37137Sbostic continue; 148*37137Sbostic case 't': 149*37137Sbostic if (!_add("\t")) 150*37137Sbostic return(0); 151*37137Sbostic continue; 15237103Sbostic case 'U': 15337103Sbostic if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7, 15437103Sbostic 2)) 15537103Sbostic return(0); 15637103Sbostic continue; 15737103Sbostic case 'W': 15837103Sbostic if (!_conv((t->tm_yday + 7 - 15937103Sbostic (t->tm_wday ? t->tm_wday : 6)) / 7, 2)) 16037103Sbostic return(0); 16137103Sbostic continue; 16237103Sbostic case 'w': 16337103Sbostic if (!_conv(t->tm_wday, 1)) 16437103Sbostic return(0); 16537103Sbostic continue; 16637103Sbostic case 'x': 16737103Sbostic if (!_fmt("%a %b %d", t)) 16837103Sbostic return(0); 16937103Sbostic continue; 17037103Sbostic case 'y': 17137103Sbostic if (!_conv((t->tm_year + TM_YEAR_BASE) 17237103Sbostic % 100, 2)) 17337103Sbostic return(0); 17437103Sbostic continue; 17537103Sbostic case 'Y': 17637103Sbostic if (!_conv(t->tm_year + TM_YEAR_BASE, 4)) 17737103Sbostic return(0); 17837103Sbostic continue; 17937103Sbostic case 'Z': 18037103Sbostic if (!_add(t->tm_zone)) 18137103Sbostic return(0); 18237103Sbostic continue; 18337103Sbostic case '%': 18437103Sbostic /* 18537103Sbostic * X311J/88-090 (4.12.3.5): if conversion char is 18637103Sbostic * undefined, behavior is undefined. Print out the 18737103Sbostic * character itself as printf(3) also does. 18837103Sbostic */ 18937103Sbostic default: 19037103Sbostic break; 19137103Sbostic } 19237103Sbostic if (!gsize--) 19337103Sbostic return(0); 19437103Sbostic *pt++ = *format; 19537103Sbostic } 19637103Sbostic return(gsize); 19737103Sbostic } 19837103Sbostic 19937103Sbostic static 20037103Sbostic _conv(n, digits) 20137103Sbostic int n, digits; 20237103Sbostic { 20337103Sbostic static char buf[10]; 20437103Sbostic register char *p; 20537103Sbostic 20637103Sbostic for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits) 20737103Sbostic *p-- = n % 10 + '0'; 20837103Sbostic while (p > buf && digits-- > 0) 20937103Sbostic *p-- = '0'; 21037103Sbostic return(_add(++p)); 21137103Sbostic } 21237103Sbostic 21337103Sbostic static 21437103Sbostic _add(str) 21537103Sbostic register char *str; 21637103Sbostic { 21737103Sbostic for (;; ++pt, --gsize) { 21837103Sbostic if (!gsize) 21937103Sbostic return(0); 22037103Sbostic if (!(*pt = *str++)) 22137103Sbostic return(1); 22237103Sbostic } 22337103Sbostic } 224