linux/bootstrap: use Forbid/Permit only when thread is main AROS thread
[AROS.git] / compiler / stdc / strftime.c
blob724ebe2955137e49acf4c6797b002ed84b22a668
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <time.h>
7 /*
8 TODO: Implement localization support
9 #include <locale.h>
10 #include <libraries/locale.h>
11 #include <proto/locale.h>
14 #define ADDS(st) tmp=strftime(s,maxsize-size,(st),timeptr);break;
16 #define ADDN(a,b) tmp=strfnumb(s,maxsize-size,(a),(b));break;
18 #define STOR(c) if(++size<=maxsize)*s++=(c);
20 #if 0
21 #define STR(a) \
22 (__localevec[LC_TIME-1]==NULL?strings[(a)-1]:GetLocaleStr(__localevec[LC_TIME-1],(a)))
23 #else
24 #define STR(a) (strings[(a)-1])
25 #endif
27 /* extern struct Locale *__localevec[]; */
29 /* All calendar strings */
30 static const unsigned char *strings[]=
32 /* 0 */
33 "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
34 /* 7 */
35 "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
36 /* 14 */
37 "January","February","March","April","May","June",
38 "July","August","September","October","November","December",
39 /* 26 */
40 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
41 /* 39 */
42 "","",
43 /* 41 */
44 "AM","PM"
46 #define ABDAY_1 7
47 #define ABMON_1 26
48 #define AM_STR 41
49 #define DAY_1 0
50 #define MON_1 14
52 static size_t strfnumb(char *s, size_t maxsize, signed int places, size_t value)
54 size_t size = 0;
56 if (places > 1)
58 size = strfnumb(s, maxsize, places - 1, value / 10);
60 else if (value >= 10)
62 size = strfnumb(s, maxsize, places + 1, value / 10);
64 else
66 while ((places++ < -1) && (++size <= maxsize))
68 s[size - 1] = ' ';
72 if (++size <= maxsize) s[size - 1] = (value % 10 + '0');
74 return size;
77 /*****************************************************************************
79 NAME */
81 size_t strftime(
83 /* SYNOPSIS */
84 char *s,
85 size_t maxsize,
86 const char *format,
87 const struct tm *timeptr)
89 /* FUNCTION
91 INPUTS
93 RESULT
95 NOTES
96 Function does not take localization into account at the moment
98 EXAMPLE
100 BUGS
102 SEE ALSO
104 INTERNALS
106 ******************************************************************************/
108 size_t size = 0, tmp;
110 if (format == NULL || timeptr == NULL) return 0;
112 while (*format)
114 if (*format == '%')
116 tmp = 0;
118 switch(*++format)
120 case 'a':
121 ADDS(STR(ABDAY_1+timeptr->tm_wday+1));
122 case 'b':
123 case 'h':
124 ADDS(STR(ABMON_1+timeptr->tm_mon+1));
125 case 'c':
126 ADDS("%m/%d/%y");
127 case 'd':
128 ADDN(2,timeptr->tm_mday);
129 case 'e':
130 ADDN(-2,timeptr->tm_mday);
131 case 'j':
132 ADDN(3,timeptr->tm_yday+1);
133 case 'k':
134 ADDN(-2,timeptr->tm_hour);
135 case 'l':
136 ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
137 case 'm':
138 ADDN(2,timeptr->tm_mon+1);
139 case 'p':
140 ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
141 case 'r':
142 ADDS("%I:%M:%S %p");
143 case 'w':
144 ADDN(1,timeptr->tm_wday);
145 case 'x':
146 ADDS("%m/%d/%y %H:%M:%S");
147 case 'y':
148 ADDN(2,timeptr->tm_year%100);
149 case 'A':
150 ADDS(STR(DAY_1+timeptr->tm_wday+1));
151 case 'B':
152 ADDS(STR(MON_1+timeptr->tm_mon+1));
153 case 'C':
154 ADDS("%a %b %e %H:%M:%S %Y");
155 case 'D':
156 ADDS("%m/%d/%y");
157 case 'H':
158 ADDN(2,timeptr->tm_hour);
159 case 'I':
160 ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
161 case 'M':
162 ADDN(2,timeptr->tm_min);
163 case 'R':
164 ADDS("%H:%M");
165 case 'S':
166 ADDN(2,timeptr->tm_sec);
167 case 'T':
168 case 'X':
169 ADDS("%H:%M:%S");
170 case 'U':
171 ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
172 case 'W':
173 ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
174 case 'Y':
175 ADDN(4,timeptr->tm_year+1900);
176 case 't':
177 STOR('\t');
178 break;
179 case 'n':
180 STOR('\n');
181 break;
182 case '%':
183 STOR('%');
184 break;
185 case '\0':
186 format--;
187 break;
190 size += tmp;
191 s += tmp;
193 else
195 STOR(*format);
198 format++;
201 STOR('\0');
203 if (size > maxsize)
205 s -= size;
207 if (maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
209 s[maxsize - 1] = '\0';
212 size = 1;
215 return size - 1;