summaryrefslogtreecommitdiff
path: root/ecos/packages/language/c/libc/time/current/src/strftime.cxx
blob: 3240d1b09fcf7bdfb64d3f351abea87b2ee4041d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//========================================================================
//
//      strftime.cxx
//
//      ISO C date and time implementation for strftime()
//
//========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
// -------------------------------------------                              
// This file is part of eCos, the Embedded Configurable Operating System.   
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under    
// the terms of the GNU General Public License as published by the Free     
// Software Foundation; either version 2 or (at your option) any later      
// version.                                                                 
//
// eCos is distributed in the hope that it will be useful, but WITHOUT      
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
// for more details.                                                        
//
// You should have received a copy of the GNU General Public License        
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
//
// As a special exception, if other files instantiate templates or use      
// macros or inline functions from this file, or you compile this file      
// and link it with other works to produce a work based on this file,       
// this file does not by itself cause the resulting work to be covered by   
// the GNU General Public License. However the source code for this file    
// must still be made available in accordance with section (3) of the GNU   
// General Public License v2.                                               
//
// This exception does not invalidate any other reasons why a work based    
// on this file might be covered by the GNU General Public License.         
// -------------------------------------------                              
// ####ECOSGPLCOPYRIGHTEND####                                              
//========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    jlarmour
// Contributors: jlarmour
// Date:         1999-02-26
// Purpose:      Provide implementation of the ISO C function strftime()
//               from ISO C section 7.12.3.5
// Description:  This file provides the implementation of strftime()
// Usage:         
//
//####DESCRIPTIONEND####
//
//========================================================================

// CONFIGURATION

#include <pkgconf/libc_time.h>          // C library configuration

// INCLUDES

#include <cyg/infra/cyg_type.h>    // Common type definitions and support
#include <cyg/infra/cyg_ass.h>     // Assertion infrastructure
#include <cyg/infra/cyg_trac.h>    // Tracing infrastructure
#include <time.h>                  // Main date and time definitions
#include <cyg/libc/time/timeutil.h>// For cyg_libc_time_{day,month}_name{,_len}

// FUNCTION PROTOTYPES

// FIXME: This all needs to be internationalized and localized, both in
// terms of reading and writing multibyte characters, and that it should
// refer to the settings of LC_TIME

// FUNCTIONS

// This helper function actually processes the format. Which format to insert
// is indicated by fmtchar, sizeleft is the maximum space allowed to be used
// in buf. The number of bytes written is returned, or -1 if there was no
// space

static cyg_count8
do_format(cyg_uint8 fmtchar, cyg_ucount32 sizeleft, char *buf,
          const struct tm *timeptr)
{
    cyg_count8 i;

    switch (fmtchar) {
    case 'a':
        CYG_PRECONDITION((timeptr->tm_wday >= 0) && (timeptr->tm_wday < 7),
                         "timeptr->tm_wday out of range!");
        if (sizeleft<3)
            return -1;
        buf[0] = cyg_libc_time_day_name[timeptr->tm_wday][0];
        buf[1] = cyg_libc_time_day_name[timeptr->tm_wday][1];
        buf[2] = cyg_libc_time_day_name[timeptr->tm_wday][2];
        return 3;
    case 'A':
        CYG_PRECONDITION((timeptr->tm_wday >= 0) && (timeptr->tm_wday < 7),
                         "timeptr->tm_wday out of range!");
        if (sizeleft < cyg_libc_time_day_name_len[timeptr->tm_wday])
            return -1;
        for (i=0; i<cyg_libc_time_day_name_len[timeptr->tm_wday]; ++i)
            buf[i] = cyg_libc_time_day_name[timeptr->tm_wday][i];
        return i;
#ifdef CYGFUN_LIBC_TIME_SUS_EXTNS
    case 'h':
        // ** fall through **
#endif
    case 'b':
        CYG_PRECONDITION((timeptr->tm_mon >= 0) && (timeptr->tm_mon < 12),
                         "timeptr->tm_mon out of range!");
        if (sizeleft<3)
            return -1;
        buf[0] = cyg_libc_time_month_name[timeptr->tm_mon][0];
        buf[1] = cyg_libc_time_month_name[timeptr->tm_mon][1];
        buf[2] = cyg_libc_time_month_name[timeptr->tm_mon][2];
        return 3;
    case 'B':
        CYG_PRECONDITION((timeptr->tm_mon >= 0) && (timeptr->tm_mon < 12),
                         "timeptr->tm_mon out of range!");
        if (sizeleft < cyg_libc_time_month_name_len[timeptr->tm_mon])
            return -1;
        for (i=0; i<cyg_libc_time_month_name_len[timeptr->tm_mon]; ++i)
            buf[i] = cyg_libc_time_month_name[timeptr->tm_mon][i];
        return i;
    case 'c':
        if (sizeleft < 26)
            return -1;

        // Recurse! Note that we know that we will have left room for the
        // trailing NULL in the strftime body
        
        i = strftime( buf, sizeleft+1, "%a %b %d %I:%M:%S%p %Y", timeptr);
        
        return ((0==i) ? -1 : i);
    case 'd':
        // Currently I don't check _actual_ numbers of days in each month here
        // FIXME: No reason why not though
        CYG_PRECONDITION((timeptr->tm_mday >= 1) && (timeptr->tm_mday < 32),
                         "timeptr->tm_mday out of range!");

        if (sizeleft < 2)
            return -1;
        buf[0] = (timeptr->tm_mday / 10) + '0';
        buf[1] = (timeptr->tm_mday % 10) + '0';
        return 2;
#ifdef CYGFUN_LIBC_TIME_SUS_EXTNS
    case 'e':
        // Currently I don't check _actual_ numbers of days in each month here
        // FIXME: No reason why not though
        CYG_PRECONDITION((timeptr->tm_mday >= 1) && (timeptr->tm_mday < 32),
                         "timeptr->tm_mday out of range!");
        if (sizeleft < 2)
            return -1;
        i = (timeptr->tm_mday / 10);
        buf[0] = (0 == i) ? ' ' : i + '0';
        buf[1] = (timeptr->tm_mday % 10) + '0';
        return 2;
#endif
    case 'H':
        CYG_PRECONDITION((timeptr->tm_hour >= 0) && (timeptr->tm_hour < 24),
                         "timeptr->tm_hour out of range!");
        if (sizeleft < 2)
            return -1;
        buf[0] = (timeptr->tm_hour / 10) + '0';
        buf[1] = (timeptr->tm_hour % 10) + '0';
        return 2;
    case 'I':
        CYG_PRECONDITION((timeptr->tm_hour >= 0) && (timeptr->tm_hour < 24),
                         "timeptr->tm_hour out of range!");
        if (sizeleft < 2)
            return -1;
        buf[0] = (((timeptr->tm_hour%12) ? (timeptr->tm_hour%12) : 12) / 10) + '0';
        buf[1] = (((timeptr->tm_hour%12) ? (timeptr->tm_hour%12) : 12) % 10) + '0';
        return 2;
    case 'j':
        CYG_PRECONDITION((timeptr->tm_yday >= 0) && (timeptr->tm_yday < 366),
                         "timeptr->tm_yday out of range!");
        if (sizeleft < 3)
            return -1;
        buf[0] = (timeptr->tm_yday / 100) + '0';
        buf[1] = ((timeptr->tm_yday % 100) / 10) + '0';
        buf[2] = (timeptr->tm_yday % 10) + '0';
        return 3;
    case 'm':
        CYG_PRECONDITION((timeptr->tm_mon >= 0) && (timeptr->tm_mon < 12),
                         "timeptr->tm_mon out of range!");
        if (sizeleft < 2)
            return -1;
        buf[0] = ((timeptr->tm_mon+1) / 10) + '0';
        buf[1] = ((timeptr->tm_mon+1) % 10) + '0';
        return 2;
    case 'M':
        CYG_PRECONDITION((timeptr->tm_min >= 0) && (timeptr->tm_min < 60),
                         "timeptr->tm_min out of range!");
        if (sizeleft < 2)
            return -1;
        buf[0] = (timeptr->tm_min / 10) + '0';
        buf[1] = (timeptr->tm_min % 10) + '0';
        return 2;
    case 'p':
        CYG_PRECONDITION((timeptr->tm_hour >= 0) && (timeptr->tm_hour < 24),
                         "timeptr->tm_hour out of range!");
        if (sizeleft < 2)
            return -1;
        buf[0] = (timeptr->tm_hour > 11) ? 'p' : 'a';
        buf[1] = 'm';
        return 2;
    case 'S':
        CYG_PRECONDITION((timeptr->tm_sec >= 0) && (timeptr->tm_sec < 62),
                         "timeptr->tm_sec out of range!");
        if (sizeleft < 2)
            return -1;
        buf[0] = (timeptr->tm_sec / 10) + '0';
        buf[1] = (timeptr->tm_sec % 10) + '0';
        return 2;
#ifdef CYGFUN_LIBC_TIME_SUS_EXTNS
    case 'T':
        if (sizeleft < 8)
            return -1;

        // Recurse! Note that we know that we will have left room for the
        // trailing NULL in the strftime body
        
        i = strftime( buf, sizeleft+1, "%H:%M:%S", timeptr);
        
        return ((0==i) ? -1 : i);
#endif
    case 'U':
        CYG_PRECONDITION((timeptr->tm_wday >= 0) && (timeptr->tm_wday < 7),
                         "timeptr->tm_wday out of range!");
        CYG_PRECONDITION((timeptr->tm_yday >= 0) && (timeptr->tm_yday < 366),
                         "timeptr->tm_yday out of range!");
        if (sizeleft < 2)
            return -1;
        i = (timeptr->tm_yday - timeptr->tm_wday + 7) / 7;
        buf[0] = (i / 10) + '0';
        buf[1] = (i % 10) + '0';
        return 2;
    case 'w':
        CYG_PRECONDITION((timeptr->tm_wday >= 0) && (timeptr->tm_wday < 7),
                         "timeptr->tm_wday out of range!");
        // Don't need to check size - we'll always be called with sizeleft > 0
        buf[0] = timeptr->tm_wday + '0';
        return 1;
    case 'W':
        CYG_PRECONDITION((timeptr->tm_wday >= 0) && (timeptr->tm_wday < 7),
                         "timeptr->tm_wday out of range!");
        CYG_PRECONDITION((timeptr->tm_yday >= 0) && (timeptr->tm_yday < 366),
                         "timeptr->tm_yday out of range!");
        if (sizeleft < 2)
            return -1;
        i = (timeptr->tm_yday + ((8-timeptr->tm_wday) % 7)) / 7;
        buf[0] = (i / 10) + '0';
        buf[1] = (i % 10) + '0';
        return 2;
    case 'x':
        if (sizeleft < 15)
            return -1;

        // Recurse! Note that we know that we will have left room for the
        // trailing NULL in the strftime body

        i = strftime( buf, sizeleft+1, "%a %b %d %Y", timeptr);

        return (0==i) ? -1 : i;
    case 'X':
        if (sizeleft < 10)
            return -1;

        // Recurse! Note that we know that we will have left room for the
        // trailing NULL in the strftime body

        i = strftime( buf, sizeleft+1, "%I:%M:%S%p", timeptr);

        return (0==i) ? -1 : i;
    case 'y':
        CYG_PRECONDITION((timeptr->tm_year > -1900) &&
                         (timeptr->tm_year < 8100),
                         "timeptr->tm_year out of range!");
        if (sizeleft < 2)
            return -1;
        buf[0] = ((timeptr->tm_year % 100) / 10) + '0';
        buf[1] = ((timeptr->tm_year % 100) % 10) + '0';
        return 2;
    case 'Y':
        CYG_PRECONDITION((timeptr->tm_year > -1900) &&
                         (timeptr->tm_year < 8100),
                         "timeptr->tm_year out of range!");
        if (sizeleft < 4)
            return -1;
        buf[0] = ((1900+timeptr->tm_year) / 1000) + '0';
        buf[1] = (((1900+timeptr->tm_year) % 1000) / 100) + '0';
        buf[2] = ((timeptr->tm_year % 100) / 10) + '0';
        buf[3] = (timeptr->tm_year % 10) + '0';
        return 4;
    case 'Z':
        // FIXME: Should store zone in timeptr->tm_zone, or failing that
        // read TZ env variable using tzset()
        return 0;
    case '%':
        // Don't need to check size - we'll always be called with sizeleft > 0
        buf[0] = '%';
        return 1;
    default:
        CYG_FAIL("invalid format character!");
        return -1;
    } // switch

} // do_format()

externC size_t
strftime( char *s, size_t maxsize, const char *format,
          const struct tm *timeptr)
{
    CYG_REPORT_FUNCNAMETYPE("strftime", "returning string length %d");
    CYG_CHECK_DATA_PTR(s, "string s is not a valid address!");
    CYG_CHECK_DATA_PTR(format, "format string is not at a valid address!");
    CYG_CHECK_DATA_PTR(timeptr,
                       "struct tm argument is not at a valid address!");
    CYG_REPORT_FUNCARG4("s is at address %08x, maxsize=%d, format=\"%s\", "
                        "timeptr is at address %08x",
                        s, maxsize, format, timeptr);

    if (!maxsize) {
        CYG_REPORT_RETVAL(0);
        return 0;
    } // if

    // lets leave the room for the trailing null up front
    --maxsize;

    cyg_ucount32 i, spos;
    cyg_count8 dof_ret;
    
    for (i=0, spos=0; (spos<maxsize) && (format[i] != '\0'); ++i) {
        if (format[i] == '%') {
            if (format[++i] == '\0')
                break;
            dof_ret = do_format(format[i], maxsize - spos, &s[spos], timeptr);
            // was there room to write _anything_?
            if (dof_ret < 0) {
                spos=0;  // ISO says we must return 0 and the contents of s
                         // are indeterminate
                break;
            }
            spos += dof_ret;
        }
        else {
            s[spos++] = format[i];
        } // else
    } // for
    
    s[spos] = '\0';
    
    CYG_REPORT_RETVAL(spos);
    return spos;

} // strftime()


// EOF strftime.cxx