define __KERNEL_STRICT_NAMES to avoid inclusion of kernel types on systems that carry...
[cake.git] / compiler / clib / strftime.c
blobf23f8835b12635663725d23a91ab5b9340a339ac
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <time.h>
7 /* #include <locale.h>
8 #include <libraries/locale.h>
9 #include <proto/locale.h> */
11 #define ADDS(st) tmp=strftime(s,maxsize-size,(st),timeptr);break;
13 #define ADDN(a,b) tmp=strfnumb(s,maxsize-size,(a),(b));break;
15 #define STOR(c) if(++size<=maxsize)*s++=(c);
17 #if 0
18 #define STR(a) \
19 (__localevec[LC_TIME-1]==NULL?strings[(a)-1]:GetLocaleStr(__localevec[LC_TIME-1],(a)))
20 #else
21 #define STR(a) (strings[(a)-1])
22 #endif
24 /* extern struct Locale *__localevec[]; */
26 /* All calendar strings */
27 static const unsigned char *strings[]=
29 /* 0 */
30 "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
31 /* 7 */
32 "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
33 /* 14 */
34 "January","February","March","April","May","June",
35 "July","August","September","October","November","December",
36 /* 26 */
37 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
38 /* 39 */
39 "","",
40 /* 41 */
41 "AM","PM"
43 #define ABDAY_1 7
44 #define ABMON_1 26
45 #define AM_STR 41
46 #define DAY_1 0
47 #define MON_1 14
49 static size_t strfnumb(char *s, size_t maxsize, signed int places, size_t value)
51 size_t size = 0;
53 if (places > 1)
55 size = strfnumb(s, maxsize, places - 1, value / 10);
57 else if (value >= 10)
59 size = strfnumb(s, maxsize, places + 1, value / 10);
61 else
63 while ((places++ < -1) && (++size <= maxsize))
65 s[size - 1] = ' ';
69 if (++size <= maxsize) s[size - 1] = (value % 10 + '0');
71 return size;
74 /*****************************************************************************
76 NAME */
78 size_t strftime(
80 /* SYNOPSIS */
81 char *s,
82 size_t maxsize,
83 const char *format,
84 const struct tm *timeptr)
86 /* FUNCTION
88 INPUTS
90 RESULT
92 NOTES
94 EXAMPLE
96 BUGS
98 SEE ALSO
100 INTERNALS
102 ******************************************************************************/
104 size_t size = 0, tmp;
106 if (format == NULL || timeptr == NULL) return 0;
108 while (*format)
110 if (*format == '%')
112 tmp = 0;
114 switch(*++format)
116 case 'a':
117 ADDS(STR(ABDAY_1+timeptr->tm_wday+1));
118 case 'b':
119 case 'h':
120 ADDS(STR(ABMON_1+timeptr->tm_mon+1));
121 case 'c':
122 ADDS("%m/%d/%y");
123 case 'd':
124 ADDN(2,timeptr->tm_mday);
125 case 'e':
126 ADDN(-2,timeptr->tm_mday);
127 case 'j':
128 ADDN(3,timeptr->tm_yday+1);
129 case 'k':
130 ADDN(-2,timeptr->tm_hour);
131 case 'l':
132 ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
133 case 'm':
134 ADDN(2,timeptr->tm_mon+1);
135 case 'p':
136 ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
137 case 'r':
138 ADDS("%I:%M:%S %p");
139 case 'w':
140 ADDN(1,timeptr->tm_wday);
141 case 'x':
142 ADDS("%m/%d/%y %H:%M:%S");
143 case 'y':
144 ADDN(2,timeptr->tm_year%100);
145 case 'A':
146 ADDS(STR(DAY_1+timeptr->tm_wday+1));
147 case 'B':
148 ADDS(STR(MON_1+timeptr->tm_mon+1));
149 case 'C':
150 ADDS("%a %b %e %H:%M:%S %Y");
151 case 'D':
152 ADDS("%m/%d/%y");
153 case 'H':
154 ADDN(2,timeptr->tm_hour);
155 case 'I':
156 ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
157 case 'M':
158 ADDN(2,timeptr->tm_min);
159 case 'R':
160 ADDS("%H:%M");
161 case 'S':
162 ADDN(2,timeptr->tm_sec);
163 case 'T':
164 case 'X':
165 ADDS("%H:%M:%S");
166 case 'U':
167 ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
168 case 'W':
169 ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
170 case 'Y':
171 ADDN(4,timeptr->tm_year+1900);
172 case 't':
173 STOR('\t');
174 break;
175 case 'n':
176 STOR('\n');
177 break;
178 case '%':
179 STOR('%');
180 break;
181 case '\0':
182 format--;
183 break;
186 size += tmp;
187 s += tmp;
189 else
191 STOR(*format);
194 format++;
197 STOR('\0');
199 if (size > maxsize)
201 s -= size;
203 if (maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
205 s[maxsize - 1] = '\0';
208 size = 1;
211 return size - 1;