OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libc/] [time/] [v2_0/] [src/] [strptime.cxx] - Blame information for rev 595

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//
2
// Adapted for use in eCos by Gary Thomas
3
// Copyright (C) 2003 Gary Thomas
4
//
5
 
6
/*
7
 * Copyright (c) 1999 Kungliga Tekniska Högskolan
8
 * (Royal Institute of Technology, Stockholm, Sweden).
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 *
15
 * 1. Redistributions of source code must retain the above copyright
16
 *    notice, this list of conditions and the following disclaimer.
17
 *
18
 * 2. Redistributions in binary form must reproduce the above copyright
19
 *    notice, this list of conditions and the following disclaimer in the
20
 *    documentation and/or other materials provided with the distribution.
21
 *
22
 * 3. Neither the name of KTH nor the names of its contributors may be
23
 *    used to endorse or promote products derived from this software without
24
 *    specific prior written permission.
25
 *
26
 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
27
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
30
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
37
 
38
#include <stddef.h>
39
#include <stdlib.h>
40
#include <ctype.h>
41
#include <time.h>
42
 
43
static const char *weekdays[] = {
44
    "Sunday",
45
    "Monday",
46
    "Tuesday",
47
    "Wednesday",
48
    "Thursday",
49
    "Friday",
50
    "Saturday",
51
    NULL
52
};
53
 
54
static const char *month[] = {
55
    "January",
56
    "February",
57
    "Mars",
58
    "April",
59
    "May",
60
    "June",
61
    "July",
62
    "August",
63
    "September",
64
    "October",
65
    "November",
66
    "December",
67
    NULL,
68
};
69
 
70
static const char *ampm[] = {
71
    "am",
72
    "pm",
73
    NULL
74
};
75
 
76
/*
77
 * Try to match `*buf' to one of the strings in `strs'.  Return the
78
 * index of the matching string (or -1 if none).  Also advance buf.
79
 */
80
 
81
static int
82
match_string (const char **buf, const char **strs, int ablen)
83
{
84
    int i = 0;
85
 
86
    for (i = 0; strs[i] != NULL; ++i) {
87
        int len = ablen > 0 ? ablen : strlen (strs[i]);
88
 
89
        if (strncasecmp (*buf, strs[i], len) == 0) {
90
            *buf += len;
91
            return i;
92
        }
93
    }
94
    return -1;
95
}
96
 
97
/*
98
 * tm_year is relative this year
99
 */
100
 
101
const int tm_year_base = 1900;
102
 
103
/*
104
 * Return TRUE iff `year' was a leap year.
105
 */
106
 
107
static int
108
is_leap_year (int year)
109
{
110
    return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
111
}
112
 
113
/*
114
 * Return the weekday [0,6] (0 = Sunday) of the first day of `year'
115
 */
116
 
117
static int
118
first_day (int year)
119
{
120
    int ret = 4;  // Jan 1, 1970 was on Thursday
121
 
122
    for (; year > 1970; --year)
123
        ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
124
    return ret;
125
}
126
 
127
/*
128
 * Set `timeptr' given `wnum' (week number [0, 53])
129
 */
130
 
131
static void
132
set_week_number_sun (struct tm *timeptr, int wnum)
133
{
134
    int fday = first_day (timeptr->tm_year + tm_year_base);
135
 
136
    timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
137
    if (timeptr->tm_yday < 0) {
138
        timeptr->tm_wday = fday;
139
        timeptr->tm_yday = 0;
140
    }
141
}
142
 
143
/*
144
 * Set `timeptr' given `wnum' (week number [0, 53])
145
 */
146
 
147
static void
148
set_week_number_mon (struct tm *timeptr, int wnum)
149
{
150
    int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
151
 
152
    timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
153
    if (timeptr->tm_yday < 0) {
154
        timeptr->tm_wday = (fday + 1) % 7;
155
        timeptr->tm_yday = 0;
156
    }
157
}
158
 
159
/*
160
 * Set `timeptr' given `wnum' (week number [0, 53])
161
 */
162
 
163
static void
164
set_week_number_mon4 (struct tm *timeptr, int wnum)
165
{
166
    int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
167
    int offset = 0;
168
 
169
    if (fday < 4)
170
        offset += 7;
171
 
172
    timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
173
    if (timeptr->tm_yday < 0) {
174
        timeptr->tm_wday = fday;
175
        timeptr->tm_yday = 0;
176
    }
177
}
178
 
179
/*
180
 *
181
 */
182
 
183
char *
184
strptime (const char *buf, const char *format, struct tm *timeptr)
185
{
186
    char c;
187
 
188
    timeptr->tm_yday = 1;  // So it's always valid
189
    timeptr->tm_isdst = 0;
190
 
191
    for (; (c = *format) != '\0'; ++format) {
192
        char *s;
193
        int ret;
194
 
195
        if (isspace (c)) {
196
            while (isspace (*buf))
197
                ++buf;
198
        } else if (c == '%' && format[1] != '\0') {
199
            c = *++format;
200
            if (c == 'E' || c == 'O')
201
                c = *++format;
202
            switch (c) {
203
            case 'A' :
204
                ret = match_string (&buf, weekdays, 0);
205
                if (ret < 0)
206
                    return NULL;
207
                timeptr->tm_wday = ret;
208
                break;
209
            case 'a' :
210
                ret = match_string (&buf, weekdays, 3);
211
                if (ret < 0)
212
                    return NULL;
213
                timeptr->tm_wday = ret;
214
                break;
215
            case 'B' :
216
                ret = match_string (&buf, month, 0);
217
                if (ret < 0)
218
                    return NULL;
219
                timeptr->tm_mon = ret;
220
                break;
221
            case 'b' :
222
            case 'h' :
223
                ret = match_string (&buf, month, 3);
224
                if (ret < 0)
225
                    return NULL;
226
                timeptr->tm_mon = ret;
227
                break;
228
            case 'C' :
229
                ret = strtol (buf, &s, 10);
230
                if (s == buf)
231
                    return NULL;
232
                timeptr->tm_year = (ret * 100) - tm_year_base;
233
                buf = s;
234
                break;
235
            case 'c' :
236
                // Date and Time in the current locale - unsupported
237
                return NULL;
238
            case 'D' :          /* %m/%d/%y */
239
                s = strptime (buf, "%m/%d/%y", timeptr);
240
                if (s == NULL)
241
                    return NULL;
242
                buf = s;
243
                break;
244
            case 'd' :
245
            case 'e' :
246
                ret = strtol (buf, &s, 10);
247
                if (s == buf)
248
                    return NULL;
249
                timeptr->tm_mday = ret;
250
                buf = s;
251
                break;
252
            case 'H' :
253
            case 'k' :
254
                ret = strtol (buf, &s, 10);
255
                if (s == buf)
256
                    return NULL;
257
                timeptr->tm_hour = ret;
258
                buf = s;
259
                break;
260
            case 'I' :
261
            case 'l' :
262
                ret = strtol (buf, &s, 10);
263
                if (s == buf)
264
                    return NULL;
265
                if (ret == 12)
266
                    timeptr->tm_hour = 0;
267
                else
268
                    timeptr->tm_hour = ret;
269
                buf = s;
270
                break;
271
            case 'j' :
272
                ret = strtol (buf, &s, 10);
273
                if (s == buf)
274
                    return NULL;
275
                timeptr->tm_yday = ret - 1;
276
                buf = s;
277
                break;
278
            case 'm' :
279
                ret = strtol (buf, &s, 10);
280
                if (s == buf)
281
                    return NULL;
282
                timeptr->tm_mon = ret - 1;
283
                buf = s;
284
                break;
285
            case 'M' :
286
                ret = strtol (buf, &s, 10);
287
                if (s == buf)
288
                    return NULL;
289
                timeptr->tm_min = ret;
290
                buf = s;
291
                break;
292
            case 'n' :
293
                if (*buf == '\n')
294
                    ++buf;
295
                else
296
                    return NULL;
297
                break;
298
            case 'p' :
299
                ret = match_string (&buf, ampm, 0);
300
                if (ret < 0)
301
                    return NULL;
302
                if (timeptr->tm_hour == 0) {
303
                    if (ret == 1)
304
                        timeptr->tm_hour = 12;
305
                } else
306
                    timeptr->tm_hour += 12;
307
                break;
308
            case 'r' :          /* %I:%M:%S %p */
309
                s = strptime (buf, "%I:%M:%S %p", timeptr);
310
                if (s == NULL)
311
                    return NULL;
312
                buf = s;
313
                break;
314
            case 'R' :          /* %H:%M */
315
                s = strptime (buf, "%H:%M", timeptr);
316
                if (s == NULL)
317
                    return NULL;
318
                buf = s;
319
                break;
320
            case 'S' :
321
                ret = strtol (buf, &s, 10);
322
                if (s == buf)
323
                    return NULL;
324
                timeptr->tm_sec = ret;
325
                buf = s;
326
                break;
327
            case 't' :
328
                if (*buf == '\t')
329
                    ++buf;
330
                else
331
                    return NULL;
332
                break;
333
            case 'T' :          /* %H:%M:%S */
334
            case 'X' :
335
                s = strptime (buf, "%H:%M:%S", timeptr);
336
                if (s == NULL)
337
                    return NULL;
338
                buf = s;
339
                break;
340
            case 'u' :
341
                ret = strtol (buf, &s, 10);
342
                if (s == buf)
343
                    return NULL;
344
                timeptr->tm_wday = ret - 1;
345
                buf = s;
346
                break;
347
            case 'w' :
348
                ret = strtol (buf, &s, 10);
349
                if (s == buf)
350
                    return NULL;
351
                timeptr->tm_wday = ret;
352
                buf = s;
353
                break;
354
            case 'U' :
355
                ret = strtol (buf, &s, 10);
356
                if (s == buf)
357
                    return NULL;
358
                set_week_number_sun (timeptr, ret);
359
                buf = s;
360
                break;
361
            case 'V' :
362
                ret = strtol (buf, &s, 10);
363
                if (s == buf)
364
                    return NULL;
365
                set_week_number_mon4 (timeptr, ret);
366
                buf = s;
367
                break;
368
            case 'W' :
369
                ret = strtol (buf, &s, 10);
370
                if (s == buf)
371
                    return NULL;
372
                set_week_number_mon (timeptr, ret);
373
                buf = s;
374
                break;
375
            case 'x' :
376
                s = strptime (buf, "%Y:%m:%d", timeptr);
377
                if (s == NULL)
378
                    return NULL;
379
                buf = s;
380
                break;
381
            case 'y' :
382
                ret = strtol (buf, &s, 10);
383
                if (s == buf)
384
                    return NULL;
385
                if (ret < 70)
386
                    timeptr->tm_year = 100 + ret;
387
                else
388
                    timeptr->tm_year = ret;
389
                buf = s;
390
                break;
391
            case 'Y' :
392
                ret = strtol (buf, &s, 10);
393
                if (s == buf)
394
                    return NULL;
395
                timeptr->tm_year = ret - tm_year_base;
396
                buf = s;
397
                break;
398
            case 'Z' :
399
                // Timezone spec not handled
400
                return NULL;
401
            case '\0' :
402
                --format;
403
                /* FALLTHROUGH */
404
            case '%' :
405
                if (*buf == '%')
406
                    ++buf;
407
                else
408
                    return NULL;
409
                break;
410
            default :
411
                if (*buf == '%' || *++buf == c)
412
                    ++buf;
413
                else
414
                    return NULL;
415
                break;
416
            }
417
        } else {
418
            if (*buf == c)
419
                ++buf;
420
            else
421
                return NULL;
422
        }
423
    }
424
    return (char *)buf;
425
}
426
 
427
// strptime.cxx

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.