Fix: 64-bit compatibility
[AROS.git] / compiler / stdc / strftime.c
blobe9e8e73bb246059b7b2128bb054de370f4e84eac
1 /*
2 Copyright © 1995-2018, 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)
111 return 0;
113 while (*format)
115 if (*format == '%')
117 tmp = 0;
119 switch(*++format)
121 case 'a':
122 ADDS(STR(ABDAY_1+timeptr->tm_wday+1));
123 case 'b':
124 case 'h':
125 ADDS(STR(ABMON_1+timeptr->tm_mon+1));
126 case 'c':
127 ADDS("%m/%d/%y");
128 case 'd':
129 ADDN(2,timeptr->tm_mday);
130 case 'e':
131 ADDN(-2,timeptr->tm_mday);
132 case 'j':
133 ADDN(3,timeptr->tm_yday+1);
134 case 'k':
135 ADDN(-2,timeptr->tm_hour);
136 case 'l':
137 ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
138 case 'm':
139 ADDN(2,timeptr->tm_mon+1);
140 case 'p':
141 ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
142 case 'r':
143 ADDS("%I:%M:%S %p");
144 case 'w':
145 ADDN(1,timeptr->tm_wday);
146 case 'x':
147 ADDS("%m/%d/%y %H:%M:%S");
148 case 'y':
149 ADDN(2,timeptr->tm_year%100);
150 case 'A':
151 ADDS(STR(DAY_1+timeptr->tm_wday+1));
152 case 'B':
153 ADDS(STR(MON_1+timeptr->tm_mon+1));
154 case 'C':
155 ADDS("%a %b %e %H:%M:%S %Y");
156 case 'D':
157 ADDS("%m/%d/%y");
158 case 'H':
159 ADDN(2,timeptr->tm_hour);
160 case 'I':
161 ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
162 case 'M':
163 ADDN(2,timeptr->tm_min);
164 case 'R':
165 ADDS("%H:%M");
166 case 'S':
167 ADDN(2,timeptr->tm_sec);
168 case 'T':
169 case 'X':
170 ADDS("%H:%M:%S");
171 case 'U':
172 ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
173 case 'W':
174 ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
175 case 'Y':
176 ADDN(4,timeptr->tm_year+1900);
177 case 't':
178 STOR('\t');
179 break;
180 case 'n':
181 STOR('\n');
182 break;
183 case '%':
184 STOR('%');
185 break;
186 case '\0':
187 format--;
188 break;
191 size += tmp;
192 s += tmp;
194 else
196 STOR(*format);
199 format++;
202 STOR('\0');
204 if (size > maxsize)
206 s -= size;
208 if (maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
210 s[maxsize - 1] = '\0';
213 size = 1;
216 return size - 1;