Added setting position while duplicating file handles with FMF_WRITE mode. It's neede...
[cake.git] / compiler / clib / strftime.c
blobeddeff90fb60e3246006124fe4dbd6e553122f26
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 size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
76 size_t size = 0, tmp;
78 if (format == NULL || timeptr == NULL) return 0;
80 while (*format)
82 if (*format == '%')
84 tmp = 0;
86 switch(*++format)
88 case 'a':
89 ADDS(STR(ABDAY_1+timeptr->tm_wday+1));
90 case 'b':
91 case 'h':
92 ADDS(STR(ABMON_1+timeptr->tm_mon+1));
93 case 'c':
94 ADDS("%m/%d/%y");
95 case 'd':
96 ADDN(2,timeptr->tm_mday);
97 case 'e':
98 ADDN(-2,timeptr->tm_mday);
99 case 'j':
100 ADDN(3,timeptr->tm_yday+1);
101 case 'k':
102 ADDN(-2,timeptr->tm_hour);
103 case 'l':
104 ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
105 case 'm':
106 ADDN(2,timeptr->tm_mon+1);
107 case 'p':
108 ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
109 case 'r':
110 ADDS("%I:%M:%S %p");
111 case 'w':
112 ADDN(1,timeptr->tm_wday);
113 case 'x':
114 ADDS("%m/%d/%y %H:%M:%S");
115 case 'y':
116 ADDN(2,timeptr->tm_year%100);
117 case 'A':
118 ADDS(STR(DAY_1+timeptr->tm_wday+1));
119 case 'B':
120 ADDS(STR(MON_1+timeptr->tm_mon+1));
121 case 'C':
122 ADDS("%a %b %e %H:%M:%S %Y");
123 case 'D':
124 ADDS("%m/%d/%y");
125 case 'H':
126 ADDN(2,timeptr->tm_hour);
127 case 'I':
128 ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
129 case 'M':
130 ADDN(2,timeptr->tm_min);
131 case 'R':
132 ADDS("%H:%M");
133 case 'S':
134 ADDN(2,timeptr->tm_sec);
135 case 'T':
136 case 'X':
137 ADDS("%H:%M:%S");
138 case 'U':
139 ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
140 case 'W':
141 ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
142 case 'Y':
143 ADDN(4,timeptr->tm_year+1900);
144 case 't':
145 STOR('\t');
146 break;
147 case 'n':
148 STOR('\n');
149 break;
150 case '%':
151 STOR('%');
152 break;
153 case '\0':
154 format--;
155 break;
158 size += tmp;
159 s += tmp;
161 else
163 STOR(*format);
166 format++;
169 STOR('\0');
171 if (size > maxsize)
173 s -= size;
175 if (maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
177 s[maxsize - 1] = '\0';
180 size = 1;
183 return size - 1;